From fed174a796f0a7494993bada2406a1639644fc5c Mon Sep 17 00:00:00 2001 From: Jeremy Nelson Date: Mon, 15 Nov 2021 13:59:02 -0700 Subject: [PATCH] Move rdf to fixtures, lint and typing updates --- conftest.py | 19 ++++++++++++ .../tasks/folio/mappings/bf_instance.py | 30 +++++++++---------- .../tasks/folio/mappings/bf_work.py | 16 +++++----- .../{tasks/folio/mappings => fixtures}/bf.ttl | 0 tests/tasks/folio/mappings/conftest.py | 16 ---------- .../tasks/folio/mappings/test_bf_instance.py | 22 ++++++++++---- tests/tasks/folio/mappings/test_bf_work.py | 15 ++++++---- 7 files changed, 69 insertions(+), 49 deletions(-) rename tests/{tasks/folio/mappings => fixtures}/bf.ttl (100%) delete mode 100644 tests/tasks/folio/mappings/conftest.py diff --git a/conftest.py b/conftest.py index 8fae185..6ea533e 100644 --- a/conftest.py +++ b/conftest.py @@ -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 diff --git a/ils_middleware/tasks/folio/mappings/bf_instance.py b/ils_middleware/tasks/folio/mappings/bf_instance.py index 5317dfd..8f524f4 100644 --- a/ils_middleware/tasks/folio/mappings/bf_instance.py +++ b/ils_middleware/tasks/folio/mappings/bf_instance.py @@ -1,6 +1,6 @@ date_of_publication = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?date WHERE {{ @@ -11,9 +11,9 @@ }} """ -identifier = """PREFIX bf: +identifier = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?identifier WHERE {{ @@ -26,7 +26,7 @@ instance_format_category = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?format_category WHERE {{ @@ -37,9 +37,9 @@ instance_format_term = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: -SELECT ?format_term +SELECT ?format_term WHERE {{ <{bf_instance}> a bf:Instance . <{bf_instance}> bf:carrier ?format_term . @@ -48,7 +48,7 @@ local_identifier = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?identifier WHERE {{ @@ -69,7 +69,7 @@ mode_of_issuance = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SElECT ?mode_of_issuance WHERE {{ @@ -80,7 +80,7 @@ note = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?note WHERE {{ @@ -93,7 +93,7 @@ physical_description_dimensions = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?dimensions WHERE {{ @@ -104,7 +104,7 @@ physical_description_extent = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?extent WHERE {{ @@ -117,7 +117,7 @@ place = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?place WHERE {{ @@ -131,7 +131,7 @@ publisher = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?publisher WHERE {{ @@ -146,7 +146,7 @@ title = """PREFIX bf: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?main_title ?subtitle ?part_number ?part_name WHERE {{ @@ -164,4 +164,4 @@ ?title bf:partName ?part_name }} }} -""" \ No newline at end of file +""" diff --git a/ils_middleware/tasks/folio/mappings/bf_work.py b/ils_middleware/tasks/folio/mappings/bf_work.py index 7637747..a73ab47 100644 --- a/ils_middleware/tasks/folio/mappings/bf_work.py +++ b/ils_middleware/tasks/folio/mappings/bf_work.py @@ -1,7 +1,7 @@ contributor = """PREFIX bf: PREFIX bflc: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?agent ?role WHERE {{ @@ -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: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?edition WHERE {{ @@ -29,7 +29,7 @@ instance_type_id = """PREFIX bf: -SELECT ?instance_type_id +SELECT ?instance_type_id WHERE {{ <{bf_work}> a bf:Work . <{bf_work}> bf:content ?instance_type . @@ -50,7 +50,7 @@ primary_contributor = """PREFIX bf: PREFIX bflc: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?agent ?role WHERE {{ @@ -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: PREFIX rdf: -PREFIX rdfs: +PREFIX rdfs: SELECT ?subject WHERE {{ @@ -77,4 +77,4 @@ ?subject_node rdfs:label ?subject . }} }} -""" \ No newline at end of file +""" diff --git a/tests/tasks/folio/mappings/bf.ttl b/tests/fixtures/bf.ttl similarity index 100% rename from tests/tasks/folio/mappings/bf.ttl rename to tests/fixtures/bf.ttl diff --git a/tests/tasks/folio/mappings/conftest.py b/tests/tasks/folio/mappings/conftest.py deleted file mode 100644 index b78bfed..0000000 --- a/tests/tasks/folio/mappings/conftest.py +++ /dev/null @@ -1,16 +0,0 @@ -import pathlib - -import pytest -import rdflib - -rdf_file = pathlib.Path(__file__).parent / "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 \ No newline at end of file diff --git a/tests/tasks/folio/mappings/test_bf_instance.py b/tests/tasks/folio/mappings/test_bf_instance.py index 1adc69c..1defcb0 100644 --- a/tests/tasks/folio/mappings/test_bf_instance.py +++ b/tests/tasks/folio/mappings/test_bf_instance.py @@ -1,10 +1,10 @@ - -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) @@ -12,6 +12,7 @@ def test_date_of_publication(test_graph: rdflib.Graph): assert str(dates[0]).startswith("2020") + def test_isbn(test_graph: rdflib.Graph): sparql = bf_instance_map.identifier.format( bf_instance=uri, bf_class="bf:Isbn" @@ -22,61 +23,71 @@ def test_isbn(test_graph: rdflib.Graph): 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_class="bf:Title") titles = [row for row in test_graph.query(sparql)] @@ -84,6 +95,7 @@ def test_main_title(test_graph: rdflib.Graph): 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_class="bf:ParallelTitle" diff --git a/tests/tasks/folio/mappings/test_bf_work.py b/tests/tasks/folio/mappings/test_bf_work.py index f9ab982..0046122 100644 --- a/tests/tasks/folio/mappings/test_bf_work.py +++ b/tests/tasks/folio/mappings/test_bf_work.py @@ -1,4 +1,4 @@ -import pytest +import pytest # noqa: F401 import rdflib import ils_middleware.tasks.folio.mappings.bf_work as bf_work_map @@ -6,15 +6,16 @@ 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) @@ -30,6 +31,7 @@ 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) @@ -37,8 +39,11 @@ def test_language(test_graph: rdflib.Graph): 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)] @@ -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 -