def test_whole(self): """ Main integration test """ # arrange record_1 = {"a": 1, "b": 2} record_2 = {"c": 8, "b": 4} # make and save db db = DbDriver(db_directory=self.directory) db.create_table("my_table") id_1 = db["my_table"].put(record_1) id_2 = db["my_table"].put(record_2) db.dump_tables() # open the db again db2 = DbDriver(db_directory=self.directory) my_table = db2["my_table"] records = my_table.find({}) self.assertEqual(len(records), 2) self.assertDictEqual(records[id_1], record_1) self.assertDictEqual(records[id_2], record_2) # remove db deleting_access_code = db2.get_deleting_access() deleting_access_code = deleting_access_code[43:53] db2.delete(deleting_access_code) assert not os.path.exists(self.directory)
def test_delete(self): # arrange self._make_synthetic_data() self.assertTrue(os.path.exists(self.directory)) dbdriver = DbDriver(db_directory=self.directory) dbdriver.create_table("foo") dbdriver.dump_tables() dbdriver.delete_table("foo") # act deleting_access_code = dbdriver.get_deleting_access() deleting_access_code = deleting_access_code[43:53] dbdriver.delete(deleting_access_code) # assert self.assertFalse(os.path.exists(self.directory))
def test_read_only_errors(self): # arrange self._make_synthetic_data() db = DbDriver(self.directory, read_only=True) # saving to harddrive with self.assertRaises(IOError) as e: db.dump_tables() self.assertEqual(e.exception.args[0], "DB is for reading only.") # adding table with self.assertRaises(IOError) as e: db.create_table("test_table") self.assertEqual(e.exception.args[0], "DB is for reading only.") # puting records with self.assertRaises(IOError) as e: db["first_table"].put({"c": 5}) self.assertEqual(e.exception.args[0], "Table is for read only.") # dropping table with self.assertRaises(IOError) as e: db.delete_table("first_table") self.assertEqual(e.exception.args[0], "DB is for reading only.") # obtaining deleting access code with self.assertRaises(IOError) as e: db.get_deleting_access() self.assertEqual(e.exception.args[0], "DB is for reading only.") # deleting db with self.assertRaises(IOError) as e: db.delete(access_code="something") self.assertEqual(e.exception.args[0], "DB is for reading only.") # clean up self._clean_synthetic_data()