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 TestGetDictionary()
        {
            var mNestedDict = new MutableDictionaryObject();

            mNestedDict.SetLong("key1", 1L);
            mNestedDict.SetString("key2", "Hello");
            mNestedDict.SetValue("key3", null);

            var mDict = new MutableDictionaryObject();

            mDict.SetLong("key1", 1L);
            mDict.SetString("key2", "Hello");
            mDict.SetValue("key3", null);
            mDict.SetDictionary("nestedDict", mNestedDict);

            using (var mDoc = new MutableDocument("test")) {
                mDoc.SetDictionary("dict", mDict);

                using (var doc = Db.Save(mDoc)) {
                    var dict = doc.GetDictionary("dict");
                    dict.Should().NotBeNull();
                    dict.GetDictionary("not-exists").Should().BeNull();
                    var nestedDict = dict.GetDictionary("nestedDict");
                    nestedDict.Should().NotBeNull();
                    nestedDict.ToDictionary().ShouldBeEquivalentTo(mNestedDict.ToDictionary());
                }
            }
        }
        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");
        }
 public void TestSetNull()
 {
     using (var mDoc = new MutableDocument("test")) {
         var mDict = new MutableDictionaryObject();
         mDict.SetValue("obj-null", null);
         mDict.SetString("string-null", null);
         mDict.SetArray("array-null", null);
         mDict.SetDictionary("dict-null", null);
         mDoc.SetDictionary("dict", mDict);
         SaveDocument(mDoc, doc =>
         {
             doc.Count.Should().Be(1);
             doc.Contains("dict").Should().BeTrue();
             var d = doc.GetDictionary("dict");
             d.Should().NotBeNull();
             d.Count.Should().Be(4);
             d.Contains("obj-null").Should().BeTrue();
             d.Contains("string-null").Should().BeTrue();
             d.Contains("array-null").Should().BeTrue();
             d.Contains("dict-null").Should().BeTrue();
             d.GetValue("obj-null").Should().BeNull();;
             d.GetValue("string-null").Should().BeNull();;
             d.GetValue("array-null").Should().BeNull();;
             d.GetValue("dict-null").Should().BeNull();
         });
     }
 }
        private MutableDocument CreateDocumentWithTag(string id, string tag)
        {
            var doc = new MutableDocument(id);

            doc.SetString("tag", tag);

            doc.SetString("firstName", "Daniel");
            doc.SetString("lastName", "Tiger");

            var address = new MutableDictionary();

            address.SetString("street", "1 Main street");
            address.SetString("city", "Mountain View");
            address.SetString("state", "CA");
            doc.SetDictionary("address", address);

            var phones = new MutableArray();

            phones.AddString("650-123-0001").AddString("650-123-0002");
            doc.SetArray("phones", phones);

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

            return(doc);
        }
Exemple #7
0
        public void TestDictionaryWithULong()
        {
            using (var doc = new MutableDocument("test_ulong")) {
                var dict = new MutableDictionaryObject();
                dict.SetValue("high_value", UInt64.MaxValue);
                doc.SetDictionary("nested", dict);
                Db.Save(doc);
            }

            using (var doc = Db.GetDocument("test_ulong")) {
                doc["nested"]["high_value"].Value.Should().Be(UInt64.MaxValue);
            }
        }
        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 TestEnumeratingDictionary()
        {
            var dict = new MutableDictionaryObject();

            for (int i = 0; i < 20; i++)
            {
                dict.SetInt($"key{i}", i);
            }

            var content = dict.ToDictionary();
            var result  = new Dictionary <string, object>();

            foreach (var item in dict)
            {
                result[item.Key] = item.Value;
            }

            result.ShouldBeEquivalentTo(content, "because that is the correct content");
            content = dict.Remove("key2").SetInt("key20", 20).SetInt("key21", 21).ToDictionary();

            result = new Dictionary <string, object>();
            foreach (var item in dict)
            {
                result[item.Key] = item.Value;
            }

            result.ShouldBeEquivalentTo(content, "because that is the correct content");

            var doc = new MutableDocument("doc1");

            doc.SetDictionary("dict", dict);
            SaveDocument(doc, d =>
            {
                result      = new Dictionary <string, object>();
                var dictObj = d.GetDictionary("dict");
                foreach (var item in dictObj)
                {
                    result[item.Key] = item.Value;
                }

                result.ShouldBeEquivalentTo(content, "because that is the correct content");
            });
        }
        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);
        }
Exemple #11
0
        public void TestTypesInDictionaryToJSON()
        {
            var dic = PopulateDictData();
            var md  = new MutableDictionaryObject();

            foreach (var item in dic)
            {
                md.SetValue(item.Key, item.Value); // platform dictionary and list or array will be converted into Couchbase object in SetValue method
            }

            using (var doc = new MutableDocument("doc1")) {
                doc.SetDictionary("dict", md);
                Db.Save(doc);
            }

            using (var doc = Db.GetDocument("doc1")) {
                var dict = doc.GetDictionary("dict");
                var json = dict.ToJSON();
                ValidateToJsonValues(json, dic);
            }
        }
        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");
        }
        public void TestGetValueFromNewEmptyDictionary()
        {
            DictionaryObject dict = new MutableDictionaryObject();

            dict.GetInt("key").Should().Be(0, "because that is the default value");
            dict.GetLong("key").Should().Be(0L, "because that is the default value");
            dict.GetDouble("key").Should().Be(0.0, "because that is the default value");
            dict.GetBoolean("key").Should().Be(false, "because that is the default value");
            dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value");
            dict.GetBlob("key").Should().BeNull("because that is the default value");
            dict.GetValue("key").Should().BeNull("because that is the default value");
            dict.GetString("key").Should().BeNull("because that is the default value");
            dict.GetDictionary("key").Should().BeNull("because that is the default value");
            dict.GetArray("key").Should().BeNull("because that is the default value");
            dict.ToDictionary().Should().BeEmpty("because the dictionary is empty");

            var doc = new MutableDocument("doc1");

            doc.SetDictionary("dict", dict);

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

            dict = gotDoc.GetDictionary("dict");
            dict.GetInt("key").Should().Be(0, "because that is the default value");
            dict.GetLong("key").Should().Be(0L, "because that is the default value");
            dict.GetDouble("key").Should().Be(0.0, "because that is the default value");
            dict.GetBoolean("key").Should().Be(false, "because that is the default value");
            dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value");
            dict.GetBlob("key").Should().BeNull("because that is the default value");
            dict.GetValue("key").Should().BeNull("because that is the default value");
            dict.GetString("key").Should().BeNull("because that is the default value");
            dict.GetDictionary("key").Should().BeNull("because that is the default value");
            dict.GetArray("key").Should().BeNull("because that is the default value");
            dict.ToDictionary().Should().BeEmpty("because the dictionary is empty");
        }
Exemple #14
0
        public void TestBlobJsonStringInLayersOfMutableDict()
        {
            var blob = ArrayTestBlob();

            Db.SaveBlob(blob);
            var nestedBlob = new Blob("text/plain", Encoding.UTF8.GetBytes("abcde"));

            Db.SaveBlob(nestedBlob);
            var b1 = new Blob("text/plain", Encoding.UTF8.GetBytes("alpha"));

            Db.SaveBlob(b1);
            var b2 = new Blob("text/plain", Encoding.UTF8.GetBytes("beta"));

            Db.SaveBlob(b2);
            var b3 = new Blob("text/plain", Encoding.UTF8.GetBytes("omega"));

            Db.SaveBlob(b3);

            var KeyValueDictionary = new Dictionary <string, object>()
            {
                { "blob", blob },
                { "blobUnderDict", new Dictionary <string, object>()
                  {
                      { "nestedBlob", nestedBlob }
                  } },
                { "blobUnderArr", new List <object>()
                  {
                      b1, b2, b3
                  } }
            };

            var dicJson = JsonConvert.SerializeObject(KeyValueDictionary);
            var md      = new MutableDictionaryObject(dicJson);

            using (var mdoc = new MutableDocument("doc1")) {
                mdoc.SetDictionary("dict", md);
                Db.Save(mdoc);
            }

            var dic = Db.GetDocument("doc1").GetDictionary("dict");

            var blob1 = dic.GetBlob("blob");

            blob1.Content.Should().NotBeNull();
            blob1.Should().BeEquivalentTo(KeyValueDictionary["blob"]);

            var blob2 = dic.GetDictionary("blobUnderDict").GetBlob("nestedBlob");

            blob2.Content.Should().NotBeNull();
            var d = (Dictionary <string, object>)KeyValueDictionary["blobUnderDict"];

            blob2.Should().BeEquivalentTo(d["nestedBlob"]);

            var blobs    = dic.GetArray("blobUnderArr");
            var cnt      = blobs.Count;
            var blobList = (List <object>)KeyValueDictionary["blobUnderArr"];

            for (int i = 0; i < cnt; i++)
            {
                var b = blobs.GetBlob(i);
                b.Content.Should().NotBeNull();
                b.Should().BeEquivalentTo(blobList[i]);
            }
        }