-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d2a788
commit a9726d6
Showing
2 changed files
with
249 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import logging | ||
import pytest | ||
import requests | ||
import pandas as pd | ||
|
||
from dlme_airflow.drivers.iiif_json_v3 import IiifV3JsonSource | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class MockIIIFCollectionV2Response: | ||
@property | ||
def status_code(self): | ||
return 200 | ||
|
||
@staticmethod | ||
def json(): | ||
return { | ||
"manifests": [ | ||
{"@id": "https://collection.edu/iiif/p15795coll29:28/manifest.json"} | ||
] | ||
} | ||
|
||
|
||
class MockIIIFCollectionV3Response: | ||
@property | ||
def status_code(self): | ||
return 200 | ||
|
||
@staticmethod | ||
def json(): | ||
return { | ||
"items": [ | ||
{"id": "https://collection.edu/iiif/p15795coll29:28/manifest.json"} | ||
] | ||
} | ||
|
||
|
||
class MockIIIFManifestResponse: | ||
@property | ||
def status_code(self): | ||
return 200 | ||
|
||
@staticmethod | ||
def json(): | ||
return { | ||
"@context": "http://iiif.io/api/presentation/3/context.json", | ||
"id": "https://collection.edu/iiif/p15795coll29:28/manifest.json", | ||
"metadata": [ | ||
{ | ||
"label": { | ||
"en": ["Source"] | ||
}, | ||
"value": { | ||
"en": ["Rare Books and Special Collections Library"] | ||
}, | ||
}, | ||
{"label": {"en": ["Title (main)"]}, "value": {"en": ["A great title of the Middle East"]}}, | ||
{"label": {"en": ["Title (sub)"]}, "value": {"en": ["Subtitle 1"]}}, | ||
{"label": {"en": ["Title (sub)"]}, "value": {"en": ["Subtitle 2"]}}, | ||
{"label": {"en": ["Date Created"]}, "value": {"en": [["1974"]]}}, | ||
], | ||
"sequences": [ | ||
{ | ||
"canvases": [ | ||
{"images": [{"resource": {"format": "image/jpeg"}}]}, | ||
{"images": [{"resource": {"format": "image/jpeg"}}]}, | ||
] | ||
} | ||
], | ||
"description": ["A descriptive phrase", " with further elaboration "], | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def mock_response(monkeypatch): | ||
def mock_get(*args, **kwargs): | ||
if args[0].endswith("v2_collection.json"): | ||
return MockIIIFCollectionV2Response() | ||
if args[0].endswith("v3_collection.json"): | ||
return MockIIIFCollectionV3Response() | ||
if args[0].endswith("manifest.json"): | ||
return MockIIIFManifestResponse() | ||
return | ||
|
||
monkeypatch.setattr(requests, "get", mock_get) | ||
|
||
|
||
@pytest.fixture | ||
def iiif_test_v3_source(): | ||
metadata = { | ||
"fields": { | ||
"context": { | ||
"path": "@context", | ||
"optional": True, | ||
}, # a specified field with one value in the metadata | ||
"description_top": {"path": "description", "optional": True}, | ||
"iiif_format": { | ||
"path": "sequences..format" | ||
}, # a specified field with multiple values in the metadata | ||
"profile": {"path": "sequences..profile"}, # a missing required field | ||
"thumbnail": { | ||
"path": "thumbnail..@id", | ||
"optional": True, | ||
}, # missing optional field | ||
} | ||
} | ||
return IiifV3JsonSource( | ||
collection_url="http://iiif_v3_collection.json", metadata=metadata | ||
) | ||
|
||
|
||
def test_IiifJsonSource_initial(iiif_test_v3_source, mock_response): | ||
assert len(iiif_test_v3_source._manifest_urls) == 0 | ||
|
||
|
||
def test_IiifJsonSource_get_schema(iiif_test_v3_source, mock_response): | ||
iiif_test_v3_source._get_schema() | ||
assert ( | ||
iiif_test_v3_source._manifest_urls[0] | ||
== "https://collection.edu/iiif/p15795coll29:28/manifest.json" | ||
) | ||
|
||
|
||
def test_IiifJsonSource_read(iiif_test_v3_source, mock_response): | ||
iiif_df = iiif_test_v3_source.read() | ||
test_columns = [ | ||
"context", | ||
"description_top", | ||
"iiif_format", | ||
"source", | ||
"title-main", | ||
"title-sub", | ||
] | ||
assert all([a == b for a, b in zip(iiif_df.columns, test_columns)]) | ||
|
||
|
||
def test_IiifJsonSource_df(iiif_test_v3_source, mock_response): | ||
iiif_df = iiif_test_v3_source.read() | ||
print(iiif_df.to_json(orient="records")) | ||
test_df = pd.DataFrame( | ||
[ | ||
{ | ||
"context": "http://iiif.io/api/presentation/3/context.json", | ||
"description_top": ["A descriptive phrase", "with further elaboration"], | ||
"iiif_format": ["image/jpeg", "image/jpeg"], | ||
"source": ["Rare Books and Special Collections Library"], | ||
"title-main": ["A great title of the Middle East"], | ||
"title-sub": ["Subtitle 1", "Subtitle 2"], | ||
"date-created": ["1974"], | ||
} | ||
] | ||
) | ||
assert iiif_df.equals(test_df) | ||
|
||
|
||
def test_IiifJsonSource_logging(iiif_test_v3_source, mock_response, caplog): | ||
with caplog.at_level(logging.WARNING): | ||
iiif_test_v3_source.read() | ||
assert ( | ||
"https://collection.edu/iiif/p15795coll29:28/manifest.json missing required field: 'profile'; searched path: 'sequences..profile'" # noqa: E501 | ||
in caplog.text | ||
) | ||
assert "missing optional field" not in caplog.text | ||
|
||
with caplog.at_level(logging.DEBUG): | ||
iiif_test_v3_source.read() | ||
assert ( | ||
"https://collection.edu/iiif/p15795coll29:28/manifest.json missing optional field: 'thumbnail'; searched path: 'thumbnail..@id'" # noqa: E501 | ||
in caplog.text | ||
) | ||
|
||
|
||
# def test_wait(iiif_test_v3_source): | ||
# driver = IiifV3JsonSource("https://example.com/iiif/", wait=2) | ||
# assert driver, "IiifJsonSource constructor accepts wait parameter" | ||
|
||
|
||
def test_list_encode(iiif_test_v3_source, mock_response): | ||
iiif_df = iiif_test_v3_source.read() | ||
assert iiif_df["date-created"][0] == ["1974"] |