"""Tests the sql connector.""" import os.path from collections.abc import Generator from unittest.mock import Mock, patch import pytest from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider from aki_prj23_transparenzregister.config.config_template import PostgreConnectionString from aki_prj23_transparenzregister.utils.sql.connector import ( get_engine, get_session, init_db, ) def test_get_engine_pg() -> None: """Tests the creation of a postgre engine.""" conn_args = PostgreConnectionString("", "", "", "", 42) with patch( "aki_prj23_transparenzregister.utils.sql.connector.sa.create_engine" ) as mock_create_engine: result = "someThing" mock_create_engine.return_value = result assert get_engine(conn_args) == result def test_get_engine_fail() -> None: """Tests what happens if the wrong type is given to the engine factory.""" with pytest.raises(TypeError, match="The type of the configuration is invalid."): get_engine(None) # type: ignore @pytest.fixture() def delete_sqlite_table() -> Generator[str, None, None]: """Cleans a path before and deletes the table after a test. Returns: The path where the sqlite table is placed. """ sqlite_test_path = "test_db.db" if os.path.exists(sqlite_test_path): os.remove(sqlite_test_path) yield sqlite_test_path if os.path.exists(sqlite_test_path): os.remove(sqlite_test_path) def test_init_pd_db() -> None: """Tests if a pg sql database can be connected and initiated to.""" with patch( "aki_prj23_transparenzregister.utils.sql.connector.get_engine" ) as mock_get_engine, patch( "aki_prj23_transparenzregister.utils.sql.connector.declarative_base" ) as mock_declarative_base: mock_get_engine.connect.return_value = {} mock_value = Mock() mock_value.metadata.create_all.return_value = None mock_declarative_base.return_value = mock_value mock_value = Mock(spec=JsonFileConfigProvider) mock_value.get_sql_connection_string.return_value = "" init_db(get_session(mock_value))