Skip to content

Commit

Permalink
Added tests for Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Sep 2, 2024
1 parent 9d9a99c commit db08380
Showing 1 changed file with 102 additions and 2 deletions.
104 changes: 102 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
from dapitains.app.app import create_app
from dapitains.app.ingest import store_catalog
from dapitains.metadata.xml_parser import parse

import uritemplate
import urllib

basedir = os.path.abspath(os.path.dirname(__file__))
BASE_URI = "http://localhost:5000"


@pytest.fixture
def app():
"""Fixture to create a new instance of the Flask app for testing."""
app = Flask(__name__)
app, db = create_app(app, base_uri="http://localhost:5000")
app, db = create_app(app, base_uri=BASE_URI)
db_path = os.path.join(basedir, 'app.db')
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Expand Down Expand Up @@ -57,3 +59,101 @@ def test_index(client):
'document': 'http://localhost:5000/document/{?resource}{&ref,start,end,tree}',
'navigation': 'http://localhost:5000/navigation/{?resource}{&ref,start,end,tree,down}',
}


def test_collection(client):
response = client.get('/')
template = uritemplate.URITemplate(response.get_json()["collection"].replace(BASE_URI, ""))
response = client.get(template.expand({}))
j = response.get_json()
assert {
'@context': 'https://distributed-text-services.github.io/specifications/context/1-alpha1.json',
'@id': 'https://foo.bar/default',
'@type': 'Collection',
'collection': 'http://localhost:5000/collection/{?id,nav}',
'dtsVersion': '1-alpha',
'dublinCore': {'abstract': ['This is a perfect example of an absract.',
{'lang': 'fr',
'value': 'Et je peux traduire en français'}]},
'member': [{'@id': 'https://example.org/collection1',
'@type': 'Collection',
'collection': 'http://localhost:5000/collection/?id=https%3A%2F%2Fexample.org%2Fcollection1',
'dublinCore': {'creator': ['John Doe'],
'date': ['2023-08-24'],
'subject': ['History']},
'title': 'My First Collection',
'totalChildren': 3,
'totalParents': 0},
{'@id': 'https://example.org/resource1',
'@type': 'Resource',
'citationTrees': [{'@type': 'CitationTree',
'citeType': 'book',
'identifier': 'nums'},
{'@type': 'CitationTree',
'citeType': 'book',
'identifier': 'alpha'}],
'collection': 'http://localhost:5000/collection/?id=https%3A%2F%2Fexample.org%2Fresource1',
'description': 'A document about historical events.',
'document': 'http://localhost:5000/document/?resource=https%3A%2F%2Fexample.org'
'%2Fresource1{&ref,start,end,tree}',
'dublinCore': {'language': ['en'], 'subject': ['World War II']},
'navigation': 'http://localhost:5000/navigation/?resource=https%3A%2F%2Fexample.org'
'%2Fresource1{&ref,start,end,tree,down}',
'title': 'Historical Document',
'totalChildren': 3,
'totalParents': 0},
{'@id': 'https://foo.bar/text',
'@type': 'Resource',
'citationTrees': [{'@type': 'CitationTree',
'citeStructure': [{'citeStructure': [{'citeType': 'verse'},
{'citeType': 'bloup'}],
'citeType': 'chapter'}],
'citeType': 'book',
'identifier': 'default'}],
'collection': 'http://localhost:5000/collection/?id=https%3A%2F%2Ffoo.bar%2Ftext',
'description': 'With a description',
'document': 'http://localhost:5000/document/?resource=https%3A%2F%2Ffoo.bar%2Ftext{&ref,'
'start,end,tree}',
'dublinCore': {'title': ['A simple resource']},
'navigation': 'http://localhost:5000/navigation/?resource=https%3A%2F%2Ffoo.bar%2Ftext{'
'&ref,start,end,tree,down}',
'title': 'A simple resource',
'totalChildren': 3,
'totalParents': 0}],
'title': 'A collection',
'totalChildren': 3,
'totalParents': 0} == j
assert uritemplate.URITemplate(j["member"][0]["collection"]).expand(
{"id": j["member"][0]["@id"]}) == f"{BASE_URI}/collection/?id={urllib.parse.quote_plus(j['member'][0]['@id'])}"
collection = uritemplate.URITemplate(j["member"][0]["collection"]).expand(
{"id": j["member"][0]["@id"]})
response = client.get(collection.replace(BASE_URI, ""))
assert {'@context': 'https://distributed-text-services.github.io/specifications/context/1-alpha1.json',
'@id': 'https://example.org/collection1',
'@type': 'Collection',
'collection': 'http://localhost:5000/collection/{?id,nav}',
'dtsVersion': '1-alpha',
'dublinCore': {'creator': ['John Doe'],
'date': ['2023-08-24'],
'subject': ['History']},
'member': [{'@id': 'https://example.org/resource1',
'@type': 'Resource',
'citationTrees': [{'@type': 'CitationTree',
'citeType': 'book',
'identifier': 'nums'},
{'@type': 'CitationTree',
'citeType': 'book',
'identifier': 'alpha'}],
'collection': 'http://localhost:5000/collection/?id=https%3A%2F%2Fexample.org%2Fresource1',
'description': 'A document about historical events.',
'document': 'http://localhost:5000/document/?resource=https%3A%2F%2Fexample.org%2Fresource1{'
'&ref,start,end,tree}',
'dublinCore': {'language': ['en'], 'subject': ['World War II']},
'navigation': 'http://localhost:5000/navigation/?resource=https%3A%2F%2Fexample.org'
'%2Fresource1{&ref,start,end,tree,down}',
'title': 'Historical Document',
'totalChildren': 1,
'totalParents': 1}],
'title': 'My First Collection',
'totalChildren': 1,
'totalParents': 1} == response.get_json()

0 comments on commit db08380

Please sign in to comment.