public async Task ValidateQueryNotFoundResponse()
        {
            CosmosDatabase db = await CosmosNotFoundTests.client.CreateDatabaseAsync("NotFoundTest" + Guid.NewGuid().ToString());

            CosmosContainer container = await db.CreateContainerAsync("NotFoundTest" + Guid.NewGuid().ToString(), "/pk", 500);

            dynamic randomItem = new { id = "test", pk = "testpk" };
            await container.CreateItemAsync(randomItem);

            await container.DeleteAsync();

            var crossPartitionQueryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2);
            var queryResponse = await crossPartitionQueryIterator.FetchNextSetAsync();

            Assert.IsNotNull(queryResponse);
            Assert.AreEqual(HttpStatusCode.Gone, queryResponse.StatusCode);

            var queryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 1, partitionKey: new Cosmos.PartitionKey("testpk"));

            this.VerifyQueryNotFoundResponse(await queryIterator.FetchNextSetAsync());

            var crossPartitionQueryIterator2 = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2);

            this.VerifyQueryNotFoundResponse(await crossPartitionQueryIterator2.FetchNextSetAsync());

            await db.DeleteAsync();
        }
Ejemplo n.º 2
0
        private static async Task QueryItemsInPartitionAsStreams(CosmosContainer container)
        {
            // SQL
            FeedIterator setIterator = container.CreateItemQueryStream(
                "SELECT F.id, F.LastName, F.IsRegistered FROM Families F",
                partitionKey: new PartitionKey("Anderson"),
                maxConcurrency: 1,
                maxItemCount: 1);

            int count = 0;

            while (setIterator.HasMoreResults)
            {
                using (CosmosResponseMessage response = await setIterator.FetchNextSetAsync())
                {
                    Assert("Response failed", response.IsSuccessStatusCode);
                    count++;
                    using (StreamReader sr = new StreamReader(response.Content))
                        using (JsonTextReader jtr = new JsonTextReader(sr))
                        {
                            JsonSerializer jsonSerializer = new JsonSerializer();
                            dynamic        items          = jsonSerializer.Deserialize <dynamic>(jtr).Documents;
                            Assert("Expected one family", items.Count == 1);
                            dynamic item = items[0];
                            Assert($"Expected LastName: Anderson Actual: {item.LastName}", string.Equals("Anderson", item.LastName.ToString(), StringComparison.InvariantCulture));
                        }
                }
            }

            Assert("Expected 1 family", count == 1);
        }
        private async Task ItemOperations(CosmosContainer container, bool containerNotExist)
        {
            if (containerNotExist)
            {
                dynamic randomItem = new { id = "test", pk = "doesnotexist" };
                Stream  create     = jsonSerializer.ToStream <dynamic>(randomItem);
                this.VerifyNotFoundResponse(await container.CreateItemStreamAsync(new PartitionKey(randomItem.pk), create));

                var queryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2);
                this.VerifyQueryNotFoundResponse(await queryIterator.FetchNextSetAsync());

                var feedIterator = container.GetItemsStreamIterator();
                this.VerifyNotFoundResponse(await feedIterator.FetchNextSetAsync());

                dynamic randomUpsertItem = new { id = DoesNotExist, pk = DoesNotExist, status = 42 };
                Stream  upsert           = jsonSerializer.ToStream <dynamic>(randomUpsertItem);
                this.VerifyNotFoundResponse(await container.UpsertItemStreamAsync(
                                                partitionKey: new Cosmos.PartitionKey(randomUpsertItem.pk),
                                                streamPayload: upsert));
            }

            this.VerifyNotFoundResponse(await container.ReadItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist));
            this.VerifyNotFoundResponse(await container.DeleteItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist));

            dynamic randomReplaceItem = new { id = "test", pk = "doesnotexist", status = 42 };
            Stream  replace           = jsonSerializer.ToStream <dynamic>(randomReplaceItem);

            this.VerifyNotFoundResponse(await container.ReplaceItemStreamAsync(
                                            partitionKey: new Cosmos.PartitionKey(randomReplaceItem.pk),
                                            id: randomReplaceItem.id,
                                            streamPayload: replace));
        }
Ejemplo n.º 4
0
        private static async Task QueryItems()
        {
            //******************************************************************************************************************
            // 1.4 - Query for items by a property other than Id
            //
            // NOTE: Operations like AsEnumerable(), ToList(), ToArray() will make as many trips to the database
            //       as required to fetch the entire result-set. Even if you set MaxItemCount to a smaller number.
            //       MaxItemCount just controls how many results to fetch each trip.
            //******************************************************************************************************************
            Console.WriteLine("\n1.4 - Querying for a item using its AccountNumber property");

            CosmosSqlQueryDefinition query = new CosmosSqlQueryDefinition(
                "select * from sales s where s.AccountNumber = @AccountInput ")
                                             .UseParameter("@AccountInput", "Account1");

            FeedIterator <SalesOrder> resultSet = container.CreateItemQuery <SalesOrder>(
                query,
                partitionKey: new PartitionKey("Account1"),
                maxItemCount: 1);

            List <SalesOrder> allSalesForAccount1 = new List <SalesOrder>();

            while (resultSet.HasMoreResults)
            {
                SalesOrder sale = (await resultSet.FetchNextSetAsync()).First();
                Console.WriteLine($"\n1.4.1 Account Number: {sale.AccountNumber}; Id: {sale.Id} ");
                allSalesForAccount1.Add(sale);
            }

            Console.WriteLine($"\n1.4.2 Query found {allSalesForAccount1.Count} items.");

            // Use the same query as before but get the cosmos response message to access the stream directly
            FeedIterator streamResultSet = container.CreateItemQueryStream(
                query,
                maxConcurrency: 1,
                partitionKey: new PartitionKey("Account1"),
                maxItemCount: 10);

            List <SalesOrder> allSalesForAccount1FromStream = new List <SalesOrder>();

            while (streamResultSet.HasMoreResults)
            {
                using (CosmosResponseMessage responseMessage = await streamResultSet.FetchNextSetAsync())
                {
                    // Item stream operations do not throw exceptions for better performance
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        dynamic           streamResponse = FromStream <dynamic>(responseMessage.Content);
                        List <SalesOrder> salesOrders    = streamResponse.Documents.ToObject <List <SalesOrder> >();
                        Console.WriteLine($"\n1.4.3 - Item Query via stream {salesOrders.Count}");
                        allSalesForAccount1FromStream.AddRange(salesOrders);
                    }
                    else
                    {
                        Console.WriteLine($"Query item from stream failed. Status code: {responseMessage.StatusCode} Message: {responseMessage.ErrorMessage}");
                    }
                }
            }

            Console.WriteLine($"\n1.4.4 Query found {allSalesForAccount1FromStream.Count} items.");

            if (allSalesForAccount1.Count != allSalesForAccount1FromStream.Count)
            {
                throw new InvalidDataException($"Both query operations should return the same list");
            }
        }