Exemplo n.º 1
0
        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();
        }