Exemplo n.º 1
0
        public async void QueryAll()
        {
            using (var client = DocumentDB.InitializeClient())
                await client.CleanEnvironmentAndLoadDocuments();

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery(collection.DocumentsLink)
                                .ToList();

                Assert.AreEqual(2, documents.Count);
            }

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM root")
                                .ToList();

                Assert.AreEqual(2, documents.Count);
            }
        }
Exemplo n.º 2
0
        public async void Run()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var database = client
                               .CreateDatabaseQuery()
                               .Where(x => x.Id == DocumentDB.DatabaseId)
                               .AsEnumerable <Database>()
                               .SingleOrDefault();

                if (database == null)
                {
                    database = await client.CreateDatabaseAsync(new Database { Id = DocumentDB.DatabaseId });
                }

                String requestContinuation = null;
                do
                {
                    var options = new FeedOptions
                    {
                        MaxItemCount        = 1,
                        RequestContinuation = requestContinuation
                    };

                    var feed = await client.ReadDatabaseFeedAsync(options);

                    var current = feed.SingleOrDefault();

                    requestContinuation = feed.ResponseContinuation;
                } while (!String.IsNullOrEmpty(requestContinuation));

                await client.DeleteDatabaseAsync(database.SelfLink);
            }
        }
Exemplo n.º 3
0
        public async void AddClass()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var document = new User
                {
                    Name     = "Emanuele",
                    Surname  = "DelBono",
                    Contacts = new[]
                    {
                        new Contact {
                            Type = "Email", Value = "*****@*****.**"
                        },
                        new Contact {
                            Type = "Fax", Value = "+39 030 6595241"
                        }
                    },
                    Social = new[]
                    {
                        new Contact {
                            Type = "twitter", Value = "emadb"
                        },
                        new Contact {
                            Type = "github", Value = "emadb"
                        }
                    }
                };
                var response = await client.CreateDocumentAsync(collection.DocumentsLink, document);

                Assert.NotNull(response);
            }
        }
Exemplo n.º 4
0
        public async void AddAndReadAttachment()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var document = await client.CreateDocumentAsync(collection.DocumentsLink, new { id = "1", name = "alessandro" });

                using (var stream = File.Open("Attachment.txt", FileMode.Open))
                    await client.CreateAttachmentAsync(document.Resource.AttachmentsLink, stream);

                var attachment = client
                                 .CreateAttachmentQuery(document.Resource.SelfLink)
                                 .AsEnumerable()
                                 .FirstOrDefault();

                var content = await client.ReadMediaAsync(attachment.MediaLink);

                var buffer = new Byte[content.ContentLength];
                await content.Media.ReadAsync(buffer, 0, (int)content.ContentLength);

                var text = Encoding.UTF8.GetString(buffer);

                Assert.NotNull(text);
            }
        }
Exemplo n.º 5
0
        public async void QuerySubDocuments()
        {
            using (var client = DocumentDB.InitializeClient())
                await client.CleanEnvironmentAndLoadDocuments();

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM x IN root.contacts")
                                .ToList();

                Assert.AreEqual(4, documents.Count);
            }

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery <User>(collection.DocumentsLink)
                                .SelectMany(x => x.Contacts)
                                .ToList();

                Assert.AreEqual(4, documents.Count);
            }
        }
Exemplo n.º 6
0
        public async void QueryWithFilter()
        {
            using (var client = DocumentDB.InitializeClient())
                await client.CleanEnvironmentAndLoadDocuments();

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery <dynamic>(collection.DocumentsLink)
                                .AsEnumerable()
                                .Where(x => x.name == "Alessandro")
                                .ToList();

                Assert.AreEqual(1, documents.Count);
            }

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT * FROM root x WHERE x.name = 'Alessandro'")
                                .ToList();

                Assert.AreEqual(1, documents.Count);
            }
        }
Exemplo n.º 7
0
        public async void QueryWithPaging()
        {
            using (var client = DocumentDB.InitializeClient())
                await client.CleanEnvironmentAndLoadDocuments();

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var count = 0;
                var query = client
                            .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT * FROM root", new FeedOptions {
                    MaxItemCount = 1
                })
                            .AsDocumentQuery();

                while (query.HasMoreResults)
                {
                    var current = await query.ExecuteNextAsync();

                    count++;
                }

                Assert.AreEqual(2, count);
            }
        }
Exemplo n.º 8
0
        public async void Run()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var database = await client.GetOrCreateDatabaseAsync();

                var collection = client
                                 .CreateDocumentCollectionQuery(database.CollectionsLink)
                                 .Where(x => x.Id == DocumentDB.CollectionId)
                                 .AsEnumerable <DocumentCollection>()
                                 .SingleOrDefault();

                if (collection == null)
                {
                    var target = new DocumentCollection {
                        Id = DocumentDB.CollectionId
                    };
                    target.IndexingPolicy.Automatic = true;
                    collection = await client.CreateDocumentCollectionAsync(database.SelfLink, target);
                }

                await client.DeleteDocumentCollectionAsync(collection.SelfLink);

                await client.DeleteDatabaseAsync(database.SelfLink);
            }
        }
Exemplo n.º 9
0
        public async void ExcludeDocument()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documentId = Guid.NewGuid().ToString();
                var document   = await client.CreateDocumentAsync
                                 (
                    collection.DocumentsLink,
                    new { id = documentId, surname = "Melchiori", name = "Alessandro", random = documentId },
                    new RequestOptions { IndexingDirective = IndexingDirective.Exclude }
                                 );

                var exists = await client.ReadDocumentAsync(document.Resource.SelfLink);

                Assert.NotNull(exists);

                var query  = String.Format("SELECT * FROM root x WHERE x.random = '{0}'", documentId);
                var search = client
                             .CreateDocumentQuery <dynamic>(collection.DocumentsLink, query)
                             .AsEnumerable()
                             .Any();
                Assert.False(search);
            }
        }
Exemplo n.º 10
0
        public async void EditDocument()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var document = await client.CreateDocumentAsync(collection.DocumentsLink, new { id = "1", name = "alessandro" });

                await client.ReplaceDocumentAsync(document.Resource.SelfLink, new { id = "1", value = "edited" });
            }
        }
Exemplo n.º 11
0
        public async void LazyIndex()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var target = new DocumentCollection {
                    Id = DocumentDB.CollectionId
                };
                target.IndexingPolicy.IndexingMode = IndexingMode.Lazy;

                var collection = await client.GetOrCreateDocumentCollectionAsync(target);

                //...
            }
        }
Exemplo n.º 12
0
        public async void AddDynamic()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                dynamic document = new
                {
                    id      = "1",
                    name    = "Alessandro",
                    surname = "Melchiori",
                    title   = "software developer"
                };
                var response = await client.CreateDocumentAsync(collection.DocumentsLink, document);

                Assert.NotNull(response);
            }
        }
Exemplo n.º 13
0
        public async void RangeQuery()
        {
            using (var client = DocumentDB.InitializeClient())
                await client.CleanEnvironmentAndLoadDocuments();

            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var documents = client
                                .CreateDocumentQuery <dynamic>(collection.DocumentsLink)
                                .AsEnumerable()
                                .Where(x => x.age < 40 && x.age > 30)
                                .ToList();

                Assert.AreEqual(1, documents.Count);
            }
        }
Exemplo n.º 14
0
        public async void ExcludePaths()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var target = new DocumentCollection {
                    Id = DocumentDB.CollectionId
                };
                target.IndexingPolicy.IncludedPaths.Add(new IndexingPath {
                    Path = "/"
                });
                target.IndexingPolicy.ExcludedPaths.Add("/\"age\"/*");
                target.IndexingPolicy.ExcludedPaths.Add("/\"contact\"/\"value\"/*");

                var collection = await client.GetOrCreateDocumentCollectionAsync(target);

                var documents = GetDocuments();
                foreach (var document in documents)
                {
                    await client.CreateDocumentAsync(collection.DocumentsLink, document);
                }

                var existsByName = client
                                   .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.name = 'Alessandro'")
                                   .AsEnumerable()
                                   .Any();
                Assert.True(existsByName);

                var existsByContactType = client
                                          .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.contact.type = 'email'")
                                          .AsEnumerable()
                                          .Any();
                Assert.True(existsByContactType);

                Assert.Throws <AggregateException>(() => client
                                                   .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.age > 10")
                                                   .AsEnumerable()
                                                   .Any());

                Assert.Throws <AggregateException>(() => client
                                                   .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.contact.value = '*****@*****.**'")
                                                   .AsEnumerable()
                                                   .Any());
            }
        }
Exemplo n.º 15
0
        public async void RangeIndex()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var target = new DocumentCollection {
                    Id = DocumentDB.CollectionId
                };
                target.IndexingPolicy.IncludedPaths.Add(new IndexingPath
                {
                    IndexType = IndexType.Hash,
                    Path      = "/"
                });
                target.IndexingPolicy.IncludedPaths.Add(new IndexingPath
                {
                    IndexType = IndexType.Range,
                    Path      = @"/""age""/?",
                });

                var collection = await client.GetOrCreateDocumentCollectionAsync(target);

                //...
            }
        }
Exemplo n.º 16
0
        public async void AddDocument()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var collection = await client.GetOrCreateDocumentCollectionAsync();

                var document = new UserDocument
                {
                    Name     = "Alessandro",
                    Surname  = "Melchiori",
                    Contacts = new[]
                    {
                        new Contact {
                            Type = "Email", Value = "*****@*****.**"
                        },
                        new Contact {
                            Type = "Fax", Value = "+39 030 6595241"
                        }
                    },
                    Social = new[]
                    {
                        new Contact {
                            Type = "twitter", Value = "amelchiori"
                        },
                        new Contact {
                            Type = "slideshare", Value = "melkio"
                        },
                        new Contact {
                            Type = "github", Value = "melkio"
                        }
                    }
                };
                var response = await client.CreateDocumentAsync(collection.DocumentsLink, document);

                Assert.NotNull(response);
            }
        }
Exemplo n.º 17
0
        public async void ColelctionScan()
        {
            using (var client = DocumentDB.InitializeClient())
            {
                var target = new DocumentCollection {
                    Id = DocumentDB.CollectionId
                };
                target.IndexingPolicy.IncludedPaths.Add(new IndexingPath
                {
                    IndexType = IndexType.Hash,
                    Path      = "/"
                });
                target.IndexingPolicy.IncludedPaths.Add(new IndexingPath
                {
                    IndexType = IndexType.Hash,
                    Path      = @"/""age""/?",
                });

                var collection = await client.GetOrCreateDocumentCollectionAsync(target);

                var document = await client.CreateDocumentAsync(collection.DocumentsLink, new { id = "1", name = "alessandro", age = 35 });

                Assert.Throws <AggregateException>(() => client
                                                   .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.age > 10")
                                                   .AsEnumerable()
                                                   .Any());

                var exists = client
                             .CreateDocumentQuery <dynamic>(collection.DocumentsLink, "SELECT x FROM root x WHERE x.age > 10", new FeedOptions {
                    EnableScanInQuery = true
                })
                             .AsEnumerable()
                             .Any();
                Assert.True(exists);
            }
        }