|
| 1 | +import datetime |
| 2 | + |
| 3 | +from pytest_mock import MockerFixture |
| 4 | + |
| 5 | +from oc4ids_datastore_api.models import DatasetSQLModel |
| 6 | +from oc4ids_datastore_api.schemas import Dataset, Download, License, Publisher |
| 7 | +from oc4ids_datastore_api.services import get_all_datasets |
| 8 | + |
| 9 | + |
| 10 | +def test_get_all_datasets(mocker: MockerFixture) -> None: |
| 11 | + patch_fetch_all_datasets = mocker.patch( |
| 12 | + "oc4ids_datastore_api.services.fetch_all_datasets" |
| 13 | + ) |
| 14 | + now = datetime.datetime.now() |
| 15 | + dataset_sql_model = DatasetSQLModel( |
| 16 | + dataset_id="test_dataset", |
| 17 | + source_url="https://test-dataset.json", |
| 18 | + publisher_name="test_publisher", |
| 19 | + license_url="https://license.com", |
| 20 | + license_name="License", |
| 21 | + json_url="https://downloads/test_dataset.json", |
| 22 | + csv_url="https://downloads/test_dataset.csv", |
| 23 | + xlsx_url="https://downloads/test_dataset.xlsx", |
| 24 | + updated_at=now, |
| 25 | + ) |
| 26 | + patch_fetch_all_datasets.return_value = [dataset_sql_model] |
| 27 | + |
| 28 | + datasets = get_all_datasets() |
| 29 | + |
| 30 | + expected_dataset = Dataset( |
| 31 | + loaded_at=now, |
| 32 | + source_url="https://test-dataset.json", |
| 33 | + publisher=Publisher(name="test_publisher"), |
| 34 | + license=License(name="License", url="https://license.com"), |
| 35 | + downloads=[ |
| 36 | + Download(format="json", url="https://downloads/test_dataset.json"), |
| 37 | + Download(format="csv", url="https://downloads/test_dataset.csv"), |
| 38 | + Download(format="xlsx", url="https://downloads/test_dataset.xlsx"), |
| 39 | + ], |
| 40 | + ) |
| 41 | + assert datasets == [expected_dataset] |
| 42 | + |
| 43 | + |
| 44 | +def test_get_all_datasets_missing_download_formats(mocker: MockerFixture) -> None: |
| 45 | + patch_fetch_all_datasets = mocker.patch( |
| 46 | + "oc4ids_datastore_api.services.fetch_all_datasets" |
| 47 | + ) |
| 48 | + now = datetime.datetime.now() |
| 49 | + dataset_sql_model = DatasetSQLModel( |
| 50 | + dataset_id="test_dataset", |
| 51 | + source_url="https://test-dataset.json", |
| 52 | + publisher_name="test_publisher", |
| 53 | + license_url="https://license.com", |
| 54 | + license_name="License", |
| 55 | + json_url="https://downloads/test_dataset.json", |
| 56 | + csv_url=None, |
| 57 | + xlsx_url=None, |
| 58 | + updated_at=now, |
| 59 | + ) |
| 60 | + patch_fetch_all_datasets.return_value = [dataset_sql_model] |
| 61 | + |
| 62 | + datasets = get_all_datasets() |
| 63 | + |
| 64 | + expected_dataset = Dataset( |
| 65 | + loaded_at=now, |
| 66 | + source_url="https://test-dataset.json", |
| 67 | + publisher=Publisher(name="test_publisher"), |
| 68 | + license=License(name="License", url="https://license.com"), |
| 69 | + downloads=[ |
| 70 | + Download(format="json", url="https://downloads/test_dataset.json"), |
| 71 | + ], |
| 72 | + ) |
| 73 | + |
| 74 | + assert datasets == [expected_dataset] |
0 commit comments