Skip to content

Commit 74a4f00

Browse files
authored
Merge pull request #755 from tisnik/lcore-740-type-hints-for-rest-of-unit-tests
LCORE-740: type hints for rest of unit tests
2 parents b52ea28 + 56d8e06 commit 74a4f00

File tree

11 files changed

+273
-180
lines changed

11 files changed

+273
-180
lines changed

tests/unit/app/test_database.py

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
# pylint: disable=protected-access
44

5+
from typing import Generator
56
from pathlib import Path
67
import tempfile
78
import pytest
8-
from pytest_mock import MockerFixture
9+
from pytest_mock import MockerFixture, MockType
910
from sqlalchemy.engine.base import Engine
1011
from sqlalchemy.orm import Session
1112

@@ -14,7 +15,7 @@
1415

1516

1617
@pytest.fixture(name="reset_database_state")
17-
def reset_database_state_fixture():
18+
def reset_database_state_fixture() -> Generator:
1819
"""Reset global database state before and after tests."""
1920
original_engine = database.engine
2021
original_session_local = database.session_local
@@ -31,7 +32,7 @@ def reset_database_state_fixture():
3132

3233

3334
@pytest.fixture(name="base_postgres_config")
34-
def base_postgres_config_fixture():
35+
def base_postgres_config_fixture() -> PostgreSQLDatabaseConfiguration:
3536
"""Provide base PostgreSQL configuration for tests."""
3637
return PostgreSQLDatabaseConfiguration(
3738
host="localhost",
@@ -47,7 +48,7 @@ def base_postgres_config_fixture():
4748
class TestGetEngine:
4849
"""Test cases for get_engine function."""
4950

50-
def test_get_engine_when_initialized(self, mocker: MockerFixture):
51+
def test_get_engine_when_initialized(self, mocker: MockerFixture) -> None:
5152
"""Test get_engine returns engine when initialized."""
5253
mock_engine = mocker.MagicMock(spec=Engine)
5354
database.engine = mock_engine
@@ -56,7 +57,7 @@ def test_get_engine_when_initialized(self, mocker: MockerFixture):
5657

5758
assert result is mock_engine
5859

59-
def test_get_engine_when_not_initialized(self):
60+
def test_get_engine_when_not_initialized(self) -> None:
6061
"""Test get_engine raises RuntimeError when not initialized."""
6162
database.engine = None
6263

@@ -68,7 +69,7 @@ def test_get_engine_when_not_initialized(self):
6869
class TestGetSession:
6970
"""Test cases for get_session function."""
7071

71-
def test_get_session_when_initialized(self, mocker: MockerFixture):
72+
def test_get_session_when_initialized(self, mocker: MockerFixture) -> None:
7273
"""Test get_session returns session when initialized."""
7374
mock_session_local = mocker.MagicMock()
7475
mock_session = mocker.MagicMock(spec=Session)
@@ -80,7 +81,7 @@ def test_get_session_when_initialized(self, mocker: MockerFixture):
8081
assert result is mock_session
8182
mock_session_local.assert_called_once()
8283

83-
def test_get_session_when_not_initialized(self):
84+
def test_get_session_when_not_initialized(self) -> None:
8485
"""Test get_session raises RuntimeError when not initialized."""
8586
database.session_local = None
8687

@@ -91,7 +92,7 @@ def test_get_session_when_not_initialized(self):
9192
class TestCreateTables:
9293
"""Test cases for create_tables function."""
9394

94-
def test_create_tables_success(self, mocker: MockerFixture):
95+
def test_create_tables_success(self, mocker: MockerFixture) -> None:
9596
"""Test create_tables calls Base.metadata.create_all with engine."""
9697
mock_base = mocker.patch("app.database.Base")
9798
mock_get_engine = mocker.patch("app.database.get_engine")
@@ -103,7 +104,9 @@ def test_create_tables_success(self, mocker: MockerFixture):
103104
mock_get_engine.assert_called_once()
104105
mock_base.metadata.create_all.assert_called_once_with(mock_engine)
105106

106-
def test_create_tables_when_engine_not_initialized(self, mocker: MockerFixture):
107+
def test_create_tables_when_engine_not_initialized(
108+
self, mocker: MockerFixture
109+
) -> None:
107110
"""Test create_tables raises error when engine not initialized."""
108111
mock_get_engine = mocker.patch("app.database.get_engine")
109112
mock_get_engine.side_effect = RuntimeError("Database engine not initialized")
@@ -115,7 +118,7 @@ def test_create_tables_when_engine_not_initialized(self, mocker: MockerFixture):
115118
class TestCreateSqliteEngine:
116119
"""Test cases for _create_sqlite_engine function."""
117120

118-
def test_create_sqlite_engine_success(self):
121+
def test_create_sqlite_engine_success(self) -> None:
119122
"""Test _create_sqlite_engine creates engine successfully."""
120123
with tempfile.TemporaryDirectory() as temp_dir:
121124
db_path = Path(temp_dir) / "test.db"
@@ -126,7 +129,7 @@ def test_create_sqlite_engine_success(self):
126129
assert isinstance(engine, Engine)
127130
assert f"sqlite:///{db_path}" in str(engine.url)
128131

129-
def test_create_sqlite_engine_directory_not_exists(self):
132+
def test_create_sqlite_engine_directory_not_exists(self) -> None:
130133
"""Test _create_sqlite_engine raises error when directory doesn't exist."""
131134
config = SQLiteDatabaseConfiguration(db_path="/nonexistent/path/test.db")
132135

@@ -135,7 +138,7 @@ def test_create_sqlite_engine_directory_not_exists(self):
135138
):
136139
database._create_sqlite_engine(config)
137140

138-
def test_create_sqlite_engine_creation_failure(self, mocker: MockerFixture):
141+
def test_create_sqlite_engine_creation_failure(self, mocker: MockerFixture) -> None:
139142
"""Test _create_sqlite_engine handles engine creation failure."""
140143
mock_create_engine = mocker.patch("app.database.create_engine")
141144
with tempfile.TemporaryDirectory() as temp_dir:
@@ -151,8 +154,10 @@ class TestCreatePostgresEngine:
151154
"""Test cases for _create_postgres_engine function."""
152155

153156
def test_create_postgres_engine_success_default_schema(
154-
self, mocker: MockerFixture, base_postgres_config
155-
):
157+
self,
158+
mocker: MockerFixture,
159+
base_postgres_config: PostgreSQLDatabaseConfiguration,
160+
) -> None:
156161
"""Test _create_postgres_engine creates engine successfully with default schema."""
157162
mock_create_engine = mocker.patch("app.database.create_engine")
158163
mock_engine = mocker.MagicMock(spec=Engine)
@@ -171,8 +176,10 @@ def test_create_postgres_engine_success_default_schema(
171176
assert expected_url == call_args[0][0]
172177

173178
def test_create_postgres_engine_success_custom_schema(
174-
self, mocker: MockerFixture, base_postgres_config
175-
):
179+
self,
180+
mocker: MockerFixture,
181+
base_postgres_config: PostgreSQLDatabaseConfiguration,
182+
) -> None:
176183
"""Test _create_postgres_engine creates engine successfully with custom schema."""
177184
mock_create_engine = mocker.patch("app.database.create_engine")
178185
mock_engine = mocker.MagicMock(spec=Engine)
@@ -193,8 +200,10 @@ def test_create_postgres_engine_success_custom_schema(
193200
mock_connection.commit.assert_called_once()
194201

195202
def test_create_postgres_engine_with_ca_cert(
196-
self, mocker: MockerFixture, base_postgres_config
197-
):
203+
self,
204+
mocker: MockerFixture,
205+
base_postgres_config: PostgreSQLDatabaseConfiguration,
206+
) -> None:
198207
"""Test _create_postgres_engine with CA certificate path."""
199208
mock_create_engine = mocker.patch("app.database.create_engine")
200209
mock_engine = mocker.MagicMock(spec=Engine)
@@ -212,8 +221,10 @@ def test_create_postgres_engine_with_ca_cert(
212221
assert call_args[1]["connect_args"]["sslrootcert"] == cert_file.name
213222

214223
def test_create_postgres_engine_creation_failure(
215-
self, mocker: MockerFixture, base_postgres_config
216-
):
224+
self,
225+
mocker: MockerFixture,
226+
base_postgres_config: PostgreSQLDatabaseConfiguration,
227+
) -> None:
217228
"""Test _create_postgres_engine handles engine creation failure."""
218229
mock_create_engine = mocker.patch("app.database.create_engine")
219230
mock_create_engine.side_effect = Exception("Connection failed")
@@ -222,8 +233,10 @@ def test_create_postgres_engine_creation_failure(
222233
database._create_postgres_engine(base_postgres_config)
223234

224235
def test_create_postgres_engine_schema_creation_failure(
225-
self, mocker: MockerFixture, base_postgres_config
226-
):
236+
self,
237+
mocker: MockerFixture,
238+
base_postgres_config: PostgreSQLDatabaseConfiguration,
239+
) -> None:
227240
"""Test _create_postgres_engine handles schema creation failure."""
228241
mock_create_engine = mocker.patch("app.database.create_engine")
229242
mock_engine = mocker.MagicMock(spec=Engine)
@@ -246,10 +259,10 @@ def _setup_common_mocks(
246259
self,
247260
*,
248261
mocker: MockerFixture,
249-
mock_sessionmaker,
250-
mock_logger,
251-
enable_debug=False,
252-
):
262+
mock_sessionmaker: MockType,
263+
mock_logger: MockType,
264+
enable_debug: bool = False,
265+
) -> tuple[MockType, MockType]:
253266
"""Setup common mocks for initialize_database tests."""
254267
mock_engine = mocker.MagicMock(spec=Engine)
255268
mock_session_local = mocker.MagicMock()
@@ -258,8 +271,12 @@ def _setup_common_mocks(
258271
return mock_engine, mock_session_local
259272

260273
def _verify_common_assertions(
261-
self, *, mock_sessionmaker, mock_engine, mock_session_local
262-
):
274+
self,
275+
*,
276+
mock_sessionmaker: MockType,
277+
mock_engine: MockType,
278+
mock_session_local: MockType,
279+
) -> None:
263280
"""Verify common assertions for initialize_database tests."""
264281
mock_sessionmaker.assert_called_once_with(
265282
autocommit=False, autoflush=False, bind=mock_engine
@@ -270,7 +287,7 @@ def _verify_common_assertions(
270287
def test_initialize_database_sqlite(
271288
self,
272289
mocker: MockerFixture,
273-
):
290+
) -> None:
274291
"""Test initialize_database with SQLite configuration."""
275292
# Setup mocks
276293
mock_configuration = mocker.patch("app.database.configuration")
@@ -304,8 +321,8 @@ def test_initialize_database_sqlite(
304321
def test_initialize_database_postgres(
305322
self,
306323
mocker: MockerFixture,
307-
base_postgres_config,
308-
):
324+
base_postgres_config: PostgreSQLDatabaseConfiguration,
325+
) -> None:
309326
"""Test initialize_database with PostgreSQL configuration."""
310327
# Setup mocks
311328
mock_configuration = mocker.patch("app.database.configuration")

tests/unit/app/test_routers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Unit tests for routers.py."""
22

3-
from typing import Any, Optional
3+
from typing import Any, Optional, Sequence, Callable
44

55
from fastapi import FastAPI
66

@@ -37,14 +37,14 @@ def include_router( # pylint: disable=too-many-arguments
3737
router: Any,
3838
*,
3939
prefix: str = "",
40-
tags=None,
41-
dependencies=None,
42-
responses=None,
43-
deprecated=None,
44-
include_in_schema=None,
45-
default_response_class=None,
46-
callbacks=None,
47-
generate_unique_id_function=None,
40+
tags: Optional[list] = None,
41+
dependencies: Optional[Sequence] = None,
42+
responses: Optional[dict] = None,
43+
deprecated: Optional[bool] = None,
44+
include_in_schema: Optional[bool] = None,
45+
default_response_class: Optional[Any] = None,
46+
callbacks: Optional[list] = None,
47+
generate_unique_id_function: Optional[Callable] = None,
4848
) -> None:
4949
"""Register new router."""
5050
self.routers.append((router, prefix))

0 commit comments

Comments
 (0)