Coverage for tests/conftest.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-21 23:45 +0000

1import pathlib 

2import tempfile 

3import typing 

4 

5import faker 

6import pytest 

7from fastapi.testclient import TestClient 

8 

9from whole_app import views 

10from whole_app.settings import SETTINGS, StorageProviders 

11 

12 

13@pytest.fixture(scope="session") 

14def faker_obj() -> faker.Faker: 

15 return faker.Faker("ru_RU") 

16 

17 

18@pytest.fixture(autouse=True) 

19def patch_file_provider_for_temp( 

20 monkeypatch: typing.Any, 

21) -> typing.Generator[None, None, None]: 

22 """Patch settings, to rewrite dict path to temporary directory.""" 

23 with monkeypatch.context() as patcher, tempfile.TemporaryDirectory() as tmp_dir_name: 

24 yield patcher.setattr(SETTINGS, "dictionaries_path", pathlib.Path(tmp_dir_name)) 

25 

26 

27# pylint: disable=redefined-outer-name 

28@pytest.fixture 

29def app_client( 

30 monkeypatch: pytest.MonkeyPatch, 

31 faker_obj: typing.Any, 

32) -> typing.Generator[TestClient, None, None]: 

33 """Fake client with patched fake storage. 

34 

35 Also in a form of context manager it allow us to test startup events 

36 on every test. 

37 """ 

38 fake_api_key: typing.Final[str] = faker_obj.password() 

39 with TestClient(views.SPELL_APP) as local_client, monkeypatch.context() as patcher: 

40 patcher.setattr( 

41 SETTINGS, 

42 "dictionaries_storage_provider", 

43 StorageProviders.DUMMY, 

44 ) 

45 patcher.setattr(SETTINGS, "api_key", fake_api_key) 

46 local_client.headers.update({SETTINGS.api_key_header_name: fake_api_key}) 

47 yield local_client