def setUp(self):
     self.cache = SQLiteCache(FILENAME, DATABASE_NAME, SCHEMA, 'first')
 def test_reload_and_destroy(self):
     newCache = SQLiteCache(FILENAME, DATABASE_NAME, SCHEMA, 'first')
     newCache.destroy()
     self.assertFalse(os.path.exists(newCache.filename))
class Test(unittest.TestCase):


    def setUp(self):
        self.cache = SQLiteCache(FILENAME, DATABASE_NAME, SCHEMA, 'first')

    def tearDown(self):
        self.cache.destroy()
        assert not os.path.exists(self.cache.filename)

    def test_reload_with_wrong_schema(self):
        # should raise an exception for an immutable sequence
        self.assertRaises(WrongSchema, SQLiteCache, FILENAME, DATABASE_NAME, SCHEMA[2:], 'first')

    def test_reload_and_destroy(self):
        newCache = SQLiteCache(FILENAME, DATABASE_NAME, SCHEMA, 'first')
        newCache.destroy()
        self.assertFalse(os.path.exists(newCache.filename))

    def test_insert_right_number_columns(self):
        self.record = (1, 2, 3, 'foo')
        self.assertTrue(self.cache.insert_record(self.record))
        self.assertTrue(self.cache.is_in(1))

    def test_insert(self):
        self.test_insert_right_number_columns()
        self.assertEqual(self.cache.get_all_records(), [self.record])

    def test_insert_wrong_number_columns(self):
        self.assertRaises(WrongNumberOfParameters,
                          self.cache.insert_record,
                          (1,2,3,'foo','123'))

    def test_set_wrong_key(self):
        self.assertRaises(NonexistentKey,
                          SQLiteCache,
                          FILENAME,
                          DATABASE_NAME,
                          SCHEMA,
                          'bad')

    def test_set_key(self):
        self.cache.key = 'first'

    def test_update_record_without_parameters(self):
        self.test_insert_right_number_columns()
        self.cache.key='first'
        self.assertRaises(WrongNumberOfParameters,self.cache.update_record,1)

    def test_update_record_with_too_much_parameters(self):
        self.test_insert_right_number_columns()
        self.cache.key='first'
        self.assertRaises(WrongNumberOfParameters,
                          self.cache.update_record,
                          1,
                          second=5,
                          third=10,
                          fourth='foo',
                          fifth='alpha')

    def test_update_record_with_wrong_parameters(self):
        self.test_insert_right_number_columns()
        self.cache.key='first'
        self.assertRaises(UnknownColumn,
                          self.cache.update_record,
                          1,
                          second=5,
                          third=10,
                          fifth='alpha')

    def test_update_record(self):
        self.test_insert_right_number_columns()
        self.cache.key='first'
        self.cache.update_record(1, second=5, third=10, fourth='alpha')
        self.assertIn((1, 5, 10, u'alpha'), self.cache.get_all_records())

    def test_update_part_of_record(self):
        self.test_insert_right_number_columns()
        self.cache.key='first'
        self.cache.update_record(1, fourth='alpha')
        self.assertIn((1, 2, 3, u'alpha'), self.cache.get_all_records())

    def test_get_non_existent_record(self):
        self.test_set_key()
        self.assertIsNone(self.cache.get_record(123))

    def test_delete_non_existent_record(self):
        self.test_set_key()
        self.assertTrue(self.cache.delete_record('foo'))

    def test_delete_record(self):
        self.test_set_key()
        self.test_insert_right_number_columns()
        self.assertTrue(self.cache.delete_record(1))
        self.assertIsNone(self.cache.get_record(1))
        self.test_emptyness()

    def test_delete_records(self):
        self.test_set_key()
        self.test_insert_right_number_columns()
        record2 = (5, 6, 7, 'foo')
        self.cache.insert_record(record2)
        self.cache.delete_records([1,5])
        self.test_emptyness()

    def test_emptyness(self):
        self.assertEqual(self.cache.get_all_records(),[],'DB should be empty')