def test_data_object_dict_can_insert_to_doc(self): '''Validate DataObjectDict can be inserted as child of DataObject''' data_obj = SimpleDataObject("test_obj") data_dict = {'key1': 'value1', 'key2': 'value2'} data_dict_obj = DataObjectDict("TestChild", data_dict) data_obj.insert_children(data_dict_obj)
class TestDataObjectCacheXmlSupport(unittest.TestCase): '''Tests to test DOC XML specific methods''' def setUp(self): '''Create DOC, and empty registry of classes, some children and refs''' # Hack to ensure that registry is empty before we use it, self.orig_registry = DOC._CACHE_CLASS_REGISTRY DOC._CACHE_CLASS_REGISTRY = dict() DataObjectCache.register_class([SimpleDataObject, SimpleDataObject2, SimpleDataObject3, SimpleDataObjectHandlesChildren]) # Create a tree that looks like: # # root # child_1 # child_1_1 # child_1_2 # child_2 # child_2_1 # child_2_1_1 # child_2_1_1_1 # child_2_1_1_2 # child_3 # child_3_1 # child_3_1_2 # child_3_1_2 # child_4 # child_5 # child_5_1 # child_5_2 # child_5_2_1 # child_5_2_2 # child_5_2_3 # child_5_2_3_1 # child_5_2_3_2 # child_5_2_3_3 # child_5_same_name # Create root node self.doc = DataObjectCache() # Create some persistent content self.persistent_root = SimpleDataObject("persistent_root") # Add some children, used by most tests. self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject3("child_5") self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.persistent_root.insert_children(self.do_list) self.doc.persistent.insert_children(self.persistent_root) # Now let's add the children of children, etc. for use by # get_descendants() tests. # child_1 children self.child_1_1 = SimpleDataObject("child_1_1") self.child_1_2 = SimpleDataObject("child_1_2") self.child_1.insert_children([self.child_1_1, self.child_1_2]) # child_2 tree self.child_2_1 = SimpleDataObject2("child_2_1") self.child_2.insert_children(self.child_2_1) self.child_2_1_1 = SimpleDataObject2("child_2_1_1") self.child_2_1.insert_children(self.child_2_1_1) self.child_2_1_1_1 = SimpleDataObject2("child_2_1_1_1") self.child_2_1_1_2 = SimpleDataObject2("child_2_1_1_2") self.child_2_1_1.insert_children([self.child_2_1_1_1, self.child_2_1_1_2]) # child_3 tree self.child_3_1 = SimpleDataObject("child_3_1") self.child_3.insert_children(self.child_3_1) self.child_3_1_1 = SimpleDataObject("child_3_1_1") self.child_3_1_2 = SimpleDataObject("child_3_1_2") self.child_3_1_2_same_name = SimpleDataObject("child_3_1_2") self.child_3_1.insert_children([self.child_3_1_1, self.child_3_1_2, self.child_3_1_2_same_name]) # child_5 tree self.child_5_1 = SimpleDataObject("child_5_1") self.child_5_2 = SimpleDataObject("child_5_2") self.child_5.insert_children([self.child_5_1, self.child_5_2]) self.child_5_2_1 = SimpleDataObject("child_5_2_1") self.child_5_2_2 = SimpleDataObject("child_5_2_2") self.child_5_2_3 = SimpleDataObjectHandlesChildren("child_5_2_3") self.child_5_2.insert_children([self.child_5_2_1, self.child_5_2_2, self.child_5_2_3]) self.child_5_2_3_1 = SimpleDataObject("child_5_2_3_1") self.child_5_2_3_2 = SimpleDataObject("child_5_2_3_2") self.child_5_2_3_3 = SimpleDataObject("child_5_2_3_3") self.child_5_2_3_3_same_name = SimpleDataObject("child_5_2_3_3") self.child_5_2_3.insert_children([self.child_5_2_3_1, self.child_5_2_3_2, self.child_5_2_3_3, self.child_5_2_3_3_same_name]) # Create some volatile content, not much, just enough to test that it's # not overwritten on loading of snapshot. self.volatile_root = SimpleDataObject2("volatile_root") self.doc.volatile.insert_children(self.volatile_root) def tearDown(self): '''Restore class registry and clear DOC and other references''' # Hack to ensure that registry is restored after we use it. DOC._CACHE_CLASS_REGISTRY = self.orig_registry self.doc.clear() self.doc = None self.persistent_root = None self.volatile_root = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.do_list = None self.child_1_1 = None self.child_1_2 = None self.child_2_1 = None self.child_2_1_1 = None self.child_2_1_1_1 = None self.child_2_1_1_2 = None self.child_3_1 = None self.child_3_1_1 = None self.child_3_1_2 = None self.child_3_1_2_same_name = None self.child_5_1 = None self.child_5_2 = None self.child_5_2_1 = None self.child_5_2_2 = None self.child_5_2_3 = None self.child_5_2_3_1 = None self.child_5_2_3_2 = None self.child_5_2_3_3 = None self.child_5_2_3_3_same_name = None def test_data_object_cache_xml_support_skip_sub_tree_elements(self): '''Validate no XML generated by volatile and persistent''' # doc.volatile and doc.persistent shouldn't generate their own # XML, so ensure tha this is the case. xml_tree = self.doc.generate_xml_manifest() child_names = [] for xml_child in xml_tree: child_names.append(xml_child.get("name")) self.assertEqual(child_names, \ [self.persistent_root.name, self.volatile_root.name]) def test_data_object_cache_xml_support_generates_expected_xml(self): '''Validate that expected XML is generated by generate_xml_manifest''' indentation = '''\ ''' expected_xml = '''\ <root> ..<SimpleDataObject name="persistent_root"> ....<SimpleDataObject2 name="child_1"> ......<SimpleDataObject name="child_1_1"/> ......<SimpleDataObject name="child_1_2"/> ....</SimpleDataObject2> ....<SimpleDataObject name="child_2"> ......<SimpleDataObject2 name="child_2_1"> ........<SimpleDataObject2 name="child_2_1_1"> ..........<SimpleDataObject2 name="child_2_1_1_1"/> ..........<SimpleDataObject2 name="child_2_1_1_2"/> ........</SimpleDataObject2> ......</SimpleDataObject2> ....</SimpleDataObject> ....<SimpleDataObject name="child_3"> ......<SimpleDataObject name="child_3_1"> ........<SimpleDataObject name="child_3_1_1"/> ........<SimpleDataObject name="child_3_1_2"/> ........<SimpleDataObject name="child_3_1_2"/> ......</SimpleDataObject> ....</SimpleDataObject> ....<SimpleDataObject2 name="child_4"/> ....<SimpleDataObject name="child_5_1"/> ....<SimpleDataObject name="child_5_2"> ......<SimpleDataObject name="child_5_2_1"/> ......<SimpleDataObject name="child_5_2_2"/> ......<SimpleDataObjectHandlesChildren name="child_5_2_3"> ........<so_child name="child_5_2_3_1"/> ........<so_child name="child_5_2_3_2"/> ........<so_child name="child_5_2_3_3"/> ........<so_child name="child_5_2_3_3"/> ......</SimpleDataObjectHandlesChildren> ....</SimpleDataObject> ..</SimpleDataObject> ..<SimpleDataObject2 name="volatile_root"/> </root> '''.replace(indentation, "").replace(".", " ") xml_str = self.doc.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "Resulting XML doesn't match expected (len=%d != %d):\ \nGOT:\n%s\nEXPECTED:\n%s\n" % (len(xml_str), len(expected_xml), xml_str, expected_xml)) def test_data_object_cache_xml_support_children_from_xml_volatile(self): '''Validate import_from_manifest_xml volatile flag''' # Get original XML tree orig_xml_tree = self.doc.generate_xml_manifest() orig_xml_str = self.doc.get_xml_tree_str() # Empty the DOC self.doc.clear() self.assertTrue(self.doc.is_empty) # Now, try to re-create DOC from oricinal XML self.doc.import_from_manifest_xml(orig_xml_tree, volatile=True) self.assertTrue(self.doc.volatile.has_children) self.assertFalse(self.doc.persistent.has_children) imported_xml_str = self.doc.get_xml_tree_str() self.assertEqual(imported_xml_str, orig_xml_str, "Resulting XML doesn't match expected (len=%d != %d):\ \nGOT:\n%s\nEXPECTED:\n%s\n" % (len(imported_xml_str), len(orig_xml_str), imported_xml_str, orig_xml_str)) def test_data_object_cache_xml_support_children_from_xml_persistent(self): '''Validate default XML import into persistent tree''' # Get original XML tree orig_xml_tree = self.doc.generate_xml_manifest() orig_xml_str = self.doc.get_xml_tree_str() # Empty the DOC self.doc.clear() self.assertTrue(self.doc.is_empty) # Now, try to re-create DOC from oricinal XML self.doc.import_from_manifest_xml(orig_xml_tree) # Ensure it's in the correct tree self.assertFalse(self.doc.volatile.has_children) self.assertTrue(self.doc.persistent.has_children) imported_xml_str = self.doc.get_xml_tree_str() self.assertEqual(imported_xml_str, orig_xml_str, "Resulting XML doesn't match expected (len=%d != %d):\ \nGOT:\n%s\nEXPECTED:\n%s\n" % (len(imported_xml_str), len(orig_xml_str), imported_xml_str, orig_xml_str)) def test_data_object_cache_xml_methods(self): '''Validate correct values returned from XML methods''' self.assertNotEqual(self.doc.to_xml(), None) self.assertEqual(self.doc.persistent.to_xml(), None) self.assertEqual(self.doc.volatile.to_xml(), None) self.assertFalse(self.doc.can_handle(None)) self.assertFalse(self.doc.from_xml(None)) self.assertFalse(self.doc.persistent.can_handle(None)) self.assertFalse(self.doc.persistent.from_xml(None)) self.assertFalse(self.doc.volatile.can_handle(None)) self.assertFalse(self.doc.volatile.from_xml(None))
class TestDataObjectCacheSnapshots(unittest.TestCase): '''Tests to validate DOC snapshots support''' def setUp(self): '''Set up correct environment for tests. Creates reference to a temp dir and file and StringIO buffer. Creates a tree of elements in the DOC to validate before/after. ''' # Create temporary work directory self.temp_dir = mkdtemp(prefix="doc_test-") self.temp_file = mktemp(prefix="snapshot-", dir=self.temp_dir) # Create StringIO memory buffer for I/O test self.buffer = StringIO() # Create a tree that looks like: # DOC # volatile # volatile_root # persistent # persistent_root # child_1 # child_1_1 # child_1_2 # child_2 # child_2_1 # child_2_1_1 # child_2_1_1_1 # child_2_1_1_2 # child_3 # child_3_1 # child_3_1_2 # child_3_1_2 # child_3_1_2_same_name # child_4 # child_5 # child_5_1 # child_5_2 # child_5_2_1 # child_5_2_2 # child_5_2_3 # child_5_2_3_1 # child_5_2_3_2 # child_5_2_3_3 # child_5_2_3_3_same_name # child_5_same_name # Create DOC node self.doc = DataObjectCache() # Create some persistent content self.persistent_root = SimpleDataObject("persistent_root") # Add some children, used by most tests. self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject3("child_5") self.child_5_same_name = SimpleDataObject("child_5") self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.do_list.append(self.child_5_same_name) self.persistent_root.insert_children(self.do_list) self.doc.persistent.insert_children(self.persistent_root) # Now let's add the children of children, etc. for use by # get_descendants() tests. # child_1 children self.child_1_1 = SimpleDataObject("child_1_1") self.child_1_2 = SimpleDataObject("child_1_2") self.child_1.insert_children([self.child_1_1, self.child_1_2]) # child_2 tree self.child_2_1 = SimpleDataObject2("child_2_1") self.child_2.insert_children(self.child_2_1) self.child_2_1_1 = SimpleDataObject2("child_2_1_1") self.child_2_1.insert_children(self.child_2_1_1) self.child_2_1_1_1 = SimpleDataObject2("child_2_1_1_1") self.child_2_1_1_2 = SimpleDataObject2("child_2_1_1_2") self.child_2_1_1.insert_children( [self.child_2_1_1_1, self.child_2_1_1_2]) # child_3 tree self.child_3_1 = SimpleDataObject("child_3_1") self.child_3.insert_children(self.child_3_1) self.child_3_1_1 = SimpleDataObject("child_3_1_1") self.child_3_1_2 = SimpleDataObject("child_3_1_2") self.child_3_1_2_same_name = SimpleDataObject("child_3_1_2") self.child_3_1.insert_children([self.child_3_1_1, self.child_3_1_2, self.child_3_1_2_same_name]) # child_5 tree self.child_5_1 = SimpleDataObject("child_5_1") self.child_5_2 = SimpleDataObject("child_5_2") self.child_5.insert_children([self.child_5_1, self.child_5_2]) self.child_5_2_1 = SimpleDataObject("child_5_2_1") self.child_5_2_2 = SimpleDataObject("child_5_2_2") self.child_5_2_3 = SimpleDataObject4("child_5_2_3") self.child_5_2.insert_children( [self.child_5_2_1, self.child_5_2_2, self.child_5_2_3]) self.child_5_2_3_1 = SimpleDataObject("child_5_2_3_1") self.child_5_2_3_2 = SimpleDataObject("child_5_2_3_2") self.child_5_2_3_3 = SimpleDataObject("child_5_2_3_3") self.child_5_2_3_3_same_name = SimpleDataObject("child_5_2_3_3") self.child_5_2_3.insert_children( [self.child_5_2_3_1, self.child_5_2_3_2, self.child_5_2_3_3, self.child_5_2_3_3_same_name]) # Create some volatile content, not much, just enough to test that it's # not overwritten on loading of snapshot. self.volatile_root = SimpleDataObject2("volatile_root") self.doc.volatile.insert_children(self.volatile_root) def tearDown(self): '''Cleanup test environment and references.''' # Remove temp dir, may not always be there. if path.exists(self.temp_file): unlink(self.temp_file) if path.exists(self.temp_dir): rmdir(self.temp_dir) # Remove buffer self.buffer.close() # Free's memory self.buffer = None # Unset variables. self.doc.clear() self.doc = None self.volatile_root = None self.persistent_root = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.child_5_same_name = None self.do_list = None self.child_1_1 = None self.child_1_2 = None self.child_2_1 = None self.child_2_1_1 = None self.child_2_1_1_1 = None self.child_2_1_1_2 = None self.child_3_1 = None self.child_3_1_1 = None self.child_3_1_2 = None self.child_3_1_2_same_name = None self.child_5_1 = None self.child_5_2 = None self.child_5_2_1 = None self.child_5_2_2 = None self.child_5_2_3 = None self.child_5_2_3_1 = None self.child_5_2_3_2 = None self.child_5_2_3_3 = None self.child_5_2_3_3_same_name = None def test_data_object_cache_snapshots_write_file_string(self): '''Validate writing of a snapshot to a file''' try: self.doc.take_snapshot(self.temp_file) except Exception, e: self.fail("Got unexpected error writing snapshot: " + str(e)) try: stat_info = stat(self.temp_file) self.assertFalse(stat_info.st_size < 2048, "Snapshot file size is too small: %d" % (stat_info.st_size)) except Exception, e: self.fail("Got unexpected error stat-ing snapshot file: " + str(e))
class TestDataObjectInsertion(unittest.TestCase): '''Tests for DataObject insertion methods''' def setUp(self): '''Create simple data object reference''' self.data_obj = SimpleDataObject("root") def tearDown(self): '''Clean up data_obj reference''' self.data_obj = None def test_insert_children_default_single(self): '''Validate insertion of single child''' new_do = SimpleDataObject("child_1") self.data_obj.insert_children(new_do) self.assertEqual(self.data_obj.children[0], new_do) def test_insert_children_default_fail_both_before_and_after(self): '''Validate failure on insert with invalid before and after''' new_do = SimpleDataObject("child_1") self.assertRaises(ValueError, self.data_obj.insert_children, new_do, new_do, new_do) def test_insert_children_default_fail_single(self): '''Validate failure on insert with non-DataObjectBase sub-class''' new_obj = object() self.assertRaises(TypeError, self.data_obj.insert_children, new_obj) def test_insert_children_default_fail_single_null_param(self): '''Validate failure on insert of None value''' self.assertRaises(TypeError, self.data_obj.insert_children, None) def test_insert_children_default_list(self): '''Validate insert of list of children''' new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) i = 0 for child in self.data_obj.children: self.assertEqual(child, new_do_list[i]) i += 1 self.assertEqual(i, len(new_do_list)) def test_insert_children_default_fail_list(self): '''Validate failure of insert of list of children with bad element''' new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(object()) self.assertRaises(TypeError, self.data_obj.insert_children, new_do_list) def test_insert_children_default_fail_tuple(self): '''Validate failure of insert of tuple of children with bad element''' # Create tuple new_do_list = (SimpleDataObject("child_1"), SimpleDataObject("child_2"), SimpleDataObject("child_3"), SimpleDataObject("child_4"), object()) self.assertRaises(TypeError, self.data_obj.insert_children, new_do_list) def test_insert_children_default_fail_invalid_type(self): '''Validate failure of insert of child with non-DataObjectBase''' self.assertRaises(TypeError, self.data_obj.insert_children, object()) def test_insert_children_before_single(self): '''Validate insertion of children list with before value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 new_do = SimpleDataObject("before_child_3") self.data_obj.insert_children(new_do, before=child_3) i = 0 for child in self.data_obj.children: if i == 2: self.assertEqual(child, new_do, str(self.data_obj)) break i += 1 def test_insert_children_before_doesnt_exist(self): '''Validate failure on insertion with non-existant 'before' obj''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) not_in_list = SimpleDataObject("child_not_in_list") new_do = SimpleDataObject("before_child_3") #Now for the real test, to insert something before non-existant child self.assertRaises(ObjectNotFoundError, self.data_obj.insert_children, new_do, before=not_in_list) def test_insert_children_after_doesnt_exist(self): '''Validate failure on insertion with non-existant 'after' obj''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) not_in_list = SimpleDataObject("child_not_in_list") new_do = SimpleDataObject("after_child_3") #Now for the real test, to insert something after non-existant child self.assertRaises(ObjectNotFoundError, self.data_obj.insert_children, new_do, after=not_in_list) def test_insert_children_before_first_single(self): '''Validate insertion of child with before == first child''' # Populate existing children first. new_do_list = list() child_1 = SimpleDataObject("child_1") new_do_list.append(child_1) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_1 new_do = SimpleDataObject("before_child_1") self.data_obj.insert_children(new_do, before=child_1) self.assertEqual(self.data_obj.children[0], new_do, str(self.data_obj)) def test_insert_children_after_single(self): '''Validate insertion of children with after value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 new_do = SimpleDataObject("after_child_5") self.data_obj.insert_children(new_do, after=child_3) i = 0 for child in self.data_obj.children: if i == 3: self.assertEqual(child, new_do, str(self.data_obj)) break i += 1 def test_insert_children_after_last_single(self): '''Validate insertion of children with after == last child''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) child_5 = SimpleDataObject("child_5") new_do_list.append(child_5) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_5 new_do = SimpleDataObject("after_child_5") self.data_obj.insert_children(new_do, after=child_5) children = self.data_obj.children self.assertEqual(children[len(children) - 1], new_do, str(self.data_obj)) def test_insert_children_before_list(self): '''Validate insertion of children with before value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 to_insert = list() to_insert.append(SimpleDataObject("before_child_3 - A")) to_insert.append(SimpleDataObject("before_child_3 - B")) to_insert.append(SimpleDataObject("before_child_3 - C")) self.data_obj.insert_children(to_insert, before=child_3) i = 0 j = 0 for child in self.data_obj.children: if i >= 2: self.assertEqual( child, to_insert[j], "child = %s ; compared_to = %s" % (child, to_insert[j])) j += 1 if j >= len(to_insert): break i += 1 def test_insert_children_before_first_list(self): '''Validate insertion of children with before == first child''' # Populate existing children first. new_do_list = list() child_1 = SimpleDataObject("child_1") new_do_list.append(child_1) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_1 to_insert = list() to_insert.append(SimpleDataObject("before_child_1 - A")) to_insert.append(SimpleDataObject("before_child_1 - B")) to_insert.append(SimpleDataObject("before_child_1 - C")) self.data_obj.insert_children(to_insert, before=child_1) i = 0 for child in self.data_obj.children: self.assertEqual( child, to_insert[i], "child = %s ; compared_to = %s" % (child, to_insert[i])) i += 1 if not (i < len(to_insert)): break def test_insert_children_after_list(self): '''Validate insertion of children list with after value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_3 to_insert = list() to_insert.append(SimpleDataObject("after_child_3 - A")) to_insert.append(SimpleDataObject("after_child_3 - B")) to_insert.append(SimpleDataObject("after_child_3 - C")) self.data_obj.insert_children(to_insert, after=child_3) i = 0 j = 0 for child in self.data_obj.children: if i >= 3: self.assertEqual( child, to_insert[j], "child = %s ; compared_to = %s" % (child, to_insert[j])) j += 1 if j >= len(to_insert): break i += 1 def test_insert_children_after_last_list(self): '''Validate insertion of children list with after == last child''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) child_5 = SimpleDataObject("child_5") new_do_list.append(child_5) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_5 to_insert = list() to_insert.append(SimpleDataObject("after_child_3 - A")) to_insert.append(SimpleDataObject("after_child_3 - B")) to_insert.append(SimpleDataObject("after_child_3 - C")) self.data_obj.insert_children(to_insert, after=child_5) children = self.data_obj.children num_children = len(children) offset = num_children - len(to_insert) for i in range(len(to_insert)): self.assertEqual( children[offset + i], to_insert[i], "child = %s ; compared_to = %s" % (children[offset + i], to_insert[i]))
class TestDataObjectXmlSupport(unittest.TestCase): '''Tests to ensure that DataObject XML support is working as expected''' def setUp(self): '''Create an XML tree for testing and references to them. The tree will look like the following: root child_1 child_1_1 child_1_2 child_2 child_2_1 child_2_1_1 child_2_1_1_1 child_2_1_1_2 child_3 child_3_1 child_3_1_2 child_3_1_2 child_3_1_2_same_name child_4 child_5 child_5_1 child_5_2 child_5_2_1 child_5_2_2 child_5_2_3 child_5_2_3_1 child_5_2_3_2 child_5_2_3_3 child_5_2_3_3_same_name child_5_same_name ''' # Create root node self.data_obj = SimpleDataObject("root") # Add some children, used by most tests. self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject3("child_5") self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.data_obj.insert_children(self.do_list) # Now let's add the children of children, etc. for use by # get_descendants() tests. # child_1 children self.child_1_1 = SimpleDataObject("child_1_1") self.child_1_2 = SimpleDataObject("child_1_2") self.child_1.insert_children([self.child_1_1, self.child_1_2]) # child_2 tree self.child_2_1 = SimpleDataObject2("child_2_1") self.child_2.insert_children(self.child_2_1) self.child_2_1_1 = SimpleDataObject2("child_2_1_1") self.child_2_1.insert_children(self.child_2_1_1) self.child_2_1_1_1 = SimpleDataObject2("child_2_1_1_1") self.child_2_1_1_2 = SimpleDataObject2("child_2_1_1_2") self.child_2_1_1.insert_children( [self.child_2_1_1_1, self.child_2_1_1_2]) # child_3 tree self.child_3_1 = SimpleDataObject("child_3_1") self.child_3.insert_children(self.child_3_1) self.child_3_1_1 = SimpleDataObject("child_3_1_1") self.child_3_1_2 = SimpleDataObject("child_3_1_2") self.child_3_1_2_same_name = SimpleDataObject("child_3_1_2") self.child_3_1.insert_children( [self.child_3_1_1, self.child_3_1_2, self.child_3_1_2_same_name]) # child_5 tree self.child_5_1 = SimpleDataObject("child_5_1") self.child_5_2 = SimpleDataObject("child_5_2") self.child_5.insert_children([self.child_5_1, self.child_5_2]) self.child_5_2_1 = SimpleDataObject("child_5_2_1") self.child_5_2_2 = SimpleDataObject("child_5_2_2") self.child_5_2_3 = SimpleDataObjectHandlesChildren("child_5_2_3") self.child_5_2.insert_children( [self.child_5_2_1, self.child_5_2_2, self.child_5_2_3]) self.child_5_2_3_1 = SimpleDataObject("child_5_2_3_1") self.child_5_2_3_2 = SimpleDataObject("child_5_2_3_2") self.child_5_2_3_3 = SimpleDataObject("child_5_2_3_3") self.child_5_2_3_3_same_name = SimpleDataObject("child_5_2_3_3") self.child_5_2_3.insert_children([ self.child_5_2_3_1, self.child_5_2_3_2, self.child_5_2_3_3, self.child_5_2_3_3_same_name ]) def tearDown(self): '''Clean up all references to objects''' self.data_obj = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.do_list = None self.child_1_1 = None self.child_1_2 = None self.child_2_1 = None self.child_2_1_1 = None self.child_2_1_1_1 = None self.child_2_1_1_2 = None self.child_3_1 = None self.child_3_1_1 = None self.child_3_1_2 = None self.child_3_1_2_same_name = None self.child_5_1 = None self.child_5_2 = None self.child_5_2_1 = None self.child_5_2_2 = None self.child_5_2_3 = None self.child_5_2_3_1 = None self.child_5_2_3_2 = None self.child_5_2_3_3 = None self.child_5_2_3_3_same_name = None def test_data_object_xml_support_skips_levels(self): '''Validate XML generation will skip levels if child doesn't gen XML''' # SimpleDataObject3 objects don't generate XML, so should be skipped # and all of child_5's children should be direct sub-elements of the # node called 'root' xml_tree = self.data_obj.get_xml_tree() child_names = [] for xml_child in xml_tree: child_names.append(xml_child.get("name")) self.assertEqual(child_names, [ self.child_1.name, self.child_2.name, self.child_3.name, self.child_4.name, self.child_5_1.name, self.child_5_2.name ]) def test_data_object_xml_support_parent_generates_xml(self): '''Validate ability to generate XML for children''' # child_5_2_3 generates the xml for it's children in the form: # <child name="<NAME>"/> xml_tree = self.child_5_2_3.get_xml_tree() names = [] for xml_child in xml_tree: self.assertEqual( xml_child.tag, SimpleDataObjectHandlesChildren.TAG_NAME, "sib-element had unexpected tag : %s" % (xml_child.tag)) names.append(xml_child.get("name")) self.assertEqual(names, \ [self.child_5_2_3_1.name, self.child_5_2_3_2.name, self.child_5_2_3_3.name, self.child_5_2_3_3_same_name.name]) def test_data_object_xml_support_get_tree_string(self): '''Validate ability to generate XML using get_xml_tree_str()''' # Define expected string, compensate for indent. Using '.' in expected # string to remove conflict with indent replacement. indentation = '''\ ''' expected_xml = '''\ <SimpleDataObject name="root"> ..<SimpleDataObject2 name="child_1"> ....<SimpleDataObject name="child_1_1"/> ....<SimpleDataObject name="child_1_2"/> ..</SimpleDataObject2> ..<SimpleDataObject name="child_2"> ....<SimpleDataObject2 name="child_2_1"> ......<SimpleDataObject2 name="child_2_1_1"> ........<SimpleDataObject2 name="child_2_1_1_1"/> ........<SimpleDataObject2 name="child_2_1_1_2"/> ......</SimpleDataObject2> ....</SimpleDataObject2> ..</SimpleDataObject> ..<SimpleDataObject name="child_3"> ....<SimpleDataObject name="child_3_1"> ......<SimpleDataObject name="child_3_1_1"/> ......<SimpleDataObject name="child_3_1_2"/> ......<SimpleDataObject name="child_3_1_2"/> ....</SimpleDataObject> ..</SimpleDataObject> ..<SimpleDataObject2 name="child_4"/> ..<SimpleDataObject name="child_5_1"/> ..<SimpleDataObject name="child_5_2"> ....<SimpleDataObject name="child_5_2_1"/> ....<SimpleDataObject name="child_5_2_2"/> ....<SimpleDataObjectHandlesChildren name="child_5_2_3"> ......<so_child name="child_5_2_3_1"/> ......<so_child name="child_5_2_3_2"/> ......<so_child name="child_5_2_3_3"/> ......<so_child name="child_5_2_3_3"/> ....</SimpleDataObjectHandlesChildren> ..</SimpleDataObject> </SimpleDataObject> '''.replace(indentation, "").replace(".", " ") xml_str = self.data_obj.get_xml_tree_str() self.assertEqual( xml_str, expected_xml, "Resulting XML doesn't match expected (len=%d != %d):\ \nGOT:\n'%s'\nEXPECTED:\n'%s'\n" % (len(xml_str), len(expected_xml), xml_str, expected_xml)) def test_data_object_xml_methods(self): '''Validate XML methods react correctly to None parameter''' self.assertFalse(DataObject.can_handle(None)) self.assertFalse(DataObject.from_xml(None))
class TestDataObjectXmlSupport(unittest.TestCase): '''Tests to ensure that DataObject XML support is working as expected''' def setUp(self): '''Create an XML tree for testing and references to them. The tree will look like the following: root child_1 child_1_1 child_1_2 child_2 child_2_1 child_2_1_1 child_2_1_1_1 child_2_1_1_2 child_3 child_3_1 child_3_1_2 child_3_1_2 child_3_1_2_same_name child_4 child_5 child_5_1 child_5_2 child_5_2_1 child_5_2_2 child_5_2_3 child_5_2_3_1 child_5_2_3_2 child_5_2_3_3 child_5_2_3_3_same_name child_5_same_name ''' # Create root node self.data_obj = SimpleDataObject("root") # Add some children, used by most tests. self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject3("child_5") self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.data_obj.insert_children(self.do_list) # Now let's add the children of children, etc. for use by # get_descendants() tests. # child_1 children self.child_1_1 = SimpleDataObject("child_1_1") self.child_1_2 = SimpleDataObject("child_1_2") self.child_1.insert_children([self.child_1_1, self.child_1_2]) # child_2 tree self.child_2_1 = SimpleDataObject2("child_2_1") self.child_2.insert_children(self.child_2_1) self.child_2_1_1 = SimpleDataObject2("child_2_1_1") self.child_2_1.insert_children(self.child_2_1_1) self.child_2_1_1_1 = SimpleDataObject2("child_2_1_1_1") self.child_2_1_1_2 = SimpleDataObject2("child_2_1_1_2") self.child_2_1_1.insert_children( [self.child_2_1_1_1, self.child_2_1_1_2]) # child_3 tree self.child_3_1 = SimpleDataObject("child_3_1") self.child_3.insert_children(self.child_3_1) self.child_3_1_1 = SimpleDataObject("child_3_1_1") self.child_3_1_2 = SimpleDataObject("child_3_1_2") self.child_3_1_2_same_name = SimpleDataObject("child_3_1_2") self.child_3_1.insert_children([self.child_3_1_1, self.child_3_1_2, self.child_3_1_2_same_name]) # child_5 tree self.child_5_1 = SimpleDataObject("child_5_1") self.child_5_2 = SimpleDataObject("child_5_2") self.child_5.insert_children([self.child_5_1, self.child_5_2]) self.child_5_2_1 = SimpleDataObject("child_5_2_1") self.child_5_2_2 = SimpleDataObject("child_5_2_2") self.child_5_2_3 = SimpleDataObjectHandlesChildren("child_5_2_3") self.child_5_2.insert_children( [self.child_5_2_1, self.child_5_2_2, self.child_5_2_3]) self.child_5_2_3_1 = SimpleDataObject("child_5_2_3_1") self.child_5_2_3_2 = SimpleDataObject("child_5_2_3_2") self.child_5_2_3_3 = SimpleDataObject("child_5_2_3_3") self.child_5_2_3_3_same_name = SimpleDataObject("child_5_2_3_3") self.child_5_2_3.insert_children( [self.child_5_2_3_1, self.child_5_2_3_2, self.child_5_2_3_3, self.child_5_2_3_3_same_name]) def tearDown(self): '''Clean up all references to objects''' self.data_obj = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.do_list = None self.child_1_1 = None self.child_1_2 = None self.child_2_1 = None self.child_2_1_1 = None self.child_2_1_1_1 = None self.child_2_1_1_2 = None self.child_3_1 = None self.child_3_1_1 = None self.child_3_1_2 = None self.child_3_1_2_same_name = None self.child_5_1 = None self.child_5_2 = None self.child_5_2_1 = None self.child_5_2_2 = None self.child_5_2_3 = None self.child_5_2_3_1 = None self.child_5_2_3_2 = None self.child_5_2_3_3 = None self.child_5_2_3_3_same_name = None def test_data_object_xml_support_skips_levels(self): '''Validate XML generation will skip levels if child doesn't gen XML''' # SimpleDataObject3 objects don't generate XML, so should be skipped # and all of child_5's children should be direct sub-elements of the # node called 'root' xml_tree = self.data_obj.get_xml_tree() child_names = [] for xml_child in xml_tree: child_names.append(xml_child.get("name")) self.assertEqual(child_names, [self.child_1.name, self.child_2.name, self.child_3.name, self.child_4.name, self.child_5_1.name, self.child_5_2.name]) def test_data_object_xml_support_parent_generates_xml(self): '''Validate ability to generate XML for children''' # child_5_2_3 generates the xml for it's children in the form: # <child name="<NAME>"/> xml_tree = self.child_5_2_3.get_xml_tree() names = [] for xml_child in xml_tree: self.assertEqual(xml_child.tag, SimpleDataObjectHandlesChildren.TAG_NAME, "sib-element had unexpected tag : %s" % (xml_child.tag)) names.append(xml_child.get("name")) self.assertEqual(names, \ [self.child_5_2_3_1.name, self.child_5_2_3_2.name, self.child_5_2_3_3.name, self.child_5_2_3_3_same_name.name]) def test_data_object_xml_support_get_tree_string(self): '''Validate ability to generate XML using get_xml_tree_str()''' # Define expected string, compensate for indent. Using '.' in expected # string to remove conflict with indent replacement. indentation = '''\ ''' expected_xml = '''\ <SimpleDataObject name="root"> ..<SimpleDataObject2 name="child_1"> ....<SimpleDataObject name="child_1_1"/> ....<SimpleDataObject name="child_1_2"/> ..</SimpleDataObject2> ..<SimpleDataObject name="child_2"> ....<SimpleDataObject2 name="child_2_1"> ......<SimpleDataObject2 name="child_2_1_1"> ........<SimpleDataObject2 name="child_2_1_1_1"/> ........<SimpleDataObject2 name="child_2_1_1_2"/> ......</SimpleDataObject2> ....</SimpleDataObject2> ..</SimpleDataObject> ..<SimpleDataObject name="child_3"> ....<SimpleDataObject name="child_3_1"> ......<SimpleDataObject name="child_3_1_1"/> ......<SimpleDataObject name="child_3_1_2"/> ......<SimpleDataObject name="child_3_1_2"/> ....</SimpleDataObject> ..</SimpleDataObject> ..<SimpleDataObject2 name="child_4"/> ..<SimpleDataObject name="child_5_1"/> ..<SimpleDataObject name="child_5_2"> ....<SimpleDataObject name="child_5_2_1"/> ....<SimpleDataObject name="child_5_2_2"/> ....<SimpleDataObjectHandlesChildren name="child_5_2_3"> ......<so_child name="child_5_2_3_1"/> ......<so_child name="child_5_2_3_2"/> ......<so_child name="child_5_2_3_3"/> ......<so_child name="child_5_2_3_3"/> ....</SimpleDataObjectHandlesChildren> ..</SimpleDataObject> </SimpleDataObject> '''.replace(indentation, "").replace(".", " ") xml_str = self.data_obj.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "Resulting XML doesn't match expected (len=%d != %d):\ \nGOT:\n'%s'\nEXPECTED:\n'%s'\n" % (len(xml_str), len(expected_xml), xml_str, expected_xml)) def test_data_object_xml_methods(self): '''Validate XML methods react correctly to None parameter''' self.assertFalse(DataObject.can_handle(None)) self.assertFalse(DataObject.from_xml(None))
class TestDataObjectDeletion(unittest.TestCase): '''Tests for various DataObject deletion methods''' def setUp(self): '''Create simple tree and references to children''' # Create root node self.data_obj = SimpleDataObject("root") # Add some children self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject("child_5") # Add a child to child_2 self.child_2_1 = SimpleDataObject("child_2_1") self.child_2.insert_children(self.child_2_1) self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.data_obj.insert_children(self.do_list) def tearDown(self): '''Clean up stored references''' self.data_obj = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.child_2_1 = None self.do_list = None def test_data_object_delete_self(self): '''Validate that self.delete() deletes self from parent''' obj = self.data_obj.get_children(self.child_4.name) self.assertEqual(obj, [self.child_4], "Failed to locate child_4 as child of data_obj.") self.child_4.delete() # Delete self from parent. self.assertRaises(ObjectNotFoundError, self.data_obj.get_children, self.child_4.name, not_found_is_err=True) def test_data_object_delete_self_and_children(self): '''Validate self.delete() deletes self plus children''' obj = self.data_obj.get_children(self.child_2.name) self.assertEqual(obj, [self.child_2], "Failed to locate child_2 as child of data_obj.") self.child_2.delete() # Delete self from parent. self.assertRaises(ObjectNotFoundError, self.data_obj.get_children, self.child_2.name, not_found_is_err=True) # Ensure that child_2 now has no children self.assertFalse(self.child_2.has_children, "child_2 shouldn't have children anymore.") # Ensure that child_2 now has no children self.assertFalse(self.child_2.has_children, "child_2 shouldn't have children anymore.") def test_data_object_delete_all(self): '''Validate delete_children() deletes all children nodes''' self.data_obj.delete_children() self.assertFalse(self.data_obj.has_children, str(self.data_obj)) def test_data_object_delete_specific_single(self): '''Validate deletion of a specific child by reference''' self.data_obj.delete_children(self.child_3) for child in self.data_obj.children: self.assertNotEqual( child.name, self.child_3.name, "Found deleted object 'child_3': %s" + str(self.data_obj)) def test_data_object_delete_specific_list(self): '''Validate deletion of a list of references to specific children''' self.data_obj.delete_children([self.child_3, self.child_5]) for child in self.data_obj.children: self.assertNotEqual( child, self.child_3, "Found deleted object 'child_3': %s" + str(self.data_obj)) self.assertNotEqual( child, self.child_5, "Found deleted object 'child_5': %s" + str(self.data_obj)) def test_data_object_delete_by_name_no_children(self): '''Validate failure if asked to delete children if there are none''' self.assertRaises(ObjectNotFoundError, self.child_2_1.delete_children, name="ignored", not_found_is_err=True) def test_data_object_delete_by_class_type_no_children(self): '''Validate failure if asked to delete non-existant class type''' self.assertRaises(ObjectNotFoundError, self.child_2_1.delete_children, class_type=SimpleDataObject, not_found_is_err=True) def test_data_object_delete_by_name(self): '''Validate failure if asked to delete non-exitant child by name''' self.data_obj.delete_children(name=self.child_4.name) for child in self.data_obj.children: self.assertNotEqual( child, self.child_4, "Found deleted object 'child_4': %s" + str(self.data_obj)) def test_data_object_delete_by_type(self): '''Validate correct deletion of specific class types''' # Should remove child_1 and child_4 which are of type SimpleDataObject2 self.data_obj.delete_children(class_type=SimpleDataObject2) for child in self.data_obj.children: self.assertNotEqual( child, self.child_1, "Found deleted object 'child_1': %s" + str(self.data_obj)) self.assertNotEqual( child, self.child_4, "Found deleted object 'child_4': %s" + str(self.data_obj)) def test_data_object_delete_by_name_and_type(self): '''Validate correct deletion of an obj by name and type''' # Should remove child_4 which has name and type SimpleDataObject2 self.data_obj.delete_children(name=self.child_4.name, class_type=SimpleDataObject2) found_child_1 = False for child in self.data_obj.children: if child == self.child_1: found_child_1 = True self.assertNotEqual( child, self.child_4, "Found deleted object 'child_4': %s" + str(self.data_obj)) self.assertTrue( found_child_1, "child_1 should still be present: %s" % (str(self.data_obj))) def test_data_object_delete_by_children_not_exist_single(self): '''Validate failure if asked to delete non-existant child reference''' not_a_child = SimpleDataObject("Not A Child 1") self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, not_a_child, not_found_is_err=True) def test_data_object_delete_by_children_not_exist_list(self): '''Validate failure when deleting a list of non-existant children''' not_a_child_list = [self.child_5, SimpleDataObject("Not A Child 1")] self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, not_a_child_list, not_found_is_err=True) def test_data_object_delete_by_children_not_exist_tuple(self): '''Validate deletion of a tuple containing non-existant ref''' not_a_child_list = (self.child_5, SimpleDataObject("Not A Child 1")) self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, not_a_child_list, not_found_is_err=True) def test_data_object_delete_by_name_not_exist(self): '''Validate failure when deleting non-existant child by name''' self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, name="non_existant_name", not_found_is_err=True) def test_data_object_delete_by_type_not_exist(self): '''Validate failure when deleting non-existant child by type''' self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, class_type=SimpleDataObject3, not_found_is_err=True) def test_data_object_delete_by_name_exist_and_type_not_exist(self): '''Validate failure when deleting child name and non-existant type''' self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, name=self.child_4.name, class_type=SimpleDataObject3, not_found_is_err=True) def test_data_object_delete_by_name_exist_and_type_not_exist_not_err(self): '''Validate no err when deleting child name and non-existant type''' self.data_obj.delete_children(name=self.child_4.name, class_type=SimpleDataObject3, not_found_is_err=False) def test_data_object_delete_by_name_not_exist_and_type_exist(self): '''Validate failure when deleting child non-existant name and type''' self.assertRaises(ObjectNotFoundError, self.data_obj.delete_children, name="non existant name", class_type=SimpleDataObject2, not_found_is_err=True) def test_data_object_delete_by_name_not_exist_and_type_exist_not_err(self): '''Validate not err when deleting child non-existant name and type''' self.data_obj.delete_children(name="non existant name", class_type=SimpleDataObject2, not_found_is_err=False) def test_data_object_delete_by_object_with_name_and_type_ignored(self): '''Validate name and type ignored if specific child ref provided''' # Should remove child_3 only, and ignore name and type self.data_obj.delete_children(children=self.child_3, name=self.child_4.name, class_type=SimpleDataObject2) found_child_1 = False found_child_2 = False found_child_4 = False found_child_5 = False for child in self.data_obj.children: if child == self.child_1: found_child_1 = True if child == self.child_2: found_child_2 = True if child == self.child_4: found_child_4 = True if child == self.child_5: found_child_5 = True self.assertNotEqual( child, self.child_3, "Found deleted object 'child_3': %s" + str(self.data_obj)) self.assertTrue( found_child_1, "child_1 should still be present: %s" % (str(self.data_obj))) self.assertTrue( found_child_2, "child_2 should still be present: %s" % (str(self.data_obj))) self.assertTrue( found_child_4, "child_4 should still be present: %s" % (str(self.data_obj))) self.assertTrue( found_child_5, "child_5 should still be present: %s" % (str(self.data_obj)))
def test_data_object_simple_can_insert_to_doc(self): '''Validate SimpleXmlHandlerBase can be inserted as child of DataObject ''' data_obj = SimpleDataObject("test_obj") data_obj.insert_children(SimpleXmlHandlerTag("test"))
class TestDataObjectCacheSnapshots(unittest.TestCase): '''Tests to validate DOC snapshots support''' def setUp(self): '''Set up correct environment for tests. Creates reference to a temp dir and file and StringIO buffer. Creates a tree of elements in the DOC to validate before/after. ''' # Create temporary work directory self.temp_dir = mkdtemp(prefix="doc_test-") self.temp_file = mktemp(prefix="snapshot-", dir=self.temp_dir) # Create StringIO memory buffer for I/O test self.buffer = StringIO() # Create a tree that looks like: # DOC # volatile # volatile_root # persistent # persistent_root # child_1 # child_1_1 # child_1_2 # child_2 # child_2_1 # child_2_1_1 # child_2_1_1_1 # child_2_1_1_2 # child_3 # child_3_1 # child_3_1_2 # child_3_1_2 # child_3_1_2_same_name # child_4 # child_5 # child_5_1 # child_5_2 # child_5_2_1 # child_5_2_2 # child_5_2_3 # child_5_2_3_1 # child_5_2_3_2 # child_5_2_3_3 # child_5_2_3_3_same_name # child_5_same_name # Create DOC node self.doc = DataObjectCache() # Create some persistent content self.persistent_root = SimpleDataObject("persistent_root") # Add some children, used by most tests. self.child_1 = SimpleDataObject2("child_1") self.child_2 = SimpleDataObject("child_2") self.child_3 = SimpleDataObject("child_3") self.child_4 = SimpleDataObject2("child_4") self.child_5 = SimpleDataObject3("child_5") self.child_5_same_name = SimpleDataObject("child_5") self.do_list = list() self.do_list.append(self.child_1) self.do_list.append(self.child_2) self.do_list.append(self.child_3) self.do_list.append(self.child_4) self.do_list.append(self.child_5) self.do_list.append(self.child_5_same_name) self.persistent_root.insert_children(self.do_list) self.doc.persistent.insert_children(self.persistent_root) # Now let's add the children of children, etc. for use by # get_descendants() tests. # child_1 children self.child_1_1 = SimpleDataObject("child_1_1") self.child_1_2 = SimpleDataObject("child_1_2") self.child_1.insert_children([self.child_1_1, self.child_1_2]) # child_2 tree self.child_2_1 = SimpleDataObject2("child_2_1") self.child_2.insert_children(self.child_2_1) self.child_2_1_1 = SimpleDataObject2("child_2_1_1") self.child_2_1.insert_children(self.child_2_1_1) self.child_2_1_1_1 = SimpleDataObject2("child_2_1_1_1") self.child_2_1_1_2 = SimpleDataObject2("child_2_1_1_2") self.child_2_1_1.insert_children( [self.child_2_1_1_1, self.child_2_1_1_2]) # child_3 tree self.child_3_1 = SimpleDataObject("child_3_1") self.child_3.insert_children(self.child_3_1) self.child_3_1_1 = SimpleDataObject("child_3_1_1") self.child_3_1_2 = SimpleDataObject("child_3_1_2") self.child_3_1_2_same_name = SimpleDataObject("child_3_1_2") self.child_3_1.insert_children( [self.child_3_1_1, self.child_3_1_2, self.child_3_1_2_same_name]) # child_5 tree self.child_5_1 = SimpleDataObject("child_5_1") self.child_5_2 = SimpleDataObject("child_5_2") self.child_5.insert_children([self.child_5_1, self.child_5_2]) self.child_5_2_1 = SimpleDataObject("child_5_2_1") self.child_5_2_2 = SimpleDataObject("child_5_2_2") self.child_5_2_3 = SimpleDataObject4("child_5_2_3") self.child_5_2.insert_children( [self.child_5_2_1, self.child_5_2_2, self.child_5_2_3]) self.child_5_2_3_1 = SimpleDataObject("child_5_2_3_1") self.child_5_2_3_2 = SimpleDataObject("child_5_2_3_2") self.child_5_2_3_3 = SimpleDataObject("child_5_2_3_3") self.child_5_2_3_3_same_name = SimpleDataObject("child_5_2_3_3") self.child_5_2_3.insert_children([ self.child_5_2_3_1, self.child_5_2_3_2, self.child_5_2_3_3, self.child_5_2_3_3_same_name ]) # Create some volatile content, not much, just enough to test that it's # not overwritten on loading of snapshot. self.volatile_root = SimpleDataObject2("volatile_root") self.doc.volatile.insert_children(self.volatile_root) def tearDown(self): '''Cleanup test environment and references.''' # Remove temp dir, may not always be there. if path.exists(self.temp_file): unlink(self.temp_file) if path.exists(self.temp_dir): rmdir(self.temp_dir) # Remove buffer self.buffer.close() # Free's memory self.buffer = None # Unset variables. self.doc.clear() self.doc = None self.volatile_root = None self.persistent_root = None self.child_1 = None self.child_2 = None self.child_3 = None self.child_4 = None self.child_5 = None self.child_5_same_name = None self.do_list = None self.child_1_1 = None self.child_1_2 = None self.child_2_1 = None self.child_2_1_1 = None self.child_2_1_1_1 = None self.child_2_1_1_2 = None self.child_3_1 = None self.child_3_1_1 = None self.child_3_1_2 = None self.child_3_1_2_same_name = None self.child_5_1 = None self.child_5_2 = None self.child_5_2_1 = None self.child_5_2_2 = None self.child_5_2_3 = None self.child_5_2_3_1 = None self.child_5_2_3_2 = None self.child_5_2_3_3 = None self.child_5_2_3_3_same_name = None def test_data_object_cache_snapshots_write_file_string(self): '''Validate writing of a snapshot to a file''' try: self.doc.take_snapshot(self.temp_file) except Exception, e: self.fail("Got unexpected error writing snapshot: " + str(e)) try: stat_info = stat(self.temp_file) self.assertFalse( stat_info.st_size < 2048, "Snapshot file size is too small: %d" % (stat_info.st_size)) except Exception, e: self.fail("Got unexpected error stat-ing snapshot file: " + str(e))
class TestDataObjectInsertion(unittest.TestCase): '''Tests for DataObject insertion methods''' def setUp(self): '''Create simple data object reference''' self.data_obj = SimpleDataObject("root") def tearDown(self): '''Clean up data_obj reference''' self.data_obj = None def test_insert_children_default_single(self): '''Validate insertion of single child''' new_do = SimpleDataObject("child_1") self.data_obj.insert_children(new_do) self.assertEqual(self.data_obj.children[0], new_do) def test_insert_children_default_fail_both_before_and_after(self): '''Validate failure on insert with invalid before and after''' new_do = SimpleDataObject("child_1") self.assertRaises(ValueError, self.data_obj.insert_children, new_do, new_do, new_do) def test_insert_children_default_fail_single(self): '''Validate failure on insert with non-DataObjectBase sub-class''' new_obj = object() self.assertRaises(TypeError, self.data_obj.insert_children, new_obj) def test_insert_children_default_fail_single_null_param(self): '''Validate failure on insert of None value''' self.assertRaises(TypeError, self.data_obj.insert_children, None) def test_insert_children_default_list(self): '''Validate insert of list of children''' new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) i = 0 for child in self.data_obj.children: self.assertEqual(child, new_do_list[i]) i += 1 self.assertEqual(i, len(new_do_list)) def test_insert_children_default_fail_list(self): '''Validate failure of insert of list of children with bad element''' new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(object()) self.assertRaises(TypeError, self.data_obj.insert_children, new_do_list) def test_insert_children_default_fail_tuple(self): '''Validate failure of insert of tuple of children with bad element''' # Create tuple new_do_list = ( SimpleDataObject("child_1"), SimpleDataObject("child_2"), SimpleDataObject("child_3"), SimpleDataObject("child_4"), object()) self.assertRaises(TypeError, self.data_obj.insert_children, new_do_list) def test_insert_children_default_fail_invalid_type(self): '''Validate failure of insert of child with non-DataObjectBase''' self.assertRaises(TypeError, self.data_obj.insert_children, object()) def test_insert_children_before_single(self): '''Validate insertion of children list with before value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 new_do = SimpleDataObject("before_child_3") self.data_obj.insert_children(new_do, before=child_3) i = 0 for child in self.data_obj.children: if i == 2: self.assertEqual(child, new_do, str(self.data_obj)) break i += 1 def test_insert_children_before_doesnt_exist(self): '''Validate failure on insertion with non-existant 'before' obj''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) not_in_list = SimpleDataObject("child_not_in_list") new_do = SimpleDataObject("before_child_3") #Now for the real test, to insert something before non-existant child self.assertRaises(ObjectNotFoundError, self.data_obj.insert_children, new_do, before=not_in_list) def test_insert_children_after_doesnt_exist(self): '''Validate failure on insertion with non-existant 'after' obj''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) not_in_list = SimpleDataObject("child_not_in_list") new_do = SimpleDataObject("after_child_3") #Now for the real test, to insert something after non-existant child self.assertRaises(ObjectNotFoundError, self.data_obj.insert_children, new_do, after=not_in_list) def test_insert_children_before_first_single(self): '''Validate insertion of child with before == first child''' # Populate existing children first. new_do_list = list() child_1 = SimpleDataObject("child_1") new_do_list.append(child_1) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_1 new_do = SimpleDataObject("before_child_1") self.data_obj.insert_children(new_do, before=child_1) self.assertEqual(self.data_obj.children[0], new_do, str(self.data_obj)) def test_insert_children_after_single(self): '''Validate insertion of children with after value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 new_do = SimpleDataObject("after_child_5") self.data_obj.insert_children(new_do, after=child_3) i = 0 for child in self.data_obj.children: if i == 3: self.assertEqual(child, new_do, str(self.data_obj)) break i += 1 def test_insert_children_after_last_single(self): '''Validate insertion of children with after == last child''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) child_5 = SimpleDataObject("child_5") new_do_list.append(child_5) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_5 new_do = SimpleDataObject("after_child_5") self.data_obj.insert_children(new_do, after=child_5) children = self.data_obj.children self.assertEqual(children[len(children) - 1], new_do, str(self.data_obj)) def test_insert_children_before_list(self): '''Validate insertion of children with before value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_3 to_insert = list() to_insert.append(SimpleDataObject("before_child_3 - A")) to_insert.append(SimpleDataObject("before_child_3 - B")) to_insert.append(SimpleDataObject("before_child_3 - C")) self.data_obj.insert_children(to_insert, before=child_3) i = 0 j = 0 for child in self.data_obj.children: if i >= 2: self.assertEqual(child, to_insert[j], "child = %s ; compared_to = %s" % (child, to_insert[j])) j += 1 if j >= len(to_insert): break i += 1 def test_insert_children_before_first_list(self): '''Validate insertion of children with before == first child''' # Populate existing children first. new_do_list = list() child_1 = SimpleDataObject("child_1") new_do_list.append(child_1) new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something before child_1 to_insert = list() to_insert.append(SimpleDataObject("before_child_1 - A")) to_insert.append(SimpleDataObject("before_child_1 - B")) to_insert.append(SimpleDataObject("before_child_1 - C")) self.data_obj.insert_children(to_insert, before=child_1) i = 0 for child in self.data_obj.children: self.assertEqual(child, to_insert[i], "child = %s ; compared_to = %s" % (child, to_insert[i])) i += 1 if not (i < len(to_insert)): break def test_insert_children_after_list(self): '''Validate insertion of children list with after value''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_1")) new_do_list.append(SimpleDataObject("child_2")) child_3 = SimpleDataObject("child_3") new_do_list.append(child_3) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_3 to_insert = list() to_insert.append(SimpleDataObject("after_child_3 - A")) to_insert.append(SimpleDataObject("after_child_3 - B")) to_insert.append(SimpleDataObject("after_child_3 - C")) self.data_obj.insert_children(to_insert, after=child_3) i = 0 j = 0 for child in self.data_obj.children: if i >= 3: self.assertEqual(child, to_insert[j], "child = %s ; compared_to = %s" % (child, to_insert[j])) j += 1 if j >= len(to_insert): break i += 1 def test_insert_children_after_last_list(self): '''Validate insertion of children list with after == last child''' # Populate existing children first. new_do_list = list() new_do_list.append(SimpleDataObject("child_2")) new_do_list.append(SimpleDataObject("child_3")) new_do_list.append(SimpleDataObject("child_4")) new_do_list.append(SimpleDataObject("child_5")) child_5 = SimpleDataObject("child_5") new_do_list.append(child_5) self.data_obj.insert_children(new_do_list) #Now for the real test, to insert something after child_5 to_insert = list() to_insert.append(SimpleDataObject("after_child_3 - A")) to_insert.append(SimpleDataObject("after_child_3 - B")) to_insert.append(SimpleDataObject("after_child_3 - C")) self.data_obj.insert_children(to_insert, after=child_5) children = self.data_obj.children num_children = len(children) offset = num_children - len(to_insert) for i in range(len(to_insert)): self.assertEqual(children[offset + i], to_insert[i], "child = %s ; compared_to = %s" % (children[offset + i], to_insert[i]))