1 import unittest 2 from unittest import TestCase 3 from metadata_model import * 4 5 class TestInnerNamespace(TestCase): 6 def test_combine_children_by_name(self): 7 # 8 # Set up 9 # 10 kind = Kind("some_root_kind", parent=None) 11 ins_outer = InnerNamespace("static", parent=kind) 12 kind._namespaces = [ins_outer] 13 14 ins1 = InnerNamespace("ins1", parent=ins_outer) 15 ins1a = InnerNamespace("ins1", parent=ins_outer) # same name deliberately 16 entry1 = Entry(name="entry1", type="int32", kind="static", 17 parent=ins1) 18 entry2 = Entry(name="entry2", type="int32", kind="static", 19 parent=ins1a) 20 entry3 = Entry(name="entry3", type="int32", kind="static", 21 parent=ins_outer) 22 23 ins_outer._namespaces = [ins1, ins1a] 24 ins_outer._entries = [entry3] 25 26 ins1._entries = [entry1] 27 ins1a._entries = [entry2] 28 29 # 30 # Test 31 # 32 combined_children_namespace = ins_outer.combine_children_by_name() 33 34 self.assertIsInstance(combined_children_namespace, InnerNamespace) 35 combined_ins = [i for i in combined_children_namespace.namespaces] 36 combined_ent = [i for i in combined_children_namespace.entries] 37 38 self.assertEquals(kind, combined_children_namespace.parent) 39 self.assertEquals(1, len(combined_ins)) 40 self.assertEquals(1, len(combined_ent)) 41 42 self.assertEquals("ins1", combined_ins[0].name) 43 self.assertEquals("entry3", combined_ent[0].name) 44 45 new_ins = combined_ins[0] 46 self.assertIn(entry1, new_ins.entries) 47 self.assertIn(entry2, new_ins.entries) 48 49 50 class TestKind(TestCase): 51 def test_combine_kinds_into_single_node(self): 52 # 53 # Set up 54 # 55 section = Section("some_section", parent=None) 56 kind_static = Kind("static", parent=section) 57 kind_dynamic = Kind("dynamic", parent=section) 58 section._kinds = [kind_static, kind_dynamic] 59 60 ins1 = InnerNamespace("ins1", parent=kind_static) 61 ins2 = InnerNamespace("ins2", parent=kind_dynamic) 62 entry1 = Entry(name="entry1", type="int32", kind="static", 63 parent=kind_static) 64 entry2 = Entry(name="entry2", type="int32", kind="static", 65 parent=kind_dynamic) 66 67 kind_static._namespaces = [ins1] 68 kind_static._entries = [entry1] 69 70 kind_dynamic._namespaces = [ins2] 71 kind_dynamic._entries = [entry2] 72 73 # 74 # Test 75 # 76 combined_kind = section.combine_kinds_into_single_node() 77 78 self.assertEquals(section, combined_kind.parent) 79 80 self.assertIn(ins1, combined_kind.namespaces) 81 self.assertIn(ins2, combined_kind.namespaces) 82 83 self.assertIn(entry1, combined_kind.entries) 84 self.assertIn(entry2, combined_kind.entries) 85 86 def test_combine_children_by_name(self): 87 # 88 # Set up 89 # 90 section = Section("some_section", parent=None) 91 kind_static = Kind("static", parent=section) 92 section._kinds = [kind_static] 93 94 ins1 = InnerNamespace("ins1", parent=kind_static) 95 ins1a = InnerNamespace("ins1", parent=kind_static) # same name deliberately 96 entry1 = Entry(name="entry1", type="int32", kind="static", 97 parent=ins1) 98 entry2 = Entry(name="entry2", type="int32", kind="static", 99 parent=ins1a) 100 entry3 = Entry(name="entry3", type="int32", kind="static", 101 parent=kind_static) 102 103 kind_static._namespaces = [ins1, ins1a] 104 kind_static._entries = [entry3] 105 106 ins1._entries = [entry1] 107 ins1a._entries = [entry2] 108 109 # 110 # Test 111 # 112 combined_children_kind = kind_static.combine_children_by_name() 113 114 self.assertIsInstance(combined_children_kind, Kind) 115 combined_ins = [i for i in combined_children_kind.namespaces] 116 combined_ent = [i for i in combined_children_kind.entries] 117 118 self.assertEquals(section, combined_children_kind.parent) 119 self.assertEquals(1, len(combined_ins)) 120 self.assertEquals(1, len(combined_ent)) 121 122 self.assertEquals("ins1", combined_ins[0].name) 123 self.assertEquals("entry3", combined_ent[0].name) 124 125 new_ins = combined_ins[0] 126 self.assertIn(entry1, new_ins.entries) 127 self.assertIn(entry2, new_ins.entries) 128 129 if __name__ == '__main__': 130 unittest.main() 131