def test_write_file_change(self): """ Should write the file again if it changed. """ ppd = PPD() id1 = ppd.addFile(StringIO('foo bar'), 'joe.txt', {'meta': 'a'}) id2 = ppd.addFile(StringIO('baz who'), 'joe.txt', {'meta': 'b'}) obj1 = ppd.getObject(id1) obj2 = ppd.getObject(id2) tmpdir = FilePath(self.mktemp()) reported = [] dumper = RuleBasedFileDumper(tmpdir.path, ppd=ppd, reporter=reported.append) dumper.performAction({ 'write_file': 'foo/bar/{filename}', }, obj1) reported.pop() dumper.performAction({ 'write_file': 'foo/bar/{filename}', }, obj2) exp = tmpdir.child('foo').child('bar').child('joe.txt') self.assertTrue(exp.exists()) self.assertEqual(exp.getContent(), 'baz who') self.assertEqual(len(reported), 1, "Should have reported the write because something changed")
def test_merge_yaml_noChange(self): """ When merging YAML, if there's no change, don't report the file as having been written. """ tmpdir = FilePath(self.mktemp()) reported = [] obj = { '_id': 45, 'name': 'hi', 'guy': 'smiley', } dumper = RuleBasedFileDumper(tmpdir.path, reporter=reported.append) dumper.performAction({ 'merge_yaml': 'foo/bar/{_id}.yml', }, obj) reported.pop() dumper.performAction({ 'merge_yaml': 'foo/bar/{_id}.yml', }, obj) yml = tmpdir.child('foo').child('bar').child('45.yml') self.assertTrue(yml.exists(), "Should have created the file") content = yaml.safe_load(yml.getContent()) self.assertEqual(content, obj, "Should have written out the YAML data.") self.assertEqual(len(reported), 0, "Should not have reported the write because nothing changed.")
def test_write_file_noChange(self): """ Should not write the file the second time if nothing changed. """ ppd = PPD() obj_id = ppd.addFile(StringIO('foo bar'), 'joe.txt', {'meta': 'data'}) obj = ppd.getObject(obj_id) tmpdir = FilePath(self.mktemp()) reported = [] dumper = RuleBasedFileDumper(tmpdir.path, ppd=ppd, reporter=reported.append) dumper.performAction({ 'write_file': 'foo/bar/{filename}', }, obj) reported.pop() dumper.performAction({ 'write_file': 'foo/bar/{filename}', }, obj) self.assertEqual(len(reported), 0, "Should not have reported the write because nothing changed")
def test_write_file(self): """ You can write file contents out for file objects. """ ppd = PPD() obj_id = ppd.addFile(StringIO('foo bar'), 'joe.txt', {'meta': 'data'}) obj = ppd.getObject(obj_id) tmpdir = FilePath(self.mktemp()) reported = [] dumper = RuleBasedFileDumper(tmpdir.path, ppd=ppd, reporter=reported.append) dumper.performAction({ 'write_file': 'foo/bar/{filename}', }, obj) exp = tmpdir.child('foo').child('bar').child('joe.txt') self.assertTrue(exp.exists(), "Should make the file") self.assertEqual(exp.getContent(), 'foo bar') self.assertEqual(len(reported), 1, "Should have reported the write because something changed")
def test_merge_yaml_firstTime(self): """ You can write the object as YAML to a file. """ tmpdir = FilePath(self.mktemp()) reported = [] obj = { '_id': 45, 'name': 'hi', 'guy': 'smiley', } dumper = RuleBasedFileDumper(tmpdir.path, reporter=reported.append) dumper.performAction({ 'merge_yaml': 'foo/bar/{_id}.yml', }, obj) yml = tmpdir.child('foo').child('bar').child('45.yml') self.assertTrue(yml.exists(), "Should have created the file") content = yaml.safe_load(yml.getContent()) self.assertEqual(content, obj, "Should have written out the YAML data.") self.assertEqual(len(reported), 1, "Should have reported the write")