public void LiteDbMutableNode_fails_on_removing_if_node_has_child_nodes() { // ARRANGE var child = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "a"); var secondChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "b"); var node = new LiteDbMutableNode <int>(this.nodes, this.rootDocument).AddChild(child).AddChild(secondChild); var grandChild = new LiteDbMutableNode <int>(this.nodes, new BsonDocument(), "c"); child.AddChild(grandChild); // ACT var result = node.RemoveAllChildNodes(recurse: false); // ASSERT // node has a child now Assert.False(result); Assert.True(node.HasChildNodes); // check db // all nodes are still there Assert.Equal(4, this.nodes.FindAll().Count()); }
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)); }
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); }
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); }