def test_gets_loaded_tables(self, database: Database) -> None: """Test get loaded tables.""" fake_loaded_tables: Dict[str, Optional[str]] = { "Broken Witt Rebels": "alternative", "Bonny Doon": None, "Jack White": "alternative", "Greta Van Fleet": None, "Tenacious D": "Rock", } database._loaded_tables = fake_loaded_tables expected: List[Dict[str, str]] = [ { "table_name": "Broken Witt Rebels", "benchmark": "alternative" }, { "table_name": "Jack White", "benchmark": "alternative" }, { "table_name": "Tenacious D", "benchmark": "Rock" }, ] received: List[Dict[str, str]] = database.get_loaded_tables() assert expected == received
def test_starts_unsuccessful_worker(self, database: Database) -> None: """Test start of unsuccessful worker.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.start.return_value = False database._worker_pool = mocked_worker_pool result: bool = database.start_worker() mocked_worker_pool.start.assert_called_once() assert type(result) is bool assert not result
def test_gets_worker_pool_status(self, database: Database) -> None: """Test return of worker pool status.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.get_status.return_value = "running" database._worker_pool = mocked_worker_pool result: str = database.get_worker_pool_status() assert type(result) is str assert result == "running"
def test_gets_worker_pool_queue_length(self, database: Database) -> None: """Test return of queue length from worker pool.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.get_queue_length.return_value = 42 database._worker_pool = mocked_worker_pool result: int = database.get_queue_length() assert type(result) is int assert result == 42
def test_closes_unsuccessful_worker(self, database: Database) -> None: """Test unsuccessful close of worker.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.close.return_value = False database._worker_pool = mocked_worker_pool result: bool = database.close_worker() mocked_worker_pool.close.assert_called_once() assert type(result) is bool assert not result
def test_deactivats_plugin_with_success(self, database: Database) -> None: """Test entry point for plug-in deactivation with success.""" mocked_background_scheduler: MagicMock = MagicMock() mocked_background_scheduler.deactivate_plugin.return_value = True fake_plugin: str = "Coolputer" database._background_scheduler = mocked_background_scheduler result: bool = database.deactivate_plugin(fake_plugin) mocked_background_scheduler.deactivate_plugin.assert_called_once_with( fake_plugin) assert type(result) is bool assert result
def test_gets_plugins_when_database_blocked(self, database: Database) -> None: """Test get plug-ins when database is blocked.""" database._database_blocked.value = True result: Optional[List] = database.get_plugins() assert not result
def test_gets_unblocked_database_status(self, database: Database) -> None: """Test return value for unblocked database.""" database._database_blocked.value = True result: bool = database.get_database_blocked() assert type(result) is bool assert result
def test_deletes_data_while_worker_pool_is_running( self, database: Database) -> None: """Test delete of data while worker pool is running.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.get_status.return_value = "running" mocked_background_scheduler: MagicMock = MagicMock() mocked_background_scheduler.delete_tables.return_value = True database._worker_pool = mocked_worker_pool database._background_scheduler = mocked_background_scheduler result: bool = database.delete_data(default_tables) mocked_worker_pool.get_status.assert_called_once() mocked_background_scheduler.delete_tables.assert_not_called() assert type(result) is bool assert not result
def test_gets_loaded_benchmarks_for_not_present_benchmarks( self, database: Database) -> None: """Test get loaded benchmark for not present benchmarks.""" fake_loaded_tables: Dict[str, Optional[str]] = { "The Dough Rollers": "alternative", "Broken Witt Rebels": "alternative", "Bonny Doon": None, "Jack White": "alternative", "Gary Clark Jr.": "Rock", "Greta Van Fleet": "Rock", "Tenacious D": "Rock", } database._loaded_tables = fake_loaded_tables expected = ["Rock"] results: List[str] = database.get_loaded_benchmarks() assert Counter(results) == Counter(expected)
def test_deletes_data_while_worker_pool_is_closed_and_load_table_is_successful( self, database: Database) -> None: """Test delete of data while worker pool is closed and background scheduler is returning true.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.get_status.return_value = "closed" mocked_background_scheduler: MagicMock = MagicMock() mocked_background_scheduler.delete_tables.return_value = True database._worker_pool = mocked_worker_pool database._background_scheduler = mocked_background_scheduler result: bool = database.delete_data(default_tables) mocked_worker_pool.get_status.assert_called_once() mocked_background_scheduler.delete_tables.assert_called_once_with( default_tables) assert type(result) is bool assert result
def test_loads_data_while_worker_pool_is_closed_and_load_table_failed( self, database: Database) -> None: """Test loading data while worker pool is running.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.get_status.return_value = "closed" mocked_background_scheduler: MagicMock = MagicMock() mocked_background_scheduler.load_tables.return_value = False database._worker_pool = mocked_worker_pool database._background_scheduler = mocked_background_scheduler result: bool = database.load_data(default_tables) mocked_worker_pool.get_status.assert_called_once() mocked_background_scheduler.load_tables.assert_called_once_with( default_tables) assert type(result) is bool assert not result
def test_closes_database(self, database: Database) -> None: """Test closing of database.""" mocked_worker_pool: MagicMock = MagicMock() mocked_worker_pool.terminate.return_value = None mocked_background_scheduler: MagicMock = MagicMock() mocked_background_scheduler.close.return_value = None mocked_connection_pool: MagicMock = MagicMock() mocked_connection_pool.closeall.return_value = None database._worker_pool = mocked_worker_pool database._background_scheduler = mocked_background_scheduler database._connection_pool = mocked_connection_pool database.close() mocked_worker_pool.terminate.assert_called_once() mocked_background_scheduler.close.assert_called_once() mocked_connection_pool.closeall.assert_called_once()
def test_gets_plugins_when_database_unblocked_and_plugins_exists( self, database: Database) -> None: """Test get existing plug-ins.""" database._database_blocked.value = False expected: List[str] = [ "Hildegunst von Mythenmetz", "Rumo von Zamonien" ] result: Optional[List] = database.get_plugins() assert type(result) is list assert Counter(result) == Counter(expected)
def test_gets_plugins_settings_when_database_blocked( self, database: Database) -> None: """Test get plug-ins settings when database is blocked.""" database._database_blocked.value = True result: Optional[List] = database.get_plugin_setting() global mocked_pool_cur mocked_pool_cur.execute.assert_not_called() assert not result reset_mocked_pool_cursor()
def test_set_plugin_settings_when_database_blocked( self, database: Database) -> None: """Test set plug-in setting while the database is blocked.""" database._database_blocked.value = True result: bool = database.set_plugin_setting("Eiskaltius", "M. böslich") global mocked_pool_cur mocked_pool_cur.execute.assert_not_called() assert type(result) is bool assert not result reset_mocked_pool_cursor()
def test_gets_plugins_when_database_unblocked_and_no_plugins_exists( self, database: Database) -> None: """Test get not existing plug-ins.""" database._database_blocked.value = False result: Optional[List] = database.get_plugins() global mocked_pool_cur mocked_pool_cur.execute.assert_called_once_with( ("SELECT name FROM meta_plugins;"), None) assert type(result) is list assert result == [] reset_mocked_pool_cursor()
def test_creates_empty_loaded_tables(self, database: Database) -> None: """Test creates empty loaded tables.""" expected_results: Dict[str, Optional[str]] = { "The Dough Rollers": None, "Broken Witt Rebels": None, "Bonny Doon": None, "Jack White": None, "Gary Clark Jr.": None, "Greta Van Fleet": None, "Tenacious D": None, } result: Dict[str, Optional[str]] = database.create_empty_loaded_tables() assert expected_results == result
def database(self) -> Database: """Get a new Database.""" return Database( database_id, database_user, database_password, database_host, database_port, database_name, number_workers, workload_publisher_url, default_tables, storage_host, storage_password, storage_port, storage_user, )
def test_sets_plugin_setting_when_database_is_unblocked( self, database: Database) -> None: """Test set plug-in setting while the database is not blocked.""" database._database_blocked.value = False result: bool = database.set_plugin_setting("M. böslich", "Eiskaltius") global mocked_pool_cur mocked_pool_cur.execute.assert_called_once_with( "UPDATE meta_settings SET value=%s WHERE name=%s;", ( "Eiskaltius", "M. böslich", ), ) assert type(result) is bool assert result reset_mocked_pool_cursor()
def test_gets_plugins_settings_when_database_unblocked_and_plugins_exists( self, database: Database) -> None: """Test get existing plug-ins settings.""" database._database_blocked.value = False expected_plugin_one: Dict[str, str] = { "name": "Hildegunst von Mythenmetz", "value": "Lindwurm", "description": "sprachliche Begabung", } expected_plugin_two: Dict[str, str] = { "name": "Rumo von Zamonien", "value": "Wolpertinger", "description": "gute Schachspieler und gute Kämpfer", } expected = [expected_plugin_one, expected_plugin_two] result: Optional[List] = database.get_plugin_setting() assert type(result) is list assert result[:] == expected[:] # type: ignore