def test_addObject(self): """ Adding an object should change last_updated """ ppd = PPD() self.fakeLastUpdated(ppd, 12.2) ppd.addObject({'foo': 'bar'}) self.assertNotEqual(ppd.last_updated(), 12.2)
def test_deleteObject(self): """ Deleting an object is an update """ ppd = PPD() ppd.addObject({'foo': 'bar'}) self.fakeLastUpdated(ppd, 12.2) ppd.deleteObject(0) self.assertNotEqual(ppd.last_updated(), 12.2)
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_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_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_listObjects_id(self): """ You can just list object ids. """ i = PPD() id1 = i.addObject({'foo': 'bar'}) id2 = i.addObject({'hey': 'ho'}) objects = i.listObjects(id_only=True) self.assertEqual(objects, [id1, id2]) objects = i.listObjects({'foo': 'bar'}, id_only=True) self.assertEqual(objects, [id1]) objects = i.listObjects({'foo': 'b*'}, id_only=True) self.assertEqual(objects, [id1])
def test_listObjects_anchor(self): """ You can list objects anchored by certain keys. """ i = PPD() data = [ {'unrelated': 'object'}, {'host': 'foo.com', 'state': 'up'}, {'host': 'foo.com', 'location': 'Africa'}, {'host': 'foo.com', 'port': '100', 'state': 'closed'}, {'host': 'foo.com', 'port': '100', 'state': 'open'}, {'host': 'foo.com', 'port': '200', 'state': 'closed'}, {'host': 'bar.com', 'state': 'down'}, ] for d in data: i.addObject(d) objects = i.listObjects(anchors=['host']) self.assertEqual(len(objects), 2) foo = objects[0] bar = objects[1] self.assertEqual(foo['host'], 'foo.com') self.assertEqual(foo['state'], 'closed') self.assertEqual(foo['location'], 'Africa') self.assertEqual(foo['port'], '200') self.assertEqual(bar['host'], 'bar.com') self.assertEqual(bar['state'], 'down') objects = i.listObjects(anchors=['host', 'port']) self.assertEqual(len(objects), 4) foo = objects[0] self.assertEqual(foo['host'], 'foo.com') self.assertEqual(foo['state'], 'up') self.assertEqual(foo['location'], 'Africa') foo_port100 = objects[1] self.assertEqual(foo_port100['host'], 'foo.com') self.assertEqual(foo_port100['port'], '100') self.assertEqual(foo_port100['state'], 'open') foo_port200 = objects[2] self.assertEqual(foo_port200['host'], 'foo.com') self.assertEqual(foo_port200['port'], '200') self.assertEqual(foo_port200['state'], 'closed') bar = objects[3] self.assertEqual(bar['host'], 'bar.com')
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_deleteObject(self): """ You can delete objects by id """ i = PPD() id1 = i.addObject({'foo': 'bar'}) i.deleteObject(id1) objects = i.listObjects() self.assertEqual(len(objects), 0, "Should have deleted the object")
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")
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")
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")