def test_diff_key_model_file(self): """MultipleElement diff method with ModelFile elements with a specific key""" class MyElement(ModelFile): def __init__(self, sep='=', linesep=" "): ModelFile.__init__(self, sep, linesep) self.add_element('node', check='string') self.add_element('data', check='string') def key(self): return self.get('node') elem = MultipleElement(MyElement()) elem.parse("node=foo data=bar") other = elem.emptycopy() other.parse("node=foo data=bar") other.parse("node=foo2 data=else") added, changed, removed = elem.diff(other) self.assertEqual(added.as_dict(), [{'node':'foo2', 'data':'else'}]) self.assertEqual(removed.get(), None) self.assertEqual(changed.get(), None) other.clear() other.parse("node=foo data=else") added, changed, removed = elem.diff(other) self.assertEqual(added.get(), None) self.assertEqual(removed.get(), None) self.assertEqual(changed.as_dict(), [{'node':'foo', 'data':'else'}]) self.assertEqual(str(changed.elements()[0].old), "node=foo data=bar")
def test_diff_key_model_file(self): """MultipleElement diff method with ModelFile elements with a specific key""" class MyElement(ModelFile): def __init__(self, sep='=', linesep=" "): ModelFile.__init__(self, sep, linesep) self.add_element('node', check='string') self.add_element('data', check='string') def key(self): return self.get('node') elem = MultipleElement(MyElement()) elem.parse("node=foo data=bar") other = elem.emptycopy() other.parse("node=foo data=bar") other.parse("node=foo2 data=else") added, changed, removed = elem.diff(other) self.assertEqual(added.as_dict(), [{'node': 'foo2', 'data': 'else'}]) self.assertEqual(removed.get(), None) self.assertEqual(changed.get(), None) other.clear() other.parse("node=foo data=else") added, changed, removed = elem.diff(other) self.assertEqual(added.get(), None) self.assertEqual(removed.get(), None) self.assertEqual(changed.as_dict(), [{'node': 'foo', 'data': 'else'}]) self.assertEqual(str(changed.elements()[0].old), "node=foo data=bar")
def testBase(self): """MultipleElement base methods""" elem = MultipleElement(SimpleElement(check='string')) # Default self.assertEqual(elem.get([]), []) # First add() elem.add("3") self.assertEqual(elem.get(), ["3"]) self.assertEqual(elem.content(), ["3"]) self.assertEqual(elem[0], "3") self.assertEqual(str(elem), "3") self.assertEqual(len(elem), 1) # Second add() elem.add("5") elem.add("7") self.assertEqual(elem.get(), ["3", "5", "7"]) self.assertEqual(elem.content(), ["3", "5", "7"]) self.assertEqual(elem[1], "5") self.assertEqual(str(elem), "3\n5\n7") self.assertEqual(len(elem), 3) # Remove an item del elem[1] self.assertEqual(elem.get(), ["3", "7"]) self.assertEqual(len(elem), 2) elem.remove("3") self.assertEqual(elem.get(), ["7"]) self.assertEqual(len(elem), 1) self.assertRaises(KeyError, elem.remove, "6") # replace() elem.replace("12") self.assertEqual(elem.get(), ["12"]) # clear() elem.clear() self.assertEqual(len(elem), 0) # parse() elem.parse("4") self.assertEqual(elem.get(), ["4"]) self.assertEqual(str(elem), "4") self.assertEqual(len(elem), 1) # __eq__ other = elem.emptycopy() self.assertNotEqual(elem, other) other.add("4") self.assertEqual(elem, other)
def testBase(self): """MultipleElement base methods""" elem = MultipleElement(SimpleElement(check='string')) # Default self.assertEqual(elem.get([]), []) # First add() elem.add("3") self.assertEqual(elem.get(), ["3"]) self.assertEqual(elem.content(), ["3"]) self.assertEqual(elem[0], "3") self.assertEqual(str(elem), "3") self.assertEqual(len(elem), 1) # Second add() elem.add("5") elem.add("7") self.assertEqual(elem.get(), ["3", "5", "7"]) self.assertEqual(elem.content(), ["3", "5", "7"]) self.assertEqual(elem[1], "5") self.assertEqual(str(elem), "3 5 7") self.assertEqual(len(elem), 3) # Remove an item del elem[1] self.assertEqual(elem.get(), ["3", "7"]) self.assertEqual(len(elem), 2) elem.remove("3") self.assertEqual(elem.get(), ["7"]) self.assertEqual(len(elem), 1) self.assertRaises(KeyError, elem.remove, "6") # replace() elem.replace("12") self.assertEqual(elem.get(), ["12"]) # clear() elem.clear() self.assertEqual(len(elem), 0) # parse() elem.parse("4") self.assertEqual(elem.get(), ["4"]) self.assertEqual(str(elem), "4") self.assertEqual(len(elem), 1) # __eq__ other = elem.emptycopy() self.assertNotEqual(elem, other) other.add("4") self.assertEqual(elem, other)