def testDiffMultipleElement(self): """diff between 2 modelfiles with a MultipleElement""" model = ModelFile() model.add_element('name', check='string', multiple=True) model2 = model.emptycopy() # Empty models have no difference added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Same model have no difference model.add('name', 'foo') model2.add('name', 'foo') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Add a value model2.add('name', 'bar') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'name': ['bar']}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Remove a value del model2['name'] model.add('name', 'bar') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {'name': ['foo', 'bar']}) self.assertEqual(changed.as_dict(), {})
def testDiffDictMultipleElement(self): """diff between 2 modelfiles with a multiple subelement""" model = ModelFile() element = ModelFile(sep="=", linesep=" ") element.add_element('dev', check='string') element.add_element('index', check='digit') model.add_custom('target', element, multiple=True) model2 = model.emptycopy() # Empty models have no difference added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Same model have no difference model.parse('target: dev=/dev/sda index=1') model2.parse('target: dev=/dev/sda index=1') self.assertEqual(len(model['target']), 1) self.assertEqual(len(model2['target']), 1) added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Add a new element model2.parse('target: dev=/dev/sdb index=2') self.assertEqual(len(model['target']), 1) self.assertEqual(len(model2['target']), 2) added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'target': [{ 'index': 2, 'dev': '/dev/sdb' }]}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Change an element del model2['target'] model2.parse('target: dev=/dev/sdb index=1') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'target': [{ 'index': 1, 'dev': '/dev/sdb' }]}) self.assertEqual(removed.as_dict(), {'target': [{ 'index': 1, 'dev': '/dev/sda' }]}) self.assertEqual(changed.as_dict(), {})
def testSaveModelToFile(self): """save a ModelFile to a file""" testfile = makeTempFile("""foo: my test bar: 3 foo: another bar: 1 bar: 2""") # Create a model model = ModelFile() model.add_element("foo", check="string", multiple=True) model.add_element("bar", check="digit", multiple=True) # Load a test file model.load(testfile.name) # Save it to a new file filename = makeTempFilename() model.save(filename, "# Some header") # Reload the file in another model model2 = model.emptycopy() model2.load(filename) os.unlink(filename) # Compare the two files. They should have no difference added, changed, removed = model.diff(model2) self.assertTrue(len(changed) == len(added) == len(removed) == 0)
def testDiffDictMultipleElement(self): """diff between 2 modelfiles with a multiple subelement""" model = ModelFile() element = ModelFile(sep="=", linesep=" ") element.add_element('dev', check='string') element.add_element('index', check='digit') model.add_custom('target', element, multiple=True) model2 = model.emptycopy() # Empty models have no difference added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Same model have no difference model.parse('target: dev=/dev/sda index=1') model2.parse('target: dev=/dev/sda index=1') self.assertEqual(len(model['target']), 1) self.assertEqual(len(model2['target']), 1) added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Add a new element model2.parse('target: dev=/dev/sdb index=2') self.assertEqual(len(model['target']), 1) self.assertEqual(len(model2['target']), 2) added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'target': [{'index': 2, 'dev': '/dev/sdb'}]}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Change an element del model2['target'] model2.parse('target: dev=/dev/sdb index=1') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'target': [{'index': 1, 'dev': '/dev/sdb'}]}) self.assertEqual(removed.as_dict(), {'target': [{'index': 1, 'dev': '/dev/sda'}]}) self.assertEqual(changed.as_dict(), {})
def testDiffSimpleElement(self): """diff between 2 modelfiles with a SimpleElement""" model = ModelFile() model.add_element('name', check='string') model2 = model.emptycopy() # Empty models have no difference added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Same model have no difference model.add('name', 'foo') model2.add('name', 'foo') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # A value changed del model2['name'] model2.add('name', 'bar') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {'name': 'bar'}) # A value added del model['name'] added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {'name': 'bar'}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # A value removed added, changed, removed = model2.diff(model) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {'name': 'bar'}) self.assertEqual(changed.as_dict(), {})
def testDiffDictElement(self): """diff between 2 modelfiles with a subelement""" model = ModelFile() element = ModelFile(sep="=", linesep=" ") element.add_element('size', check='digit') element.add_element('count', check='digit') model.add_custom('stripe', element) model2 = model.emptycopy() # Empty models have no difference added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Same model have no difference model.parse('stripe: count=1 size=1000000') model2.parse('stripe: count=1 size=1000000') added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {}) # Remove an attribute del model2.get('stripe')['count'] added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {'stripe': {'count': 1}}) self.assertEqual(changed.as_dict(), {}) # Change an attribute model2.get('stripe').add('count', 2) added, changed, removed = model.diff(model2) self.assertEqual(added.as_dict(), {}) self.assertEqual(removed.as_dict(), {}) self.assertEqual(changed.as_dict(), {'stripe': {'count': 2}})