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_init_nested_directory(self): db_directory = self.directory + "other/directory/level/" # prepare DB dbdriver = DbDriver(db_directory=db_directory) dbdriver.create_table("test") dbdriver.dump_tables() # assert self.assertEqual(dbdriver.db_directory, db_directory) self.assertTrue(os.path.exists(db_directory)) self.assertTrue(os.path.exists(db_directory + "test.csv")) # clean up shutil.rmtree(self.directory) self.assertFalse(os.path.exists(db_directory))
def test_dump_tables(self): """ Unit test """ # arrange mock_table = MagicMock() mock_db = MagicMock() mock_db._DbDriver__read_only = False mock_db._DbDriver__tables = { "new_table": mock_table, "not_changed_table": None } mock_db._DbDriver__dropped_tables = ["old_table", "missing_table"] mock_db._filepath.side_effect = [ "./here/old_table.csv", "./here/missing_table.csv", "./here/new_table.csv" ] mock_os = MagicMock() mock_os.path.exists.side_effect = [True, False] mock_df = MagicMock() mock_table.to_df.return_value = mock_df # act with patch("pkwscraper.lib.dbdriver.os", mock_os): DbDriver.dump_tables(mock_db) # assert mock_db._filepath.assert_has_calls( [call("old_table"), call("missing_table"), call("new_table")]) mock_os.path.exists.assert_has_calls( [call("./here/old_table.csv"), call("./here/missing_table.csv")]) mock_os.remove.assert_called_once_with("./here/old_table.csv") mock_df.to_csv.assert_called_once_with("./here/new_table.csv", sep=";") self.assertListEqual(mock_db._DbDriver__dropped_tables, [])
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()