def test_updateObject(self): """ Updating an object should change last_updated. """ ppd = PPD() ppd.addObject({'foo': 'bar'}) self.fakeLastUpdated(ppd, 12.2) ppd.updateObjects({'foo': 'baz'}) self.assertNotEqual(ppd.last_updated(), 12.2)
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_updateObjects_noChange(self): """ Matched objects should be returned whether they were updated or not. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) i.addObject({'hey': 'ho'}) objects = i.updateObjects({'foo': 'bar'}, {'foo': '*'}) self.assertEqual(len(objects), 1, "Should return matching objects") self.assertEqual(objects[0], { '_id': id1, 'foo': 'bar', })
def test_updateObjects(self): """ You can update all objects. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) id2 = i.addObject({'hey': 'ho'}) objects = i.updateObjects({'A': 'A'}) self.assertEqual(len(objects), 2, "Should return matched objects") self.assertEqual(objects[0], { '_id': id1, 'foo': 'bar', 'A': 'A', }) self.assertEqual(objects[1], { '_id': id2, 'hey': 'ho', 'A': 'A', })
def test_updateObjects_filter(self): """ You can update some objects. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) id2 = i.addObject({'hey': 'ho'}) objects = i.updateObjects({'A': 'A'}, {'foo': '*'}) self.assertEqual(len(objects), 1, "Should return matched objects") self.assertEqual(objects[0], { '_id': id1, 'foo': 'bar', 'A': 'A', }) objects = i.listObjects() self.assertEqual(objects[1], { '_id': id2, 'hey': 'ho', }, "Should have left other object alone")
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")