Coverage for tests\conftest.py: 100.00%

22 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-22 23:23 +0100

1# -*- coding: utf-8 -*- 

2 

3""" 

4* Name: interactive-clustering-gui/tests/conftest.py 

5* Description: Configuration for the pytest "app" test suite. 

6* Author: Erwan Schild 

7* Created: 13/12/2021 

8* Licence: CeCILL (https://cecill.info/licences.fr.html) 

9""" 

10 

11# ============================================================================== 

12# IMPORT PYTHON DEPENDENCIES 

13# ============================================================================== 

14 

15# from asgi_lifespan import LifespanManager # To call startup events. 

16import asyncio 

17from pathlib import Path 

18 

19import pytest 

20from httpx import AsyncClient 

21 

22from cognitivefactory.interactive_clustering_gui import app, backgroundtasks 

23 

24 

25# ============================================================================== 

26# async_client 

27# ============================================================================== 

28@pytest.fixture() 

29async def async_client(tmp_path: Path, monkeypatch): 

30 """ 

31 Provide an HTTPX asynchronous HTTP client. 

32 

33 Args: 

34 tmp_path: Pytest fixture: points to a temporary directory. 

35 monkeypatch: Pytest fixture: allows to monkeypatch objects. 

36 

37 Yields: 

38 An instance of AsyncClient using the FastAPI ASGI application. 

39 """ 

40 # Replace app.DATA_DIRECTORY with a temporary directory for the test. 

41 monkeypatch.setattr(app, "DATA_DIRECTORY", tmp_path) 

42 monkeypatch.setattr(app.backgroundtasks, "DATA_DIRECTORY", tmp_path) 

43 print(">> PYTEST TEMPORARY PATH:", tmp_path) 

44 

45 # Instanciate the application. 

46 # lifespan = LifespanManager(app) # To call startup events. 

47 httpx_client = AsyncClient( 

48 app=app.app, 

49 base_url="http://localhost", 

50 ) 

51 

52 # Return the application. 

53 # async with httpx_client as client, lifespan: # noqa: WPS316 

54 async with httpx_client as client: # noqa: WPS316 

55 yield client 

56 

57 

58# ============================================================================== 

59# set_fake_backgroundtasks 

60# ============================================================================== 

61@pytest.fixture() 

62async def fake_backgroundtasks(tmp_path: Path, monkeypatch): 

63 """ 

64 Patch the `backgroundtasks.DATA_DIRECTORY` of the worker. 

65 

66 Args: 

67 tmp_path: Pytest fixture: points to a temporary directory. 

68 monkeypatch: Pytest fixture: allows to monkeypatch objects. 

69 

70 Returns: 

71 The worker module. 

72 """ 

73 # Replace backgroundtasks.DATA_DIRECTORY with a temporary directory for the test. 

74 monkeypatch.setattr(backgroundtasks, "DATA_DIRECTORY", tmp_path) 

75 print(">> PYTEST TEMPORARY PATH:", tmp_path) 

76 

77 return backgroundtasks 

78 

79 

80# ============================================================================== 

81# event_loop 

82# ============================================================================== 

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

84def event_loop(): 

85 """ 

86 Replace the fixture used by pytest asyncio marker. 

87 

88 This is required because the default fixture has function scope, 

89 and our async client has session scope. Pytest wants both to have the same scope. 

90 Moreover, hypothesis asks to mark reusable fixtures as module or session scope. 

91 

92 Returns: 

93 An event loop. 

94 """ 

95 return asyncio.get_event_loop()