def test_addFile(self): """ Adding a file should update last_updated. """ ppd = PPD() self.fakeLastUpdated(ppd, 12.2) ppd.addFile(StringIO('foo'), 'jim.txt', {}) self.assertNotEqual(ppd.last_updated(), 12.2)
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_addFile_filenameFromMetadata(self): """ You can provide the filename in the metadata to override the default filename. """ i = PPD() fh = StringIO('\x00\x01Hey\xff') obj_id = i.addFile(fh, None, {'filename': 'something.exe'}) obj = i.getObject(obj_id) self.assertEqual(obj['filename'], 'something.exe')
def test_deleteFile(self): """ When you delete a file's metadata, the content is also deleted. """ i = PPD() fh = StringIO('\x00\x01Hey\xff') obj_id = i.addFile(fh, 'something.exe', {'hey': 'ho'}) obj = i.getObject(obj_id) file_id = obj['_file_id'] i.deleteObject(obj_id) self.assertRaises(KeyError, i.getFileContents, file_id)
def test_multipleUsers(self): """ Two instances of PPD using the same database should see the other guy's changes all the time. """ dbfile = self.mktemp() a = PPD(dbfile) b = PPD(dbfile) a.addObject({'foo': 'bar'}) self.assertEqual(len(b.listObjects()), 1, "Adding should be concurrent") a.updateObjects({'boo': 'hoo'}) self.assertEqual(len(b.listObjects({'boo': 'hoo'})), 1, "Updating should be concurrent") a.deleteObject(1) self.assertEqual(len(b.listObjects()), 0, "Deleting should be concurrent") a.addFile(StringIO('foo'), 'foo.txt', {}) self.assertEqual(len(b.listObjects()), 1, "Adding files should be concurrent")
def test_addFile_getFile(self): """ You can add a file and get the contents back. """ i = PPD() fh = StringIO('\x00\x01Hey\xff') obj_id = i.addFile(fh, 'something.exe', {'hey': 'ho'}) obj = i.getObject(obj_id) self.assertEqual(obj['filename'], 'something.exe') self.assertEqual(obj['hey'], 'ho') self.assertIn('_file_hash', obj, "Should include hash of file") contents = i.getFileContents(obj['_file_id']) self.assertEqual(contents, '\x00\x01Hey\xff', "Should return the contents provided when attaching the file" " not: %r" % (contents,))
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")
class PPD_autoDumpTest(TestCase): def setUp(self): self.tmpdir = FilePath(self.mktemp()) rules = [ { 'pattern': { 'foo': '*', }, 'actions': [ {'merge_yaml': '{foo}.yml'}, ], }, { 'pattern': { '_file_id': '*', }, 'actions': [ {'write_file': '{filename}'}, ] } ] self.reported = [] self.ppd = PPD(':memory:', RuleBasedFileDumper(self.tmpdir.path, rules=rules, reporter=self.reported.append), auto_dump=True) def test_addObject(self): """ If you add an object, and auto-dumping is enabled, it should dump. """ self.ppd.addObject({'foo': 'hey'}) self.assertTrue(self.tmpdir.child('hey.yml').exists(), "Should have run the rules") self.assertEqual(len(self.reported), 1, "Should have reported a change") def test_addFile(self): """ If you add a file, and auto-dumping is enabled, it should dump. """ self.ppd.addFile(StringIO('foo bar'), 'guys.txt', {'x': 'x'}) self.assertTrue(self.tmpdir.child('guys.txt').exists(), "Should have run the rules to create the file") self.assertEqual(len(self.reported), 1, "Should have reported the change") def test_updateObjects(self): """ If you update some objects, and auto-dumping is enabled, it should dump. """ self.ppd.addObject({'foo': 'hey'}) self.reported.pop() self.ppd.updateObjects({'foo': 'woo'}) self.assertTrue(self.tmpdir.child('woo.yml').exists(), "Should have run the rules") self.assertEqual(len(self.reported), 1, "Should have reported a change")