Esempio n. 1
0
        /// <summary>
        /// Runs a simple script which just does a server side query
        /// </summary>
        private static async Task RunSimpleScript(CosmosContainer container)
        {
            // 1. Create stored procedure for script.
            string scriptFileName = @"js\SimpleScript.js";
            string scriptId       = Path.GetFileNameWithoutExtension(scriptFileName);

            await TryDeleteStoredProcedure(container, scriptId);

            CosmosStoredProcedure sproc = await container.StoredProcedures.CreateStoredProceducreAsync(scriptId, File.ReadAllText(scriptFileName));

            // 2. Create a document.
            SampleDocument doc = new SampleDocument
            {
                Id           = Guid.NewGuid().ToString(),
                LastName     = "Estel",
                Headquarters = "Russia",
                Locations    = new Location[] { new Location {
                                                    Country = "Russia", City = "Novosibirsk"
                                                } },
                Income = 50000
            };

            CosmosItemResponse <SampleDocument> created = await container.Items.CreateItemAsync(doc.LastName, doc);

            // 3. Run the script. Pass "Hello, " as parameter.
            // The script will take the 1st document and echo: Hello, <document as json>.
            CosmosItemResponse <string> response = await container.StoredProcedures[scriptId].ExecuteAsync <string, string>(doc.LastName, "Hello");

            Console.WriteLine("Result from script: {0}\r\n", response.Resource);

            await container.Items.DeleteItemAsync <SampleDocument>(doc.LastName, doc.Id);
        }
Esempio n. 2
0
        public void AddKeyConstraintNoneMixedWithAddTracksDocumentsProperly()
        {
            var session = provider.OpenSession <SampleDocument>();

            using (session)
            {
                var item = (from d in session.Query() where d.Name == "a" select d).Single();

                item.Name = "a modified name";

                var newItem = new SampleDocument {
                    Key = item.Key, Name = "a new a"
                };
                var newItemNoDelete = new SampleDocument {
                    Name = "d"
                };

                session.Add(newItem);
                session.Add(KeyConstraint.None, newItemNoDelete);

                session.Commit();

                var result          = (from d in session.Query() where d.Key == newItem.Key select d).Single();
                var resultOfNewItem = (from d in session.Query() where d.Key == newItemNoDelete.Key select d).Single();
                Assert.That(result.Name, Is.EqualTo("a new a"));
                Assert.That(resultOfNewItem.Name, Is.EqualTo("d"));
            }
        }
Esempio n. 3
0
        public void Select()
        {
            var d = new SampleDocument {Name = "My Document"};
            AddDocument(d);

            var documents = provider.AsQueryable<SampleDocument>();

            var result = from doc in documents select doc;

            Assert.That(result.FirstOrDefault().Name, Is.EqualTo(d.Name));
        }
Esempio n. 4
0
        public void Select()
        {
            var d = new SampleDocument {
                Name = "My Document"
            };

            AddDocument(d);

            var documents = provider.AsQueryable <SampleDocument>();

            var result = from doc in documents select doc;

            Assert.That(result.FirstOrDefault().Name, Is.EqualTo(d.Name));
        }
Esempio n. 5
0
        public void SelectScalar()
        {
            const int scalar = 99;

            var d = new SampleDocument {Name = "a", NullableScalar = scalar};

            AddDocument(d);

            var documents = provider.AsQueryable<SampleDocument>();

            var result = from doc in documents select doc.NullableScalar;

            Assert.That(result.FirstOrDefault(), Is.EqualTo(scalar));
        }
Esempio n. 6
0
        public void SelectScalar()
        {
            const int scalar = 99;

            var d = new SampleDocument {
                Name = "a", NullableScalar = scalar
            };

            AddDocument(d);

            var documents = provider.AsQueryable <SampleDocument>();

            var result = from doc in documents select doc.NullableScalar;

            Assert.That(result.FirstOrDefault(), Is.EqualTo(scalar));
        }
        public void AddReplacesModifiedDocument()
        {
            var session = provider.OpenSession <SampleDocument>();

            using (session)
            {
                var item = (from d in session.Query() where d.Name == "a" select d).Single();

                item.Name = "a modified name";

                var newItem = new SampleDocument {
                    Key = item.Key, Name = "a new a"
                };

                session.Add(newItem);

                session.Commit();

                var result = (from d in session.Query() where d.Key == newItem.Key select d).Single();
                Assert.That(result.Name, Is.EqualTo("a new a"));
            }
        }
Esempio n. 8
0
        public void AddKeyConstraintNoneCanAllowDuplicates()
        {
            var session = provider.OpenSession <SampleDocument>();

            using (session)
            {
                var newItem = new SampleDocument {
                    Key = "a1", Name = "a new a"
                };

                session.Add(KeyConstraint.None, newItem);

                session.Commit();

                session.Add(KeyConstraint.None, newItem);

                session.Commit();

                var results = (from d in session.Query() where d.Key == newItem.Key select d).ToList();
                Assert.That(results.Count(), Is.EqualTo(2));
            }
        }
Esempio n. 9
0
 private ConvertedDocument Convert(SampleDocument doc)
 {
     return(new ConvertedDocument {
         Name = doc.Name
     });
 }
Esempio n. 10
0
 private ConvertedDocument Convert(SampleDocument doc)
 {
     return new ConvertedDocument {Name = doc.Name};
 }
Esempio n. 11
0
        public void AddKeyConstraintNoneMixedWithAddTracksDocumentsProperly()
        {
            var session = provider.OpenSession<SampleDocument>();

            using (session)
            {

                var item = (from d in session.Query() where d.Name == "a" select d).Single();

                item.Name = "a modified name";

                var newItem = new SampleDocument { Key = item.Key, Name = "a new a" };
                var newItemNoDelete = new SampleDocument { Name = "d" };

                session.Add(newItem);
                session.Add(KeyConstraint.None, newItemNoDelete);

                session.Commit();

                var result = (from d in session.Query() where d.Key == newItem.Key select d).Single();
                var resultOfNewItem = (from d in session.Query() where d.Key == newItemNoDelete.Key select d).Single();
                Assert.That(result.Name, Is.EqualTo("a new a"));
                Assert.That(resultOfNewItem.Name, Is.EqualTo("d"));
            }
        }
Esempio n. 12
0
        public void AddKeyConstraintNoneCanAllowDuplicates()
        {
            var session = provider.OpenSession<SampleDocument>();

            using (session)
            {

                var newItem = new SampleDocument { Key = "a1", Name = "a new a" };

                session.Add(KeyConstraint.None, newItem);

                session.Commit();

                session.Add(KeyConstraint.None, newItem);

                session.Commit();

                var results = (from d in session.Query() where d.Key == newItem.Key select d).ToList();
                Assert.That(results.Count(), Is.EqualTo(2));
            }
        }
Esempio n. 13
0
        public void AddReplacesModifiedDocument()
        {
            var session = provider.OpenSession<SampleDocument>();

            using (session)
            {
                var item = (from d in session.Query() where d.Name == "a" select d).Single();

                item.Name = "a modified name";

                var newItem = new SampleDocument { Key = item.Key, Name = "a new a" };

                session.Add(newItem);

                session.Commit();

                var result = (from d in session.Query() where d.Key == newItem.Key select d).Single();
                Assert.That(result.Name, Is.EqualTo("a new a"));
            }
        }