# python import - http://docs.python.org/library/unittest.html import os, sys, unittest # add lib path to current python path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # atoidejouer import from atoidejouer.db import story from atoidejouer.tools import storage class TestDBStory(unittest.TestCase): def setUp(self): story.DB() def tearDown(self): story.DB()._del(all=True) def test_all(self): all = [r for r in story.DB().all()] self.assertEqual(len(all), 0, "should not have row! found: %s" % len(all)) def test_add(self): # second row key = story.Key(None, 'helo', 'image', 0, 1, 'helo.png') story.DB().add(key) all = [r for r in story.DB().all()] self.assertEqual(len(all), 1, "should have 1 row! found: %s" % len(all)) self.assertEqual(all[0], key, "not the same row: %s" % all[0]) # second row key = story.Key(None, 'hola', 'image', 0, 2, 'hola.png') story.DB().add(key) all = [r for r in story.DB().all()] self.assertEqual(len(all), 2, "should have 1 row! found: %s" % len(all)) self.assertEqual(all[1], key, "not the same row: %s" % all[1]) def test_get(self): key = story.Key(None, 'helo', 'image', 0, 1, 'helo.png') story.DB().add(key) all = [r for r in story.DB().get(story.Key(name='helo'))] self.assertEqual(len(all), 1, "should have 1 row! found: %s" % len(all)) self.assertEqual(all[0], key, "not the same row: %s" % all[0]) def test_update(self): key = story.Key(None, 'helo', 'image', 0, 1, 'helo.png') story.DB().add(key) all = [r for r in story.DB().all()] key = story.Key(id=all[0].id, name='hola', layer=2, media='hola.png') story.DB().update(key) all = [r for r in story.DB().all()] self.assertEqual(len(all), 1, "should have 1 row! found: %s" % len(all)) exp_key = story.Key(None, 'hola', 'image', 0, 2, 'hola.png') self.assertEqual(all[0], exp_key, "not the same row: %s" % all[0]) def suite(): suite = unittest.TestSuite() suite.addTest(TestDBStory('test_all')) suite.addTest(TestDBStory('test_add')) suite.addTest(TestDBStory('test_get')) suite.addTest(TestDBStory('test_update')) return suite if __name__ == '__main__': unittest.TextTestRunner(verbosity=1).run(suite())