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 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"); }
protected override DictionaryObject DoPrediction(DictionaryObject input) { var blob = input.GetBlob("text"); if (blob == null) { return(null); } ContentType = blob.ContentType; var text = Encoding.UTF8.GetString(blob.Content); var wc = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length; var sc = text.Split('.', StringSplitOptions.RemoveEmptyEntries).Length; var output = new MutableDictionaryObject(); output.SetInt("wc", wc); output.SetInt("sc", sc); return(output); }
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 async Task <bool> AddItemAsync(SeasonalItem item) { using (var doc = _db.GetDocument(CoreApp.DocId)) using (var mdoc = doc.ToMutable()) using (MutableArrayObject listItems = mdoc.GetArray(CoreApp.ArrKey)) { var dictObj = new MutableDictionaryObject(); dictObj.SetString("key", item.Name); dictObj.SetInt("value", item.Quantity); var blob = new Blob("image/png", item.ImageByteArray); dictObj.SetBlob("image", blob); listItems.AddDictionary(dictObj); _db.Save(mdoc); _items.Add(_items.Count + 1, item); } return(await Task.FromResult(true)); }
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"); }