Coverage for src/tests/api/v1/portal/trainings/test_endpoints.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the portal API endpoints for trainings.""" 

2 

3import pytest 

4 

5from fastapi.testclient import TestClient 

6from kwai_core.domain.value_objects.period import Period 

7from kwai_core.domain.value_objects.timestamp import Timestamp 

8 

9 

10pytestmark = pytest.mark.api 

11 

12 

13def test_get_trainings(client: TestClient): 

14 """Test the endpoint for getting a list of trainings.""" 

15 response = client.get("/api/v1/portal/trainings") 

16 assert response.status_code == 200 

17 

18 

19async def test_get_trainings_with_date_filter( 

20 client: TestClient, make_training_in_db, make_training 

21): 

22 """Test the endpoint for getting a list of trainings.""" 

23 await make_training_in_db( 

24 make_training( 

25 period=Period.create_from_delta( 

26 Timestamp.create_from_string("2015-01-01 20:00:00"), hours=2 

27 ) 

28 ) 

29 ) 

30 response = client.get( 

31 "/api/v1/portal/trainings", 

32 params={ 

33 "filter[start]": "2015-01-01 00:00:00", 

34 "filter[end]": "2015-01-31 23:59:59", 

35 }, 

36 ) 

37 assert response.status_code == 200 

38 payload = response.json() 

39 assert payload["meta"]["count"] == 1, "There should be 1 training" 

40 

41 

42async def test_get_trainings_with_year_month( 

43 client: TestClient, make_training_in_db, make_training 

44): 

45 """Test the endpoint for getting a list of trainings.""" 

46 await make_training_in_db( 

47 make_training( 

48 period=Period.create_from_delta( 

49 Timestamp.create_from_string("2015-01-01 20:00:00"), hours=2 

50 ) 

51 ) 

52 ) 

53 response = client.get( 

54 "/api/v1/portal/trainings", 

55 params={ 

56 "filter[year]": "2015", 

57 "filter[month]": "01", 

58 }, 

59 ) 

60 assert response.status_code == 200 

61 payload = response.json() 

62 assert payload["meta"]["count"] == 1, "There should be 1 training"