Skip to content

Commit

Permalink
feat: Add some resource "__exists" checks (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
streino authored Oct 21, 2024
1 parent c463038 commit 15379e2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
8 changes: 8 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@ class ResourceRow(TypedDict):
dataset_id: str
resource_id: str
title: str
title__exists: bool
description: str
description__exists: bool
type: str
type__exists: bool
format: str | None
format__exists: bool
url: str
latest: str
checksum: dict
Expand All @@ -234,9 +238,13 @@ def from_payload(cls, dataset_id: str, payload: dict) -> ResourceRow | None:
dataset_id=dataset_id,
resource_id=payload["id"],
title=payload["title"],
title__exists=payload["title"] not in (None, ""),
description=payload["description"],
description__exists=payload["description"] not in (None, ""),
type=payload["type"],
type__exists=payload["type"] not in (None, ""),
format=payload["format"],
format__exists=payload["format"] not in (None, ""),
url=payload["url"],
latest=payload["latest"],
checksum=payload["checksum"] or {},
Expand Down
37 changes: 37 additions & 0 deletions tests/fixtures/resource_payload_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "71def345-0316-41f3-9695-06b02b14deef",
"title": "Téléchargement simple (Atom) du jeu et des documents associés via internet",
"description": "",
"filetype": "remote",
"type": "main",
"format": null,
"url": "https://atom.geo-ide.developpement-durable.gouv.fr/atomArchive/GetResource?id=5857d2b2-e5d5-427a-a0cc-7ba46d8c9f31&dataType=dataset",
"latest": "https://demo.data.gouv.fr/fr/datasets/r/71def345-0316-41f3-9695-06b02b14deef",
"checksum": null,
"filesize": null,
"mime": null,
"created_at": "2024-10-08T03:38:02.301000+00:00",
"last_modified": "2024-10-08T03:38:02.301000+00:00",
"metrics": {},
"harvest": {},
"extras": {
"check:available": false,
"check:timeout": true,
"check:date": "2024-10-09T14:30:55.778987+00:00",
"dcat": {
"accessRights": [
"Pas de restriction d'accès public selon INSPIRE"
],
"license": [
"Licence Ouverte / Open Licence Version 2.0 https://www.etalab.gouv.fr/wp-content/uploads/2017/04/ETALAB-Licence-Ouverte-v2.0.pdf",
"Aucun des articles de la loi ne peut être invoqué pour justifier d'une restriction d'accès public."
]
}
},
"preview_url": null,
"schema": null,
"internal": {
"created_at_internal": "2024-10-08T03:38:02.301000+00:00",
"last_modified_internal": "2024-10-08T03:38:02.301000+00:00"
}
}
49 changes: 35 additions & 14 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import pytest

from models import BaseModel, Dataset
from models import BaseModel, Dataset, Resource


@pytest.fixture
def fixture_payload(request):
with open(f"tests/fixtures/{request.param}", "r") as file:
data = json.load(file)
return data


def test_base_model_get_attr_by_path_return_none_on_keyerror():
Expand Down Expand Up @@ -180,16 +187,9 @@ def test_base_model_get_consistent_temporal_coverage_no_dates():
assert base.get_consistent_temporal_coverage() is True


@pytest.fixture
def payload_ok():
with open("tests/fixtures/payload_ok.json", "r") as file:
data = json.load(file)

return data


def test_base_model_harvest_spread(payload_ok):
base = Dataset(payload_ok, prefix="test")
@pytest.mark.parametrize("fixture_payload", ["payload_ok.json"], indirect=["fixture_payload"])
def test_base_model_harvest_spread(fixture_payload):
base = Dataset(fixture_payload, prefix="test")

actual = base.to_row()

Expand All @@ -202,10 +202,11 @@ def test_base_model_harvest_spread(payload_ok):
assert actual | expected == actual


def test_base_model_harvest_spread_with_harvest_none(payload_ok):
@pytest.mark.parametrize("fixture_payload", ["payload_ok.json"], indirect=["fixture_payload"])
def test_base_model_harvest_spread_with_harvest_none(fixture_payload):
try:
payload_ok["harvest"] = None
payload_with_empty_harvest = payload_ok
fixture_payload["harvest"] = None
payload_with_empty_harvest = fixture_payload

base = Dataset(payload_with_empty_harvest, prefix="test")
base.to_row()
Expand All @@ -223,3 +224,23 @@ def test_base_model_get_license_title_find_key():
base = Dataset({}, prefix="test", licenses=[{"id": "foo", "title": "bar"}])

assert base.get_license_title("foo") == "bar"


@pytest.mark.parametrize(
"fixture_payload", ["resource_payload_ok.json"], indirect=["fixture_payload"]
)
def test_resource_model_indicators(fixture_payload):
actual = Resource.from_payload("fake-id", fixture_payload)

expected = {
"title": "Téléchargement simple (Atom) du jeu et des documents associés via internet",
"title__exists": True,
"description": "",
"description__exists": False,
"type": "main",
"type__exists": True,
"format": None,
"format__exists": False,
}

assert actual | expected == actual # type: ignore

0 comments on commit 15379e2

Please sign in to comment.