Skip to content

Commit

Permalink
Move rdf to fixtures, lint and typing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed Nov 15, 2021
1 parent ca2c791 commit fa3299a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 deletions.
19 changes: 19 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# placing this in the root allows the tests in the 'tests' directory to import the packages in the root, e.g. 'dags'
import pathlib

import pytest
import rdflib

rdf_file = pathlib.Path(__file__).parent / "tests" / "fixtures" / "bf.ttl"


@pytest.fixture
def test_graph():
graph = rdflib.Graph()
for ns in [
("bf", "http://id.loc.gov/ontologies/bibframe/"),
("bflc", "http://id.loc.gov/ontologies/bflc/"),
("sinopia", "http://sinopia.io/vocabulary/"),
]:
graph.namespace_manager.bind(ns[0], ns[1])
graph.parse(rdf_file, format="turtle")
return graph
30 changes: 15 additions & 15 deletions ils_middleware/tasks/folio/mappings/bf_instance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
date_of_publication = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?date
WHERE {{
Expand All @@ -11,9 +11,9 @@
}}
"""

identifier = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
identifier = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?identifier
WHERE {{
Expand All @@ -26,7 +26,7 @@

instance_format_category = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?format_category
WHERE {{
Expand All @@ -37,9 +37,9 @@

instance_format_term = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?format_term
SELECT ?format_term
WHERE {{
<{bf_instance}> a bf:Instance .
<{bf_instance}> bf:carrier ?format_term .
Expand All @@ -48,7 +48,7 @@

local_identifier = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?identifier
WHERE {{
Expand All @@ -69,7 +69,7 @@

mode_of_issuance = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SElECT ?mode_of_issuance
WHERE {{
Expand All @@ -80,7 +80,7 @@

note = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?note
WHERE {{
Expand All @@ -93,7 +93,7 @@

physical_description_dimensions = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?dimensions
WHERE {{
Expand All @@ -104,7 +104,7 @@

physical_description_extent = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?extent
WHERE {{
Expand All @@ -117,7 +117,7 @@

place = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?place
WHERE {{
Expand All @@ -131,7 +131,7 @@

publisher = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?publisher
WHERE {{
Expand All @@ -146,7 +146,7 @@

title = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?main_title ?subtitle ?part_number ?part_name
WHERE {{
Expand All @@ -164,4 +164,4 @@
?title bf:partName ?part_name
}}
}}
"""
"""
16 changes: 8 additions & 8 deletions ils_middleware/tasks/folio/mappings/bf_work.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contributor = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX bflc: <http://id.loc.gov/ontologies/bflc/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?agent ?role
WHERE {{
Expand All @@ -12,13 +12,13 @@
?role_uri rdfs:label ?role .
?contrib_bnode bf:agent ?agent_uri .
?agent_uri a {bf_class} .
?agent_uri rdfs:label ?agent .
?agent_uri rdfs:label ?agent .
}}
"""

editions = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?edition
WHERE {{
Expand All @@ -29,7 +29,7 @@

instance_type_id = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
SELECT ?instance_type_id
SELECT ?instance_type_id
WHERE {{
<{bf_work}> a bf:Work .
<{bf_work}> bf:content ?instance_type .
Expand All @@ -50,7 +50,7 @@
primary_contributor = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX bflc: <http://id.loc.gov/ontologies/bflc/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?agent ?role
WHERE {{
Expand All @@ -61,13 +61,13 @@
?role_uri rdfs:label ?role .
?contrib_bnode bf:agent ?agent_uri .
?agent_uri a {bf_class} .
?agent_uri rdfs:label ?agent .
?agent_uri rdfs:label ?agent .
}}
"""

subject = """PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject
WHERE {{
Expand All @@ -77,4 +77,4 @@
?subject_node rdfs:label ?subject .
}}
}}
"""
"""
File renamed without changes.
16 changes: 0 additions & 16 deletions tests/tasks/folio/mappings/conftest.py

This file was deleted.

35 changes: 24 additions & 11 deletions tests/tasks/folio/mappings/test_bf_instance.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,105 @@

import pytest
import pytest # noqa: F401
import rdflib

import ils_middleware.tasks.folio.mappings.bf_instance as bf_instance_map

uri = 'https://api.stage.sinopia.io/resource/b0319047-acd0-4f30-bd8b-98e6c1bac6b0'
uri = "https://api.stage.sinopia.io/resource/b0319047-acd0-4f30-bd8b-98e6c1bac6b0"


def test_date_of_publication(test_graph: rdflib.Graph):
sparql = bf_instance_map.date_of_publication.format(bf_instance=uri)
dates = [row[0] for row in test_graph.query(sparql)]

assert str(dates[0]).startswith("2020")


def test_isbn(test_graph: rdflib.Graph):
sparql = bf_instance_map.identifier.format(bf_instance=uri,
bf_identifier_class="bf:Isbn")
sparql = bf_instance_map.identifier.format(
bf_instance=uri, bf_identifier_class="bf:Isbn"
)

isbns = [row[0] for row in test_graph.query(sparql)]

assert str(isbns[0]).startswith("9788869694110")
assert str(isbns[1]).startswith("9788869694103")


def test_media_format(test_graph: rdflib.Graph):
sparql = bf_instance_map.instance_format_category.format(bf_instance=uri)
media_formats = [row[0] for row in test_graph.query(sparql)]

assert str(media_formats[0]).startswith("http://id.loc.gov/vocabulary/mediaTypes/c")


def test_carrier_term(test_graph: rdflib.Graph):
sparql = bf_instance_map.instance_format_term.format(bf_instance=uri)
terms = [row[0] for row in test_graph.query(sparql)]

assert str(terms[0]).startswith("http://id.loc.gov/vocabulary/carriers/cr")


def test_local_idenitifier(test_graph: rdflib.Graph):
sparql = bf_instance_map.local_identifier.format(bf_instance=uri)
local_idents = [row[0] for row in test_graph.query(sparql)]

assert str(local_idents[0]).startswith("1272909598")



def test_mode_of_issuance(test_graph: rdflib.Graph):
sparql = bf_instance_map.mode_of_issuance.format(bf_instance=uri)
modes = [row[0] for row in test_graph.query(sparql)]

assert str(modes[0]).startswith("http://id.loc.gov/vocabulary/issuance/mono")


def test_note(test_graph: rdflib.Graph):
sparql = bf_instance_map.note.format(bf_instance=uri)
notes = [row[0] for row in test_graph.query(sparql)]

assert len(str(notes[0])) == 50
assert len(str(notes[1])) == 90



def test_physical_description_dimensions(test_graph: rdflib.Graph):
sparql = bf_instance_map.physical_description_dimensions.format(bf_instance=uri)
dimensions = [row[0] for row in test_graph.query(sparql)]

assert str(dimensions[0]).startswith("30 cm by 15 cm")


def test_physical_description_extent(test_graph: rdflib.Graph):
sparql = bf_instance_map.physical_description_extent.format(bf_instance=uri)
extents = [row[0] for row in test_graph.query(sparql)]

assert str(extents[0]).startswith("1 online resource (128 pages)")


def test_place(test_graph: rdflib.Graph):
sparql = bf_instance_map.place.format(bf_instance=uri)
places = [row[0] for row in test_graph.query(sparql)]

assert str(places[0]).startswith("Venice (Italy)")


def test_publisher(test_graph: rdflib.Graph):
sparql = bf_instance_map.publisher.format(bf_instance=uri)
publishers = [row[0] for row in test_graph.query(sparql)]

assert str(publishers[0]).startswith("Edizioni Ca'Foscari")


def test_main_title(test_graph: rdflib.Graph):
sparql = bf_instance_map.title.format(bf_instance=uri,
bf_title_class="bf:Title")
sparql = bf_instance_map.title.format(bf_instance=uri, bf_title_class="bf:Title")
titles = [row for row in test_graph.query(sparql)]

assert str(titles[0][0]).startswith("Scrivere di Islam")
assert str(titles[0][1]).startswith("raccontere la diaspora")


def test_parallel_title(test_graph: rdflib.Graph):
sparql = bf_instance_map.title.format(bf_instance=uri,
bf_title_class="bf:ParallelTitle")
sparql = bf_instance_map.title.format(
bf_instance=uri, bf_title_class="bf:ParallelTitle"
)
titles = [row for row in test_graph.query(sparql)]

assert str(titles[0][0]).startswith("Writing about Islam")
Expand Down
15 changes: 10 additions & 5 deletions tests/tasks/folio/mappings/test_bf_work.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import pytest
import pytest # noqa: F401
import rdflib

import ils_middleware.tasks.folio.mappings.bf_work as bf_work_map


work_uri = "https://api.stage.sinopia.io/resource/c96d8b55-e0ac-48a5-9a9b-b0684758c99e"


def test_contributor_author_person(test_graph: rdflib.Graph):
sparql = bf_work_map.contributor.format(bf_work=work_uri,
bf_class="bf:Person")
sparql = bf_work_map.contributor.format(bf_work=work_uri, bf_class="bf:Person")

contributors = [row for row in test_graph.query(sparql)]

assert str(contributors[0][0]).startswith("Ramzanali Fazel, Shirin")
assert str(contributors[0][1]).startswith("Author")


def test_edition(test_graph: rdflib.Graph):
sparql = bf_work_map.editions.format(bf_work=work_uri)

Expand All @@ -30,15 +31,19 @@ def test_instance_type_id(test_graph: rdflib.Graph):

assert str(type_idents[0]).startswith("Text")


def test_language(test_graph: rdflib.Graph):
sparql = bf_work_map.language.format(bf_work=work_uri)

languages = [row[0] for row in test_graph.query(sparql)]

assert str(languages[0]).startswith("Italian")


def test_primary_contributor(test_graph: rdflib.Graph):
sparql = bf_work_map.primary_contributor.format(bf_work=work_uri, bf_class="bf:Person")
sparql = bf_work_map.primary_contributor.format(
bf_work=work_uri, bf_class="bf:Person"
)

primary_contributors = [row for row in test_graph.query(sparql)]

Expand All @@ -48,10 +53,10 @@ def test_primary_contributor(test_graph: rdflib.Graph):
assert str(primary_contributors[1][0]).startswith("Blow, C. Joe")
assert str(primary_contributors[1][1]).startswith("Author")


def test_subject(test_graph: rdflib.Graph):
sparql = bf_work_map.subject.format(bf_work=work_uri)

subjects = [row for row in test_graph.query(sparql)]

assert len(subjects) == 3

0 comments on commit fa3299a

Please sign in to comment.