class TestFindSections(unittest.TestCase): def setUp(self): self.root_section = Section("Type test", "test") self.root_section.append(Section("sub_1", "sub_1")) self.root_section.append(Section("sub_2", "sub_2")) self.root_section.append(Section("sub_2_b", "sub_2")) self.root_section.append(Section("sub_3", "sub_2/sub_3")) def test_find_by_name(self): ret = self.root_section.find("sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find("unknown_type") self.assertIsNone(ret) def test_find_by_type(self): ret = self.root_section.find(type="sub_1") self.assertTrue(ret is not None and ret.type == "sub_1") ret = self.root_section.find(type="sub_2", findAll=True) self.assertTrue(len(ret) == 2) ret = self.root_section.find(key=None, type="sub_2", findAll=True, include_subtype=True) self.assertTrue(len(ret) == 3) def test_find_by_name_and_type(self): ret = self.root_section.find(key="sub_1", type="sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find(key="sub_1", type="sub_2") self.assertIsNone(ret)
class TestFindSections(unittest.TestCase): def setUp(self): self.root_section = Section("Type test", "test") self.root_section.append(Section("sub_1", "sub_1")) self.root_section.append(Section("sub_2", "sub_2")) self.root_section.append(Section("sub_2_b", "sub_2")) self.root_section.append(Section("sub_3", "sub_2/sub_3")) def test_find_by_name(self): ret = self.root_section.find("sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find("unknown_type") self.assertIsNone(ret) def test_find_by_type(self): ret = self.root_section.find(type="sub_1") self.assertTrue(ret is not None and ret.type == "sub_1") ret = self.root_section.find(type="sub_2", findAll=True) self.assertTrue(len(ret) == 2) ret = self.root_section.find(key=None, type="sub_2", findAll=True, include_subtype=True) self.assertTrue(len(ret) == 3) def test_find_by_name_and_type(self): ret = self.root_section.find(key="sub_1", type="sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find(key="sub_1", type="sub_2") self.assertIsNone(ret)
def test_append(self): main = Section(name="main") self.assertListEqual(main.sections, []) self.assertListEqual(main.properties, []) # Test append Section sec = Section(name="sec1") main.append(sec) self.assertEqual(len(main.sections), 1) self.assertEqual(sec.parent, main) # Test fail on Section list or tuple append with self.assertRaises(ValueError): main.append([Section(name="sec2"), Section(name="sec3")]) with self.assertRaises(ValueError): main.append((Section(name="sec2"), Section(name="sec3"))) self.assertEqual(len(main.sections), 1) # Test append Property prop = Property(name="prop") main.append(prop) self.assertEqual(len(main.properties), 1) self.assertEqual(prop.parent, main) # Test fail on Property list or tuple append with self.assertRaises(ValueError): main.append([Property(name="prop2"), Property(name="prop3")]) with self.assertRaises(ValueError): main.append((Property(name="prop2"), Property(name="prop3"))) self.assertEqual(len(main.properties), 1) # Test fail on unsupported value with self.assertRaises(ValueError): main.append(Document()) with self.assertRaises(ValueError): main.append("Section") # Test fail on same name entities with self.assertRaises(KeyError): main.append(Section(name="sec1")) self.assertEqual(len(main.sections), 1) with self.assertRaises(KeyError): main.append(Property(name="prop")) self.assertEqual(len(main.properties), 1)
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) if sys.version_info < (3, 0): str_doc = unicode(XMLWriter(doc)) else: str_doc = str(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] p = new_sec.properties["strprop"] assert (p.dtype == "string") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["txtprop"] assert (p.dtype == "text") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["intprop"] assert (p.dtype == "int") assert (type(p.value[0]) == int) p = new_sec.properties["floatprop"] assert (p.dtype == "float") assert (type(p.value[0]) == float) p = new_sec.properties["datetimeprop"] assert (p.dtype == "datetime") assert (type(p.value[0]) == dt) p = new_sec.properties["dateprop"] assert (p.dtype == "date") assert (type(p.value[0]) == date) p = new_sec.properties["timeprop"] assert (p.dtype == "time") assert (type(p.value[0]) == time) p = new_sec.properties["boolprop"] assert (p.dtype == "boolean") assert (type(p.value[0]) == bool)
def setUp(self): """ doc -- <section foo> -- <section subfoo> \ -- <section bar> -- <section subbar> """ doc = Document("author") foo = Section("foo", "footype") doc.append(foo) foo.append(Property("strprop", "somestring")) foo.append(Property("txtprop", "some\ntext")) subfoo = Section("subfoo", "footype") foo.append(subfoo) subfoo.append(Property("strprop", "somestring")) subfoo.append(Property("txtprop", "some\ntext")) bar = Section("bar", "bartype") foo.append(bar) bar.append(Property("strprop", "otherstring")) bar.append(Property("txtprop", "other\ntext")) subbar = Section("subbar", "bartype") bar.append(subbar) subbar.append(Property("strprop", "otherstring")) subbar.append(Property("txtprop", "other\ntext")) self.doc = doc
def setUp(self): """ doc -- <section foo> -- <section subfoo> \ -- <section bar> -- <section subbar> """ doc = Document("author") foo = Section("foo", "footype") doc.append(foo) foo.append(Property("strprop", "somestring")) foo.append(Property("txtprop", "some\ntext")) subfoo = Section("subfoo", "footype") foo.append(subfoo) subfoo.append(Property("strprop", "somestring")) subfoo.append(Property("txtprop", "some\ntext")) bar = Section("bar", "bartype") foo.append(bar) bar.append(Property("strprop", "otherstring")) bar.append(Property("txtprop", "other\ntext")) subbar = Section("subbar", "bartype") bar.append(subbar) subbar.append(Property("strprop", "otherstring")) subbar.append(Property("txtprop", "other\ntext")) self.doc = doc
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = unicode(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] v = new_sec.properties["strprop"].value assert(v.dtype == "string") assert(type(v.data) == unicode) v = new_sec.properties["txtprop"].value assert(v.dtype == "text") assert(type(v.data) == unicode) v = new_sec.properties["intprop"].value assert(v.dtype == "int") assert(type(v.data) == int) v = new_sec.properties["floatprop"].value assert(v.dtype == "float") assert(type(v.data) == float) v = new_sec.properties["datetimeprop"].value assert(v.dtype == "datetime") assert(type(v.data) == dt) v = new_sec.properties["dateprop"].value assert(v.dtype == "date") assert(type(v.data) == date) v = new_sec.properties["timeprop"].value assert(v.dtype == "time") assert(type(v.data) == time) v = new_sec.properties["boolprop"].value assert(v.dtype == "boolean") assert(type(v.data) == bool)
def setUp(self): """ doc -- <section sec_main> -- <section sub_main> \ -- <section sec_branch> -- <section sub_branch> """ doc = Document("author") sec_main = Section("sec_main", "maintype") doc.append(sec_main) sec_main.append(Property("strprop", "somestring")) sec_main.append(Property("txtprop", "some\ntext")) sub_main = Section("sub_main", "maintype") sec_main.append(sub_main) sub_main.append(Property("strprop", "somestring")) sub_main.append(Property("txtprop", "some\ntext")) sec_branch = Section("sec_branch", "branchtype") sec_main.append(sec_branch) sec_branch.append(Property("strprop", "otherstring")) sec_branch.append(Property("txtprop", "other\ntext")) sub_branch = Section("sub_branch", "branchtype") sec_branch.append(sub_branch) sub_branch.append(Property("strprop", "otherstring")) sub_branch.append(Property("txtprop", "other\ntext")) self.doc = doc
def test_read_write(self): doc = Document("author") sec = Section("name", "type", parent=doc) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = str(XMLWriter(doc)) new_doc = XMLReader().from_string(str_doc) new_sec = new_doc.sections[0] prop = new_sec.properties["strprop"] self.assertEqual(prop.dtype, "string") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["txtprop"] self.assertEqual(prop.dtype, "text") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["intprop"] self.assertEqual(prop.dtype, "int") self.assertIsInstance(prop.values[0], int) prop = new_sec.properties["floatprop"] self.assertEqual(prop.dtype, "float") self.assertIsInstance(prop.values[0], float) prop = new_sec.properties["datetimeprop"] self.assertEqual(prop.dtype, "datetime") self.assertIsInstance(prop.values[0], dt) prop = new_sec.properties["dateprop"] self.assertEqual(prop.dtype, "date") self.assertIsInstance(prop.values[0], date) prop = new_sec.properties["timeprop"] self.assertEqual(prop.dtype, "time") self.assertIsInstance(prop.values[0], time) prop = new_sec.properties["boolprop"] self.assertEqual(prop.dtype, "boolean") self.assertIsInstance(prop.values[0], bool)
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = unicode(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] v = new_sec.properties["strprop"].value assert (v.dtype == "string") assert (type(v.data) == unicode) v = new_sec.properties["txtprop"].value assert (v.dtype == "text") assert (type(v.data) == unicode) v = new_sec.properties["intprop"].value assert (v.dtype == "int") assert (type(v.data) == int) v = new_sec.properties["floatprop"].value assert (v.dtype == "float") assert (type(v.data) == float) v = new_sec.properties["datetimeprop"].value assert (v.dtype == "datetime") assert (type(v.data) == dt) v = new_sec.properties["dateprop"].value assert (v.dtype == "date") assert (type(v.data) == date) v = new_sec.properties["timeprop"].value assert (v.dtype == "time") assert (type(v.data) == time) v = new_sec.properties["boolprop"].value assert (v.dtype == "boolean") assert (type(v.data) == bool)