Skip to content

Commit

Permalink
Remove code redundancy in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
candleindark committed Apr 29, 2024
1 parent 43013e7 commit b04c883
Showing 1 changed file with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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")

Expand Down Expand Up @@ -35,6 +41,10 @@ def populate_with_metadata(flask_app):
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 @@ -43,39 +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")
@pytest.mark.parametrize(
"url_metadata_id, expected_metadata",
[
(
1,
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"},
),
),
(
2,
URLMetadataModel(
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}],
),
),
],
)
def test_found(self, url_metadata_id, expected_metadata, flask_client):
@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 = populated_metadata[url_metadata_id - 1]

assert returned_metadata == expected_metadata

0 comments on commit b04c883

Please sign in to comment.