def test_catchAll(self): """ Everything should match a catchall rule. """ tmpdir = FilePath(self.mktemp()) rules = [ { 'pattern': { 'foo': '*', }, 'actions': [ {'merge_yaml': '{foo}.yml'}, ] }, { 'pattern': 'all', 'actions': [ {'merge_yaml': 'extra.yml'}, ] } ] dumper = RuleBasedFileDumper(tmpdir.path, rules) dumper.dumpObject({ 'foo': 'thefoo', }) self.assertTrue(tmpdir.child('thefoo.yml').exists(), "Should have matched and acted on the first rule") dumper.dumpObject({ 'bar': 'hey', }) self.assertTrue(tmpdir.child('extra.yml').exists(), "Should have matched and acted on the second rule") self.assertEqual(len(tmpdir.children()), 2, "Should only have made " "the 2 expected files")
def test_multipleActions(self): """ Multiple actions can be specified for each rule. Each action should happen. """ tmpdir = FilePath(self.mktemp()) rules = [ { 'pattern': { 'foo': '*', }, 'actions': [ {'merge_yaml': '{foo}.yml'}, {'merge_yaml': '{foo}2.yml'}, ] }, ] dumper = RuleBasedFileDumper(tmpdir.path, rules) dumper.dumpObject({ 'foo': 'thefoo', }) self.assertTrue(tmpdir.child('thefoo.yml').exists(), "Should have matched and acted on the first rule first action") self.assertTrue(tmpdir.child('thefoo2.yml').exists(), "Should have matched and acted on the first rule second action")
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")