def basic(): """Creates a basic :class:Passage to tinker with. Passage structure is as follows: Layer1: order by ID, heads = [1.2], all = [1.1, 1.2, 1.3] Layer2: order by node unique ID descending, heads = all = [2.2, 2.1], attrib={"test": True} Nodes (tag): 1.1 (1) 1.3 (3), attrib={"node": True} 1.2 (x), order by edge tag children: 1.3 Edge: tag=test1, attrib={"Edge": True} 1.1 Edge: tag=test2 2.1 (2), children [1.1, 1.2] with edge tags [test, test2] 2.2 (2), children [1.1, 1.2, 1.3] with tags [test, test1, test] """ p = core.Passage(ID="1") core.Layer(ID="1", root=p) core.Layer(ID="2", root=p, attrib={"test": True}, orderkey=lambda x: -1 * int(x.ID.split(".")[1])) # Order is explicitly different in order to break the alignment between # the ID/Edge ordering and the order of creation/addition node11 = core.Node(ID="1.1", root=p, tag="1") node13 = core.Node(ID="1.3", root=p, tag="3", attrib={"node": True}) node12 = core.Node(ID="1.2", root=p, tag="x", orderkey=operator.attrgetter("tag")) node21 = core.Node(ID="2.1", root=p, tag="2") node22 = core.Node(ID="2.2", root=p, tag="2") node12.add("test2", node11) node12.add("test1", node13, edge_attrib={"edge": True}) node21.add("test2", node12) node21.add("test", node11) node22.add("test1", node12) node22.add("test", node13) node22.add("test", node11) return p
def create_basic_passage(): """Creates a basic :class:Passage to tinker with. Passage structure is as follows: Layer1: order by ID, heads = [1.2], all = [1.1, 1.2, 1.3] Layer2: order by node unique ID descending, heads = all = [2.2, 2.1], attrib={'test': True} Nodes (tag): 1.1 (1) 1.3 (3), attrib={'node': True} 1.2 (x), order by edge tag children: 1.3 Edge: tag=test1, attrib={'Edge': True} 1.1 Edge: tag=test2 2.1 (2), children [1.1, 1.2] with edge tags [test, test2] 2.2 (2), children [1.1, 1.2, 1.3] with tags [test, test1, test] """ p = core.Passage(ID='1') core.Layer(ID='1', root=p) core.Layer(ID='2', root=p, attrib={'test': True}, orderkey=lambda x: -1 * int(x.ID.split('.')[1])) # Order is explicitly different in order to break the alignment between # the ID/Edge ordering and the order of creation/addition node11 = core.Node(ID='1.1', root=p, tag='1') node13 = core.Node(ID='1.3', root=p, tag='3', attrib={'node': True}) node12 = core.Node(ID='1.2', root=p, tag='x', orderkey=operator.attrgetter('tag')) node21 = core.Node(ID='2.1', root=p, tag='2') node22 = core.Node(ID='2.2', root=p, tag='2') node12.add('test2', node11) node12.add('test1', node13, edge_attrib={'edge': True}) node21.add('test2', node12) node21.add('test', node11) node22.add('test1', node12) node22.add('test', node13) node22.add('test', node11) return p
def test_modifying(self): p = TestUtil.create_basic_passage() l1, l2 = p.layer("1"), p.layer("2") node11, node12, node13 = l1.all node22, node21 = l2.all # Testing attribute changes p.attrib["passage"] = 1 self.assertDictEqual(p.attrib.copy(), {"passage": 1}) del l2.attrib["test"] self.assertDictEqual(l2.attrib.copy(), {}) node13.attrib[1] = 1 self.assertDictEqual(node13.attrib.copy(), {"node": True, 1: 1}) self.assertEqual(len(node13.attrib), 2) self.assertEqual(node13.attrib.get("node"), True) self.assertEqual(node13.attrib.get("missing"), None) # Testing Node changes node14 = core.Node(ID="1.4", root=p, tag="4") node15 = core.Node(ID="1.5", root=p, tag="5") self.assertSequenceEqual(l1.all, [node11, node12, node13, node14, node15]) self.assertSequenceEqual(l1.heads, [node12, node14, node15]) node15.add("test", node11) self.assertSequenceEqual(node11.parents, [node12, node15, node21, node22]) node21.remove(node12) node21.remove(node21[0]) self.assertEqual(len(node21), 0) self.assertSequenceEqual(node12.parents, [node22]) self.assertSequenceEqual(node11.parents, [node12, node15, node22]) node14.add("test", node15) self.assertSequenceEqual(l1.heads, [node12, node14]) node12.destroy() self.assertSequenceEqual(l1.heads, [node13, node14]) self.assertSequenceEqual(node22.children, [node11, node13]) node22.tag = "x" node22[0].tag = "testx" self.assertEqual(node22.tag, "x") self.assertEqual(node22[0].tag, "testx")
def test_modifying(self): p = TestUtil.create_basic_passage() l1, l2 = p.layer('1'), p.layer('2') node11, node12, node13 = l1.all node22, node21 = l2.all # Testing attribute changes p.attrib['passage'] = 1 self.assertDictEqual(p.attrib.copy(), {'passage': 1}) del l2.attrib['test'] self.assertDictEqual(l2.attrib.copy(), {}) node13.attrib[1] = 1 self.assertDictEqual(node13.attrib.copy(), {'node': True, 1: 1}) self.assertEqual(len(node13.attrib), 2) self.assertEqual(node13.attrib.get('node'), True) self.assertEqual(node13.attrib.get('missing'), None) # Testing Node changes node14 = core.Node(ID='1.4', root=p, tag='4') node15 = core.Node(ID='1.5', root=p, tag='5') self.assertSequenceEqual(l1.all, [node11, node12, node13, node14, node15]) self.assertSequenceEqual(l1.heads, [node12, node14, node15]) node15.add('test', node11) self.assertSequenceEqual(node11.parents, [node12, node15, node21, node22]) node21.remove(node12) node21.remove(node21[0]) self.assertEqual(len(node21), 0) self.assertSequenceEqual(node12.parents, [node22]) self.assertSequenceEqual(node11.parents, [node12, node15, node22]) node14.add('test', node15) self.assertSequenceEqual(l1.heads, [node12, node14]) node12.destroy() self.assertSequenceEqual(l1.heads, [node13, node14]) self.assertSequenceEqual(node22.children, [node11, node13]) node22.tag = 'x' node22[0].tag = 'testx' self.assertEqual(node22.tag, 'x') self.assertEqual(node22[0].tag, 'testx')
def test_modifying(): p = basic() l1, l2 = p.layer("1"), p.layer("2") node11, node12, node13 = l1.all node22, node21 = l2.all # Testing attribute changes p.attrib["passage"] = 1 assert p.attrib.copy() == {"passage": 1} del l2.attrib["test"] assert l2.attrib.copy() == {} node13.attrib[1] = 1 assert node13.attrib.copy() == {"node": True, 1: 1} assert len(node13.attrib) == 2 assert node13.attrib.get("node") assert node13.attrib.get("missing") is None # Testing Node changes node14 = core.Node(ID="1.4", root=p, tag="4") node15 = core.Node(ID="1.5", root=p, tag="5") assert l1.all == [node11, node12, node13, node14, node15] assert l1.heads == [node12, node14, node15] node15.add("test", node11) assert node11.parents == [node12, node15, node21, node22] node21.remove(node12) node21.remove(node21[0]) assert len(node21) == 0 assert node12.parents == [node22] assert node11.parents == [node12, node15, node22] node14.add("test", node15) assert l1.heads == [node12, node14] node12.destroy() assert l1.heads == [node13, node14] assert node22.children == [node11, node13] node22.tag = "x" node22[0].tag = "testx" assert node22.tag == "x" assert node22[0].tag == "testx"