Skip to content

Commit

Permalink
Merge pull request datalad#362 from candleindark/bf-url-metadata-model
Browse files Browse the repository at this point in the history
BF: allows extracted metadata to be any type
  • Loading branch information
candleindark authored Apr 29, 2024
2 parents c8ea9b8 + b04c883 commit 5b1e274
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
4 changes: 3 additions & 1 deletion datalad_registry/blueprints/api/url_metadata/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from pydantic import BaseModel, Field, StrictStr


Expand Down Expand Up @@ -27,7 +29,7 @@ class URLMetadataModel(_URLMetadataRep):
dataset_version: StrictStr
extractor_version: StrictStr
extraction_parameter: dict
extracted_metadata: dict
extracted_metadata: Any

class Config:
orm_mode = True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
import pytest

from datalad_registry.blueprints.api.url_metadata import URLMetadataModel
from datalad_registry.models import RepoUrl, URLMetadata, db


@pytest.fixture
def populate_with_metadata(flask_app):
from datalad_registry.models import RepoUrl, URLMetadata, db
def populated_metadata(flask_app) -> list[URLMetadataModel]:
"""
Populate the database with URLMetadata instances.
:return: The list of URLMetadataModel instances representing the URLMetadata
instances is populated to the database.
"""

url = RepoUrl(url="https://example.com")
url_metadata = URLMetadata(
dataset_describe="abc",
dataset_version="cde",
extractor_name="complete-imagination",
extractor_version="0.1.0",
extraction_parameter={"a": 1, "b": 2},
extracted_metadata={"brave": "new world", "apple": "1984"},
url=url,
)

url_metadata_lst = [
URLMetadata(
dataset_describe="abc",
dataset_version="cde",
extractor_name="complete-imagination",
extractor_version="0.1.0",
extraction_parameter={"a": 1, "b": 2},
extracted_metadata={"brave": "new world", "apple": "1984"},
url=url,
),
URLMetadata(
dataset_describe="foo",
dataset_version="bar",
extractor_name="baz",
extractor_version="1.0.0",
extraction_parameter={"x": 10, "y": 20},
extracted_metadata=["a", 1, {"year": 1984}],
url=url,
),
]

with flask_app.app_context():
db.session.add(url)
db.session.add(url_metadata)
db.session.add_all(url_metadata_lst)
db.session.commit()

return [
URLMetadataModel.from_orm(url_metadata) for url_metadata in url_metadata_lst
]


class TestURLMetadata:
@pytest.mark.parametrize("url_metadata_id", [1, 2, 3, 60, 71, 100])
Expand All @@ -31,20 +53,13 @@ def test_not_found(self, flask_client, url_metadata_id):
resp = flask_client.get(f"/api/v2/url-metadata/{url_metadata_id}")
assert resp.status_code == 404

@pytest.mark.usefixtures("populate_with_metadata")
def test_found(self, flask_client):
resp = flask_client.get("/api/v2/url-metadata/1")
@pytest.mark.parametrize("url_metadata_id", [1, 2])
def test_found(self, url_metadata_id, populated_metadata, flask_client):
resp = flask_client.get(f"/api/v2/url-metadata/{url_metadata_id}")

assert resp.status_code == 200

returned_metadata = URLMetadataModel.parse_obj(resp.json)
expected_metadata = URLMetadataModel(
dataset_describe="abc",
dataset_version="cde",
extractor_name="complete-imagination",
extractor_version="0.1.0",
extraction_parameter={"a": 1, "b": 2},
extracted_metadata={"brave": "new world", "apple": "1984"},
)
expected_metadata = populated_metadata[url_metadata_id - 1]

assert returned_metadata == expected_metadata

0 comments on commit 5b1e274

Please sign in to comment.