Skip to content

Commit b26b5a6

Browse files
feat: don't include download formats with no URL
1 parent f3cecde commit b26b5a6

File tree

5 files changed

+103
-8
lines changed

5 files changed

+103
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ While the app is running, go to `http://127.0.0.1:8000/docs/`
3333
### Run linting and type checking
3434

3535
```
36-
black oc4ids_datastore_api/
37-
isort oc4ids_datastore_api/
38-
flake8 oc4ids_datastore_api/
39-
mypy oc4ids_datastore_api/
36+
black oc4ids_datastore_api/ tests/
37+
isort oc4ids_datastore_api/ tests/
38+
flake8 oc4ids_datastore_api/ tests/
39+
mypy oc4ids_datastore_api/ tests/
4040
```

oc4ids_datastore_api/services.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55

66
def _transform_dataset(dataset: DatasetSQLModel) -> Dataset:
7+
download_urls = {
8+
"json": dataset.json_url,
9+
"csv": dataset.csv_url,
10+
"xlsx": dataset.xlsx_url,
11+
}
712
downloads = [
8-
Download(format="json", url=(dataset.json_url or "")),
9-
Download(format="csv", url=(dataset.csv_url or "")),
10-
Download(format="xlsx", url=(dataset.xlsx_url or "")),
13+
Download(format=format, url=url) for format, url in download_urls.items() if url
1114
]
1215
return Dataset(
1316
loaded_at=dataset.updated_at,

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ dev = [
2020
"flake8",
2121
"Flake8-pyproject",
2222
"mypy",
23+
"pytest",
24+
"pytest-mock",
2325
]
2426

2527
[tool.isort]
@@ -30,3 +32,7 @@ max-line-length = 88
3032

3133
[tool.mypy]
3234
strict = true
35+
36+
[tool.pytest.ini_options]
37+
log_cli = true
38+
log_cli_level = "INFO"

requirements_dev.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ idna==3.10
5252
# anyio
5353
# email-validator
5454
# httpx
55+
iniconfig==2.0.0
56+
# via pytest
5557
isort==6.0.0
5658
# via oc4ids-datastore-api (pyproject.toml)
5759
jinja2==3.1.5
@@ -71,11 +73,15 @@ mypy-extensions==1.0.0
7173
# black
7274
# mypy
7375
packaging==24.2
74-
# via black
76+
# via
77+
# black
78+
# pytest
7579
pathspec==0.12.1
7680
# via black
7781
platformdirs==4.3.6
7882
# via black
83+
pluggy==1.5.0
84+
# via pytest
7985
psycopg2==2.9.10
8086
# via oc4ids-datastore-api (pyproject.toml)
8187
pycodestyle==2.12.1
@@ -90,6 +96,12 @@ pyflakes==3.2.0
9096
# via flake8
9197
pygments==2.19.1
9298
# via rich
99+
pytest==8.3.4
100+
# via
101+
# oc4ids-datastore-api (pyproject.toml)
102+
# pytest-mock
103+
pytest-mock==3.14.0
104+
# via oc4ids-datastore-api (pyproject.toml)
93105
python-dotenv==1.0.1
94106
# via uvicorn
95107
python-multipart==0.0.20

tests/test_services.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)