public void ValidateTransformQuery() { DocumentClient client = TestCommon.CreateClient(true); IQueryable <dynamic> dbQuery = client.CreateDatabaseQuery(@"select * from root r where r.id=""db123""").AsQueryable(); foreach (CosmosDatabaseSettings db in dbQuery) { TestCommon.Delete <CosmosDatabaseSettings>(client, db.ResourceId); } CosmosDatabaseSettings database = client.Create <CosmosDatabaseSettings>(null, new CosmosDatabaseSettings { Id = "db123" }); dbQuery = client.CreateDatabaseQuery(@"select * from root r where r.id=""db123""").AsQueryable(); foreach (CosmosDatabaseSettings db in dbQuery) { Assert.AreEqual(db.Id, "db123"); } Assert.AreNotEqual(0, System.Linq.Dynamic.Core.DynamicQueryableExtensions.AsEnumerable(dbQuery).Count()); IQueryable <dynamic> dbIdQuery = client.CreateDatabaseQuery(@"select r._rid from root r where r.id=""db123""").AsQueryable(); Assert.AreNotEqual(0, System.Linq.Dynamic.Core.DynamicQueryableExtensions.AsEnumerable(dbIdQuery).Count()); CosmosContainerSettings collection = new CosmosContainerSettings { Id = Guid.NewGuid().ToString("N") }; collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent; collection = client.Create <CosmosContainerSettings>(database.ResourceId, collection); int documentsToCreate = 100; for (int i = 0; i < documentsToCreate; i++) { dynamic myDocument = new Document(); myDocument.Id = "doc" + i; myDocument.Title = "MyBook"; //Simple Property. myDocument.Languages = new Language[] { new Language { Name = "English", Copyright = "London Publication" }, new Language { Name = "French", Copyright = "Paris Publication" } }; //Array Property myDocument.Author = new Author { Name = "Don", Location = "France" }; //Complex Property myDocument.Price = 9.99; myDocument = client.CreateDocumentAsync(collection.DocumentsLink, myDocument).Result; } //Read response as dynamic. IQueryable <dynamic> docQuery = client.CreateDocumentQuery(collection.DocumentsLink, @"select * from root r where r.Title=""MyBook""", null); IDocumentQuery <dynamic> DocumentQuery = docQuery.AsDocumentQuery(); FeedResponse <dynamic> queryResponse = DocumentQuery.ExecuteNextAsync().Result; Assert.IsNotNull(queryResponse.ResponseHeaders, "ResponseHeaders is null"); Assert.IsNotNull(queryResponse.ActivityId, "ActivityId is null"); Assert.AreEqual(documentsToCreate, queryResponse.Count); foreach (dynamic myBook in queryResponse) { Assert.AreEqual(myBook.Title, "MyBook"); } client.DeleteDocumentCollectionAsync(collection.SelfLink).Wait(); }
private async Task ValidateServerSideQueryEvalWithPaginationScenario() { DocumentClient client = TestCommon.CreateClient(false, defaultConsistencyLevel: ConsistencyLevel.Session); CosmosDatabaseSettings database = TestCommon.CreateOrGetDatabase(client); CosmosContainerSettings collection = new CosmosContainerSettings { Id = "ConsistentCollection" }; collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent; collection = client.Create <CosmosContainerSettings>( database.ResourceId, collection); //Do script post to insert as many document as we could in a tight loop. string script = @"function() { var output = 0; var client = getContext().getCollection(); function callback(err, docCreated) { if(err) throw 'Error while creating document'; output++; getContext().getResponse().setBody(output); if(output < 50) client.createDocument(client.getSelfLink(), { id: 'testDoc' + output, title : 'My Book'}, {}, callback); }; client.createDocument(client.getSelfLink(), { id: 'testDoc' + output, title : 'My Book'}, {}, callback); }"; StoredProcedureResponse <int> scriptResponse = null; int totalNumberOfDocuments = GatewayTests.CreateExecuteAndDeleteProcedure(client, collection, script, out scriptResponse); int pageSize = 5; int totalHit = 0; IDocumentQuery <Book> documentQuery = (from book in client.CreateDocumentQuery <Book>( collection.SelfLink, new FeedOptions { MaxItemCount = pageSize }) where book.Title == "My Book" select book).AsDocumentQuery(); while (documentQuery.HasMoreResults) { FeedResponse <dynamic> pagedResult = await documentQuery.ExecuteNextAsync(); string isUnfiltered = pagedResult.ResponseHeaders[HttpConstants.HttpHeaders.IsFeedUnfiltered]; Assert.IsTrue(string.IsNullOrEmpty(isUnfiltered), "Query is evaulated in client"); Assert.IsTrue(pagedResult.Count <= pageSize, "Page size is not honored in client site eval"); if (totalHit != 0 && documentQuery.HasMoreResults) { //Except first page and last page we should have seen client continuation token. Assert.IsFalse(pagedResult.ResponseHeaders[HttpConstants.HttpHeaders.Continuation].Contains(HttpConstants.Delimiters.ClientContinuationDelimiter), "Client continuation is missing from the response continuation"); } totalHit += pagedResult.Count; } Assert.AreEqual(totalHit, totalNumberOfDocuments, "Didnt get all the documents"); //Do with default pagination. documentQuery = (from book in client.CreateDocumentQuery <Book>( collection.SelfLink) where book.Title == "My Book" select book).AsDocumentQuery(); totalHit = 0; while (documentQuery.HasMoreResults) { FeedResponse <dynamic> pagedResult = await documentQuery.ExecuteNextAsync(); string isUnfiltered = pagedResult.ResponseHeaders[HttpConstants.HttpHeaders.IsFeedUnfiltered]; Assert.IsTrue(string.IsNullOrEmpty(isUnfiltered), "Query is evaulated in client"); Assert.IsTrue(pagedResult.Count == totalNumberOfDocuments, "Page size is not honored in client site eval"); totalHit += pagedResult.Count; } Assert.AreEqual(totalHit, totalNumberOfDocuments, "Didnt get all the documents"); }