public void TestReplaceDictionary()
        {
            var doc      = new MutableDocument("doc1");
            var profile1 = new MutableDictionaryObject();

            profile1.SetString("name", "Scott Tiger");
            doc.SetDictionary("profile", profile1);
            doc.GetDictionary("profile").ShouldBeEquivalentTo(profile1, "because that is what was set");

            var profile2 = new MutableDictionaryObject();

            profile2.SetString("name", "Daniel Tiger");
            doc.SetDictionary("profile", profile2);
            doc.GetDictionary("profile").ShouldBeEquivalentTo(profile2, "because that is what was set");

            profile1.SetInt("age", 20);
            profile1.GetString("name").Should().Be("Scott Tiger", "because profile1 should be detached now");
            profile1.GetInt("age").Should().Be(20, "because profile1 should be detached now");

            profile2.GetString("name").Should().Be("Daniel Tiger", "because profile2 should be unchanged");
            profile2.GetValue("age").Should().BeNull("because profile2 should be unchanged");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("profile")
            .Should()
            .NotBeSameAs(profile2, "because a new MutableDocument should return a new instance");
            var savedProfile2 = gotDoc.GetDictionary("profile");

            savedProfile2.GetString("name").Should().Be("Daniel Tiger", "because that is what was saved");
        }
        public void TestCreateDictionaryWithCSharpDictionary()
        {
            var dict = new Dictionary <string, object> {
                ["street"] = "1 Main street",
                ["city"]   = "Mountain View",
                ["state"]  = "CA"
            };
            var address = new MutableDictionaryObject(dict);

            address.ShouldBeEquivalentTo(dict, "because that is what was stored");
            address.ToDictionary().ShouldBeEquivalentTo(dict, "because that is what was stored");

            var doc1 = new MutableDocument("doc1");

            doc1.SetDictionary("address", address);
            doc1.GetDictionary("address")
            .Should()
            .BeSameAs(address, "because the document should return the same instance");

            Db.Save(doc1);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("address")
            .ToDictionary()
            .ShouldBeEquivalentTo(dict, "because the content should not have changed");
        }
        public void TestRemoveDictionary()
        {
            var doc      = new MutableDocument("doc1");
            var profile1 = new MutableDictionaryObject();

            profile1.SetString("name", "Scott Tiger");
            doc.SetDictionary("profile", profile1);
            doc.GetDictionary("profile").ShouldBeEquivalentTo(profile1, "because that was what was inserted");
            doc.Contains("profile").Should().BeTrue("because a value exists for that key");

            doc.Remove("profile");
            doc.GetValue("profile").Should().BeNull("beacuse the value for 'profile' was removed");
            doc.Contains("profile").Should().BeFalse("because the value was removed");

            profile1.SetInt("age", 20);
            profile1.GetString("name").Should().Be("Scott Tiger", "because the dictionary object should be unaffected");
            profile1.GetInt("age").Should().Be(20, "because the dictionary should still be editable");

            doc.GetValue("profile").Should()
            .BeNull("because changes to the dictionary should not have any affect anymore");

            var savedDoc = Db.Save(doc);

            savedDoc.GetValue("profile").Should().BeNull("beacuse the value for 'profile' was removed");
            savedDoc.Contains("profile").Should().BeFalse("because the value was removed");
        }
Esempio n. 4
0
        private static void Read1xAttachment()
        {
            var db = _Database;

            using (var document = new MutableDocument()) {
                // # tag::1x-attachment[]
                var attachments = document.GetDictionary("_attachments");
                var avatar      = attachments.GetBlob("avatar");
                var content     = avatar?.Content;
                // # end::1x-attachment[]
            }
        }
        public void TestCreateDictionary()
        {
            var address = new MutableDictionaryObject();

            address.Count.Should().Be(0, "because the dictionary is empty");
            address.ToDictionary().Should().BeEmpty("because the dictionary is empty");

            var doc1 = new MutableDocument("doc1");

            doc1.SetDictionary("address", address);
            doc1.GetDictionary("address")
            .Should()
            .BeSameAs(address, "because the document should return the same instance");

            Db.Save(doc1);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("address").ToDictionary().Should().BeEmpty("because the content should not have changed");
        }
        public void TestSetNestedDictionaries()
        {
            var doc    = new MutableDocument("doc1");
            var level1 = new MutableDictionaryObject();

            level1.SetString("name", "n1");
            doc.SetDictionary("level1", level1);

            var level2 = new MutableDictionaryObject();

            level2.SetString("name", "n2");
            level1.SetDictionary("level2", level2);

            var level3 = new MutableDictionaryObject();

            level3.SetString("name", "n3");
            level2.SetDictionary("level3", level3);

            doc.GetDictionary("level1").ShouldBeEquivalentTo(level1, "because that is what was inserted");
            level1.GetDictionary("level2").ShouldBeEquivalentTo(level2, "because that is what was inserted");
            level2.GetDictionary("level3").ShouldBeEquivalentTo(level3, "because that is what was inserted");
            var dict = new Dictionary <string, object> {
                ["level1"] = new Dictionary <string, object> {
                    ["name"]   = "n1",
                    ["level2"] = new Dictionary <string, object> {
                        ["name"]   = "n2",
                        ["level3"] = new Dictionary <string, object> {
                            ["name"] = "n3"
                        }
                    }
                }
            };

            doc.ToDictionary().ShouldBeEquivalentTo(dict, "because otherwise the document's contents are incorrect");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("level1").Should().NotBeSameAs(level1);
            gotDoc.ToDictionary().ShouldBeEquivalentTo(dict);
        }
        public void TestReplaceDictionaryDifferentType()
        {
            var doc      = new MutableDocument("doc1");
            var profile1 = new MutableDictionaryObject();

            profile1.SetString("name", "Scott Tiger");
            doc.SetDictionary("profile", profile1);
            doc.GetDictionary("profile").ShouldBeEquivalentTo(profile1, "because that is what was set");

            doc.SetString("profile", "Daniel Tiger");
            doc.GetString("profile").Should().Be("Daniel Tiger", "because that is what was set");

            profile1.SetInt("age", 20);
            profile1.GetString("name").Should().Be("Scott Tiger", "because profile1 should be detached now");
            profile1.GetInt("age").Should().Be(20, "because profile1 should be detached now");

            doc.GetString("profile").Should().Be("Daniel Tiger", "because profile1 should not affect the new value");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetString("profile").Should().Be("Daniel Tiger", "because that is what was saved");
        }
Esempio n. 8
0
        private void UpdateDoc(MutableDocument doc, int rounds, string tag)
        {
            for (var i = 1; i <= rounds; i++)
            {
                doc.SetInt("update", i);
                doc.SetString("tag", tag);

                var address = doc.GetDictionary("address");
                address.Should().NotBeNull();
                var street = $"{i} street.";
                address.SetString("street", street);

                var phones = doc.GetArray("phones");
                phones.Should().NotBeNull();
                phones.Count.Should().Be(2);
                var phone = $"650-000-{i:D4}";
                phones.SetString(0, phone);

                doc.SetDate("updated", DateTimeOffset.UtcNow);

                Db.Save(doc).Dispose();
            }
        }