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_addObject(self): """ You can add an object. """ i = PPD() object_id = i.addObject({'foo': 'bar'}) obj = i.getObject(object_id) self.assertEqual(obj['foo'], 'bar') self.assertEqual(obj['_id'], object_id)
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_listObjects_all(self): """ You can list all objects. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) id2 = i.addObject({'hey': 'ho'}) objects = i.listObjects() obj1 = i.getObject(id1) obj2 = i.getObject(id2) self.assertEqual(objects, [obj1, obj2], "Should return both objects")
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_listObjects_globFilter_value(self): """ You can filter objects by glob pattern. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) i.addObject({'hey': 'ho'}) obj1 = i.getObject(id1) objects = i.listObjects({'foo': 'bar'}) self.assertEqual(objects, [obj1]) objects = i.listObjects({'foo': 'b*'}) self.assertEqual(objects, [obj1])
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")