|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import AsyncClient |
| 5 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 6 | + |
| 7 | +from data_rentgen.db.models import Job |
| 8 | +from tests.fixtures.mocks import MockedUser |
| 9 | +from tests.test_server.utils.convert_to_json import job_to_json |
| 10 | +from tests.test_server.utils.enrich import enrich_jobs |
| 11 | + |
| 12 | +pytestmark = [pytest.mark.server, pytest.mark.asyncio] |
| 13 | + |
| 14 | + |
| 15 | +async def test_get_jobs_by_location_id( |
| 16 | + test_client: AsyncClient, |
| 17 | + async_session: AsyncSession, |
| 18 | + jobs_with_locations_and_types: tuple[Job, ...], |
| 19 | + mocked_user: MockedUser, |
| 20 | +) -> None: |
| 21 | + jobs = await enrich_jobs(jobs_with_locations_and_types, async_session) |
| 22 | + |
| 23 | + # first job in jobs has a different location unlike two others |
| 24 | + [_, dag_job, task_job] = jobs |
| 25 | + response = await test_client.get( |
| 26 | + "/v1/jobs", |
| 27 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 28 | + params={"location_id": dag_job.location_id}, |
| 29 | + ) |
| 30 | + |
| 31 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 32 | + assert response.json() == { |
| 33 | + "meta": { |
| 34 | + "has_next": False, |
| 35 | + "has_previous": False, |
| 36 | + "next_page": None, |
| 37 | + "page": 1, |
| 38 | + "page_size": 20, |
| 39 | + "pages_count": 1, |
| 40 | + "previous_page": None, |
| 41 | + "total_count": 2, |
| 42 | + }, |
| 43 | + "items": [ |
| 44 | + { |
| 45 | + "id": str(job.id), |
| 46 | + "data": job_to_json(job), |
| 47 | + } |
| 48 | + for job in [dag_job, task_job] |
| 49 | + ], |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +async def test_get_jobs_by_location_id_non_existent( |
| 54 | + test_client: AsyncClient, |
| 55 | + async_session: AsyncSession, |
| 56 | + jobs_with_locations_and_types: tuple[Job, ...], |
| 57 | + mocked_user: MockedUser, |
| 58 | +): |
| 59 | + response = await test_client.get( |
| 60 | + "/v1/jobs", |
| 61 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 62 | + params={"location_id": -1}, |
| 63 | + ) |
| 64 | + |
| 65 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 66 | + assert response.json() == { |
| 67 | + "meta": { |
| 68 | + "has_next": False, |
| 69 | + "has_previous": False, |
| 70 | + "next_page": None, |
| 71 | + "page": 1, |
| 72 | + "page_size": 20, |
| 73 | + "pages_count": 1, |
| 74 | + "previous_page": None, |
| 75 | + "total_count": 0, |
| 76 | + }, |
| 77 | + "items": [], |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +async def test_get_lobs_by_location_type( |
| 82 | + test_client: AsyncClient, |
| 83 | + async_session: AsyncSession, |
| 84 | + jobs_with_locations_and_types: tuple[Job, ...], |
| 85 | + mocked_user: MockedUser, |
| 86 | +) -> None: |
| 87 | + spark_job, *_ = await enrich_jobs(jobs_with_locations_and_types, async_session) |
| 88 | + response = await test_client.get( |
| 89 | + "/v1/jobs", |
| 90 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 91 | + params={"location_type": ["YARN"]}, # case-insensitive |
| 92 | + ) |
| 93 | + |
| 94 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 95 | + assert response.json() == { |
| 96 | + "meta": { |
| 97 | + "has_next": False, |
| 98 | + "has_previous": False, |
| 99 | + "next_page": None, |
| 100 | + "page": 1, |
| 101 | + "page_size": 20, |
| 102 | + "pages_count": 1, |
| 103 | + "previous_page": None, |
| 104 | + "total_count": 1, |
| 105 | + }, |
| 106 | + "items": [ |
| 107 | + { |
| 108 | + "id": str(spark_job.id), |
| 109 | + "data": job_to_json(spark_job), |
| 110 | + }, |
| 111 | + ], |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +async def test_get_jobs_by_location_type_non_existent( |
| 116 | + test_client: AsyncClient, |
| 117 | + async_session: AsyncSession, |
| 118 | + jobs_with_locations_and_types: tuple[Job, ...], |
| 119 | + mocked_user: MockedUser, |
| 120 | +): |
| 121 | + response = await test_client.get( |
| 122 | + "/v1/jobs", |
| 123 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 124 | + params={"location_type": "non_existing_location_type"}, |
| 125 | + ) |
| 126 | + |
| 127 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 128 | + assert response.json() == { |
| 129 | + "meta": { |
| 130 | + "has_next": False, |
| 131 | + "has_previous": False, |
| 132 | + "next_page": None, |
| 133 | + "page": 1, |
| 134 | + "page_size": 20, |
| 135 | + "pages_count": 1, |
| 136 | + "previous_page": None, |
| 137 | + "total_count": 0, |
| 138 | + }, |
| 139 | + "items": [], |
| 140 | + } |
0 commit comments