|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import AsyncClient |
| 5 | +from sqlalchemy import select |
| 6 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 7 | + |
| 8 | +from data_rentgen.db.models import Dataset, Location |
| 9 | +from tests.fixtures.mocks import MockedUser |
| 10 | +from tests.test_server.utils.convert_to_json import dataset_to_json, tag_values_to_json |
| 11 | +from tests.test_server.utils.enrich import enrich_datasets |
| 12 | + |
| 13 | +pytestmark = [pytest.mark.server, pytest.mark.asyncio] |
| 14 | + |
| 15 | + |
| 16 | +async def test_get_datasets_by_location_id( |
| 17 | + test_client: AsyncClient, |
| 18 | + async_session: AsyncSession, |
| 19 | + datasets_search: tuple[dict[str, Dataset], ...], |
| 20 | + mocked_user: MockedUser, |
| 21 | +) -> None: |
| 22 | + _, _, datasets_by_address = datasets_search |
| 23 | + datasets = await enrich_datasets([datasets_by_address["hdfs://my-cluster-namenode:2080"]], async_session) |
| 24 | + location_id = datasets[0].location_id |
| 25 | + |
| 26 | + response = await test_client.get( |
| 27 | + "/v1/datasets", |
| 28 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 29 | + params={"location_id": location_id}, |
| 30 | + ) |
| 31 | + |
| 32 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 33 | + assert response.json() == { |
| 34 | + "meta": { |
| 35 | + "has_next": False, |
| 36 | + "has_previous": False, |
| 37 | + "next_page": None, |
| 38 | + "page": 1, |
| 39 | + "page_size": 20, |
| 40 | + "pages_count": 1, |
| 41 | + "previous_page": None, |
| 42 | + "total_count": 1, |
| 43 | + }, |
| 44 | + "items": [ |
| 45 | + { |
| 46 | + "id": str(dataset.id), |
| 47 | + "data": dataset_to_json(dataset), |
| 48 | + "tags": [], |
| 49 | + } |
| 50 | + for dataset in datasets |
| 51 | + ], |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +async def test_get_datasets_by_location_id_non_existent( |
| 56 | + test_client: AsyncClient, |
| 57 | + async_session: AsyncSession, |
| 58 | + datasets_search: tuple[dict[str, Dataset], ...], |
| 59 | + mocked_user: MockedUser, |
| 60 | +) -> None: |
| 61 | + response = await test_client.get( |
| 62 | + "/v1/datasets", |
| 63 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 64 | + params={"location_id": -1}, |
| 65 | + ) |
| 66 | + |
| 67 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 68 | + assert response.json() == { |
| 69 | + "meta": { |
| 70 | + "has_next": False, |
| 71 | + "has_previous": False, |
| 72 | + "next_page": None, |
| 73 | + "page": 1, |
| 74 | + "page_size": 20, |
| 75 | + "pages_count": 1, |
| 76 | + "previous_page": None, |
| 77 | + "total_count": 0, |
| 78 | + }, |
| 79 | + "items": [], |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +async def test_get_datasets_by_location_type( |
| 84 | + test_client: AsyncClient, |
| 85 | + async_session: AsyncSession, |
| 86 | + datasets_search: tuple[dict[str, Dataset], ...], |
| 87 | + mocked_user: MockedUser, |
| 88 | +) -> None: |
| 89 | + # random locations created by datasets_search fixture can also have type=hdfs |
| 90 | + datasets_query = ( |
| 91 | + select(Dataset) |
| 92 | + .join(Location, Location.id == Dataset.location_id) |
| 93 | + .where(Location.type == "hdfs") |
| 94 | + .order_by(Dataset.name) |
| 95 | + ) |
| 96 | + |
| 97 | + dataset_scalars = await async_session.scalars(datasets_query) |
| 98 | + async_session.expunge_all() |
| 99 | + |
| 100 | + datasets = await enrich_datasets(list(dataset_scalars.all()), async_session) |
| 101 | + |
| 102 | + response = await test_client.get( |
| 103 | + "/v1/datasets", |
| 104 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 105 | + params={"location_type": ["HDFS"]}, # case-insensitive |
| 106 | + ) |
| 107 | + |
| 108 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 109 | + assert response.json() == { |
| 110 | + "meta": { |
| 111 | + "has_next": False, |
| 112 | + "has_previous": False, |
| 113 | + "next_page": None, |
| 114 | + "page": 1, |
| 115 | + "page_size": 20, |
| 116 | + "pages_count": 1, |
| 117 | + "previous_page": None, |
| 118 | + "total_count": len(datasets), |
| 119 | + }, |
| 120 | + "items": [ |
| 121 | + { |
| 122 | + "id": str(dataset.id), |
| 123 | + "data": dataset_to_json(dataset), |
| 124 | + "tags": [], |
| 125 | + } |
| 126 | + for dataset in datasets |
| 127 | + ], |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +async def test_get_datasets_by_location_type_non_existent( |
| 132 | + test_client: AsyncClient, |
| 133 | + async_session: AsyncSession, |
| 134 | + datasets_search: tuple[dict[str, Dataset], ...], |
| 135 | + mocked_user: MockedUser, |
| 136 | +) -> None: |
| 137 | + response = await test_client.get( |
| 138 | + "/v1/datasets", |
| 139 | + headers={"Authorization": f"Bearer {mocked_user.access_token}"}, |
| 140 | + params={"location_type": "non_existent_location_type"}, |
| 141 | + ) |
| 142 | + |
| 143 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 144 | + assert response.json() == { |
| 145 | + "meta": { |
| 146 | + "has_next": False, |
| 147 | + "has_previous": False, |
| 148 | + "next_page": None, |
| 149 | + "page": 1, |
| 150 | + "page_size": 20, |
| 151 | + "pages_count": 1, |
| 152 | + "previous_page": None, |
| 153 | + "total_count": 0, |
| 154 | + }, |
| 155 | + "items": [], |
| 156 | + } |
0 commit comments