Coverage for tests\test_get_api_projects_metadata.py: 100.00%

20 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/test_get_api_projects_metadata.py 

5* Description: Unittests for `app` module on the `GET /api/projects/{project_id}/metadata` route. 

6* Author: Erwan Schild 

7* Created: 22/02/2022 

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

9""" 

10 

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

12# IMPORT PYTHON DEPENDENCIES 

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

14 

15import json 

16 

17import pytest 

18 

19from tests.dummies_utils import create_dummy_projects 

20 

21# ============================================================================== 

22# test_ko_not_found 

23# ============================================================================== 

24 

25 

26@pytest.mark.asyncio() 

27async def test_ko_not_found(async_client): 

28 """ 

29 Test the `GET /api/projects/{project_id}/metadata` route with not existing project. 

30 

31 Arguments: 

32 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

33 """ 

34 # Assert HTTP client is created. 

35 assert async_client 

36 

37 # Assert route `GET /api/projects/{project_id}/metadata` works. 

38 response_get = await async_client.get(url="/api/projects/UNKNOWN_PROJECT/metadata") 

39 assert response_get.status_code == 404 

40 assert response_get.json() == { 

41 "detail": "The project with id 'UNKNOWN_PROJECT' doesn't exist.", 

42 } 

43 

44 

45# ============================================================================== 

46# test_ok 

47# ============================================================================== 

48 

49 

50@pytest.mark.asyncio() 

51async def test_ok(async_client, tmp_path): 

52 """ 

53 Test the `GET /api/projects/{project_id}/metadata` route with some projects. 

54 

55 Arguments: 

56 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

57 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

58 """ 

59 # Assert HTTP client is created. 

60 assert async_client 

61 

62 # Create dummy projects. 

63 create_dummy_projects( 

64 tmp_path=tmp_path, 

65 list_of_dummy_project_ids=[ 

66 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

67 ], 

68 ) 

69 

70 # Assert route `GET /api/projects/{project_id}/metadata` works. 

71 response_get = await async_client.get(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/metadata") 

72 assert response_get.status_code == 200 

73 assert list(response_get.json().keys()) == ["project_id", "metadata"] 

74 assert response_get.json() == { 

75 "project_id": "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

76 "metadata": { 

77 "project_id": "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

78 "project_name": "unittests", 

79 "creation_timestamp": 1657541236.556489, 

80 }, 

81 } 

82 

83 # Assert file content is the same. 

84 with open(tmp_path / "0a_INITIALIZATION_WITHOUT_MODELIZATION" / "metadata.json", "r") as metadata_fileobject: 

85 assert response_get.json()["metadata"] == json.load(metadata_fileobject)