示例#1
0
        public void LiteDbMutableNode_removes_child_from_current_instance()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");
            var node  = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child);

            // ACT

            var result = node.RemoveChild(child);

            // ASSERT

            Assert.NotNull(result);
            Assert.False(result.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.False(found);
            Assert.False(result.ChildNodes.Any());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(0, childNodesDoc.AsDocument.Count);
            Assert.False(childNodesDoc.AsDocument.TryGetValue("a", out var childDocId));
            Assert.False(this.nodes.Find(Query.EQ("key", "a")).Any());
        }
示例#2
0
        public void LiteDbMutableNode_fails_on_adding_child_without_key()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument());
            var node  = new LiteDbMutableNode <int>(this.nodes, this.rootDocument);

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.AddChild(child));

            // ASSERT

            Assert.Equal("Child node must have a key", result.Message);

            // node is unchanged

            Assert.False(node.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.False(found);
            Assert.False(node.ChildNodes.Any());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.False(rootDoc.TryGetValue("cn", out var childNodesDoc));
        }
示例#3
0
        public void LiteDbMutableNode_fails_on_replacing_unknown_child()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            child.SetValue(1);
            var node        = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child);
            var secondChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            secondChild.SetValue(2);
            var thirdChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            thirdChild.SetValue(3);

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.ReplaceChild(thirdChild, secondChild));

            // ASSERT

            Assert.Equal("The node (id='a') doesn't replace any of the existing child nodes", result.Message);
            Assert.True(node.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.True(found);
            Assert.Equal(child, addedChild);
            Assert.Equal(child, node.ChildNodes.Single());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(1, childNodesDoc.AsDocument.Count);
            Assert.True(childNodesDoc.AsDocument.TryGetValue("a", out var childDocId));

            var childDoc = this.nodes.FindById(childDocId);

            Assert.NotNull(childDoc);
            Assert.True(childDoc.TryGetValue("key", out var childKey));
            Assert.Equal("a", childKey.AsString);
            Assert.True(childDoc.TryGetValue("value", out var childValue));
            Assert.Equal(1, childValue.AsInt32);
            Assert.Equal(child.BsonDocument.Get("_id").AsObjectId, childDoc.Get("_id").AsObjectId);
            Assert.NotEqual(secondChild.BsonDocument.Get("_id").AsObjectId, childDoc.Get("_id").AsObjectId);
            Assert.NotEqual(thirdChild.BsonDocument.Get("_id").AsObjectId, childDoc.Get("_id").AsObjectId);
        }
示例#4
0
        public void LiteDbMutableNode_fails_on_Replace_if_keys_are_different()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            child.SetValue(1);
            var node        = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child);
            var secondChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "b");

            secondChild.SetValue(2);

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.ReplaceChild(child, secondChild));

            // ASSERT

            Assert.Equal("Key of child to replace (key='a') and new child (key='b') must be equal", result.Message);
            Assert.True(node.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.True(found);
            Assert.Equal(child, addedChild);
            Assert.Equal(child, node.ChildNodes.Single());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(1, childNodesDoc.AsDocument.Count);
            Assert.True(childNodesDoc.AsDocument.TryGetValue("a", out var childDocId));

            var childDoc = this.nodes.FindById(childDocId);

            Assert.NotNull(childDoc);
            Assert.True(childDoc.TryGetValue("key", out var childKey));
            Assert.Equal("a", childKey.AsString);
            Assert.True(childDoc.TryGetValue("value", out var childValue));
            Assert.Equal(1, childValue.AsInt32);
            Assert.Equal(child.BsonDocument.Get("_id").AsObjectId, childDoc.Get("_id").AsObjectId);
            Assert.NotEqual(secondChild.BsonDocument.Get("_id").AsObjectId, childDoc.Get("_id").AsObjectId);
        }
示例#5
0
        public void LiteDbMutableNode_fails_on_adding_same_key_twice()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            child.TryGetId(out var childId);
            var node        = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child);
            var secondChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");

            // ACT

            var result = Assert.Throws <InvalidOperationException>(() => node.AddChild(secondChild));

            // ASSERT

            Assert.Equal($"Node contains child node(id='{childId}') with same key='a'", result.Message);

            // node is unchanged

            Assert.True(node.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.True(found);
            Assert.Equal(child, addedChild);
            Assert.Equal(child, node.ChildNodes.Single());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(1, childNodesDoc.AsDocument.Count);
            Assert.True(childNodesDoc.AsDocument.TryGetValue("a", out var childDocId));

            var childDoc = this.nodes.FindById(childDocId);

            Assert.NotNull(childDoc);
            Assert.True(childDoc.TryGetValue("key", out var childKey));
            Assert.Equal("a", childKey.AsString);
        }
示例#6
0
        public void LiteDbMutableNode_removes_all_childnodes_recursivly()
        {
            // ARRANGE
            //           <node>
            //           /   \
            //          a     b
            //         /
            //        c

            var grandChild  = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "c");
            var child       = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a").AddChild(grandChild);
            var secondChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "b");
            var node        = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child).AddChild(secondChild);

            // ACT

            var result = node.RemoveAllChildNodes(recurse: true);

            // ASSERT
            // descendants have been removed

            Assert.True(result);
            Assert.False(node.HasChildNodes);

            var(found, addedChild) = node.TryGetChildNode("a");

            Assert.False(found);
            Assert.False(node.ChildNodes.Any());

            // check db
            // only the node is there

            var rootDoc = this.nodes.FindAll().Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(0, childNodesDoc.AsDocument.Count);
        }
示例#7
0
        public void LiteDbMutableNode_adds_child_to_current_instance()
        {
            // ARRANGE

            var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a");
            var node  = new LiteDbMutableNode <int>(this.nodes, this.rootDocument);

            // ACT

            var result = node.AddChild(child);

            // ASSERT
            // node has a child now

            Assert.Same(node, result);

            Assert.True(node.HasChildNodes);
            var(found, addedChild) = node.TryGetChildNode("a");
            Assert.True(found);
            Assert.Equal(child, addedChild);
            Assert.Equal(child, node.ChildNodes.Single());

            // check db

            var rootDoc = this.nodes.Find(Query.EQ("key", null)).Single();

            Assert.NotNull(rootDoc);
            Assert.True(rootDoc.TryGetValue("cn", out var childNodesDoc));
            Assert.Equal(1, childNodesDoc.AsDocument.Count);
            Assert.True(childNodesDoc.AsDocument.TryGetValue("a", out var childDocId));

            var childDoc = this.nodes.FindById(childDocId);

            Assert.NotNull(childDoc);
            Assert.True(childDoc.TryGetValue("key", out var childKey));
            Assert.Equal("a", childKey.AsString);
        }