From 07aab5f10a0dc33eb49e107815697c57022b1440 Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 10:22:48 +0200 Subject: [PATCH 1/7] chore: update gitignore & requirements --- .gitignore | 3 ++- requirements.txt | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5329840..c43ca93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ debug/* .venv +venv __pycache__/ -*.html \ No newline at end of file +*.html diff --git a/requirements.txt b/requirements.txt index f968a8b..21b9b9e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,15 @@ click==8.1.7 +iniconfig==2.0.0 isodate==0.6.1 Jinja2==3.1.3 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 +packaging==24.1 +pluggy==1.5.0 Pygments==2.17.2 pyparsing==3.1.1 +pytest==8.3.2 PyYAML==6.0.1 rdflib==7.0.0 rich==13.7.1 From 96590b1308c3441a969b748eb7c0d4d03d9e301e Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 10:54:50 +0200 Subject: [PATCH 2/7] feat: add test for sparql_query --- tests/__init__.py | 0 tests/test_sparql.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_sparql.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_sparql.py b/tests/test_sparql.py new file mode 100644 index 0000000..338a06c --- /dev/null +++ b/tests/test_sparql.py @@ -0,0 +1,33 @@ +import pytest +import rdflib + +from respecter.sparql import sparql_query, apply_sparql_query_file + + +SAMPLE_RDF = """ + a ; + "English" . + a ; + "French" . + + a ; + "English" . +""" + +SAMPLE_SPARQL = """ +SELECT ?subject WHERE { + ?subject a . + ?subject "English" . +} +""" + +def test_sparql_query(): + graph = rdflib.Graph() + graph.parse(data=SAMPLE_RDF, format="ttl") + query_results = sparql_query(graph, SAMPLE_SPARQL) + + bindings = query_results.get("results").get("bindings", []) + + assert len(bindings) == 1 + assert bindings[0].get("subject", {}).get("value", "") == "https://example.com/Alice" + From b7f4e172324c7277df519a5db13f5fc4b38707f6 Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 11:05:10 +0200 Subject: [PATCH 3/7] feat: add tests for apply_sparql_query_file --- tests/data/languages.ttl | 7 +++++++ tests/sparql/languages.sparql | 4 ++++ tests/test_sparql.py | 28 +++++++++++++++------------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 tests/data/languages.ttl create mode 100644 tests/sparql/languages.sparql diff --git a/tests/data/languages.ttl b/tests/data/languages.ttl new file mode 100644 index 0000000..062313a --- /dev/null +++ b/tests/data/languages.ttl @@ -0,0 +1,7 @@ + a ; + "English" . + a ; + "French" . + + a ; + "English" . diff --git a/tests/sparql/languages.sparql b/tests/sparql/languages.sparql new file mode 100644 index 0000000..f3b43e8 --- /dev/null +++ b/tests/sparql/languages.sparql @@ -0,0 +1,4 @@ +SELECT ?subject WHERE { + ?subject a . + ?subject "English" . +} diff --git a/tests/test_sparql.py b/tests/test_sparql.py index 338a06c..a5a5835 100644 --- a/tests/test_sparql.py +++ b/tests/test_sparql.py @@ -3,16 +3,8 @@ from respecter.sparql import sparql_query, apply_sparql_query_file - -SAMPLE_RDF = """ - a ; - "English" . - a ; - "French" . - - a ; - "English" . -""" +LANGUAGES_DATA_FILE_PATH = "tests/data/languages.ttl" +SPARQL_FILE_PATH = "tests/sparql/languages.sparql" SAMPLE_SPARQL = """ SELECT ?subject WHERE { @@ -21,13 +13,23 @@ } """ -def test_sparql_query(): +def load_graph(): graph = rdflib.Graph() - graph.parse(data=SAMPLE_RDF, format="ttl") + graph.parse(LANGUAGES_DATA_FILE_PATH, format="ttl") + return graph + +def test_sparql_query(): + graph = load_graph() query_results = sparql_query(graph, SAMPLE_SPARQL) - bindings = query_results.get("results").get("bindings", []) assert len(bindings) == 1 assert bindings[0].get("subject", {}).get("value", "") == "https://example.com/Alice" +def test_apply_sparql_query(): + graph = load_graph() + query_results = apply_sparql_query_file(graph, SPARQL_FILE_PATH) + bindings = query_results.get("results").get("bindings", []) + + assert len(bindings) == 1 + assert bindings[0].get("subject", {}).get("value", "") == "https://example.com/Alice" From ffbd45b172b0c9aa52ebc7dd226d738d5ac9b15f Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 11:25:20 +0200 Subject: [PATCH 4/7] feat: add tests for SparqlConfig class --- tests/config/sparql_config.yaml | 34 +++++++++++++++++++++++++++++++++ tests/test_sparql.py | 14 +++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/config/sparql_config.yaml diff --git a/tests/config/sparql_config.yaml b/tests/config/sparql_config.yaml new file mode 100644 index 0000000..68303c5 --- /dev/null +++ b/tests/config/sparql_config.yaml @@ -0,0 +1,34 @@ +ontology: # Replace with the actual ontology base URI + uri_base: "https://example.com" + separator: "/" + +type: + class: #the class type in the ontology (could also be skos:Concept, owl:Class, etc.) + property: + enumeration: + +predicate: + definition: + label: + example: + + +prefix: + sh: + rdf: + skos: + owl: + dct: + dcat: + vann: + schema: + sd: + bio: + spe: + rdfs: + xsd: + shsh: + dcterms: + ex: + md4i: + dpv: diff --git a/tests/test_sparql.py b/tests/test_sparql.py index a5a5835..c227571 100644 --- a/tests/test_sparql.py +++ b/tests/test_sparql.py @@ -1,10 +1,11 @@ import pytest import rdflib -from respecter.sparql import sparql_query, apply_sparql_query_file +from respecter.sparql import sparql_query, apply_sparql_query_file, SparqlConfig LANGUAGES_DATA_FILE_PATH = "tests/data/languages.ttl" SPARQL_FILE_PATH = "tests/sparql/languages.sparql" +SPARQL_CONFIG_FILE_PATH = "tests/config/sparql_config.yaml" SAMPLE_SPARQL = """ SELECT ?subject WHERE { @@ -33,3 +34,14 @@ def test_apply_sparql_query(): assert len(bindings) == 1 assert bindings[0].get("subject", {}).get("value", "") == "https://example.com/Alice" + +def test_load_sparql_config(): + sparql_config = SparqlConfig(SPARQL_CONFIG_FILE_PATH) + assert sparql_config.get_uri_base() == "https://example.com" + assert sparql_config.get_uri_separator() == "/" + assert sparql_config.get_type("class") == "" + assert sparql_config.get_type("property") == "" + assert sparql_config.get_type("enumeration") == "" + assert sparql_config.get_predicate("definition") == "" + assert sparql_config.get_predicate("example") == "" + assert sparql_config.get_predicate("label") == "" From f952570fdbf897e670b564b915408c26d0d82d22 Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 13:16:23 +0200 Subject: [PATCH 5/7] feat: add tests for helpers; refactor helpers; use pyproject.toml for pytest --- pyproject.toml | 4 ++++ respecter/helpers.py | 40 +++++++++++++++++++++------------------- tests/__init__.py | 0 tests/test_helpers.py | 15 +++++++++++++++ tests/test_sparql.py | 2 +- 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 pyproject.toml delete mode 100644 tests/__init__.py create mode 100644 tests/test_helpers.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0f67a3c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[tool.pytest.ini_options] +pythonpath = [ + "./respecter" +] diff --git a/respecter/helpers.py b/respecter/helpers.py index efabc48..8733033 100644 --- a/respecter/helpers.py +++ b/respecter/helpers.py @@ -23,28 +23,30 @@ def format_properties(properties): return [property.to_dict() for property in properties.values()] +def format_uri_value(value, qname=None, current_ontology_url=None): + if current_ontology_url and value.startswith(current_ontology_url): + value_uri = value.replace(current_ontology_url, "#") + else: + value_uri = value + if qname: + value_string = qname(value) + else: + value_string = value + return '' + value_string + "" -def format_value(value, qname=None, current_ontology_url=None): + +def format_value(item, qname=None, current_ontology_url=None): """ Format the value to be used in the template. """ # FIXME: the following section is a hack to have a working example. # This should be done differently and follow the Respec syntax for URLs. - if value.get("type") == "uri": - if current_ontology_url and value["value"].startswith(current_ontology_url): - value_uri = value["value"].replace(current_ontology_url, "#") - - else: - value_uri = value["value"] - if qname: - value_string = qname(value["value"]) - else: - value_string = value["value"] - return '' + value_string + "" - elif value.get("type") == "literal": - return value["value"] - elif value.get("type") == None: + if item.get("type") == "uri": + return format_uri_value(item.get("value", ""), qname=qname, current_ontology_url=current_ontology_url) + elif item.get("type") == "literal": + return item["value"] + elif item.get("type") == None: # Display a warning message print("Warning: missing value encountered.") return "" @@ -52,12 +54,12 @@ def format_value(value, qname=None, current_ontology_url=None): # Display a warning message print( "Warning: unknown type '" - + value.get("type") + + item.get("type") + "' for value '" - + value.get("value") + + item.get("value") + "'" ) - return value["value"] + return item["value"] def extract_fragment_identifier(uri_reference: str, separator="#"): @@ -76,7 +78,7 @@ def extract_fragment_identifier(uri_reference: str, separator="#"): "fragment" """ if separator in uri_reference: - return uri_reference.split("#")[1] + return uri_reference.split(separator)[-1] return "" diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_helpers.py b/tests/test_helpers.py new file mode 100644 index 0000000..aa5bce2 --- /dev/null +++ b/tests/test_helpers.py @@ -0,0 +1,15 @@ +import pytest + +from helpers import extract_fragment_identifier, format_value + + +def test_extract_fragment_identifier(): + assert extract_fragment_identifier("https://example.com/respecter", "/") == "respecter" + assert extract_fragment_identifier("https://example.com#respecter", "#") == "respecter" + +def test_format_value(): + assert format_value({"type": "uri", "value": "https://example.com/respecter"}, + current_ontology_url="https://example.com/") == 'https://example.com/respecter' + assert format_value({"type": "uri", "value": "https://example.com/respecter"}, + current_ontology_url="https://my-other-example.com/") == 'https://example.com/respecter' + assert format_value({"type": "litteral", "value": "Respecter"}) == "Respecter" diff --git a/tests/test_sparql.py b/tests/test_sparql.py index c227571..5ebae4f 100644 --- a/tests/test_sparql.py +++ b/tests/test_sparql.py @@ -1,7 +1,7 @@ import pytest import rdflib -from respecter.sparql import sparql_query, apply_sparql_query_file, SparqlConfig +from sparql import sparql_query, apply_sparql_query_file, SparqlConfig LANGUAGES_DATA_FILE_PATH = "tests/data/languages.ttl" SPARQL_FILE_PATH = "tests/sparql/languages.sparql" From 18a44f4e56c85a374e007a172adab169ed42c02a Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 14:37:08 +0200 Subject: [PATCH 6/7] feat: add tests for cli --- pyproject.toml | 2 +- tests/test_cli.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/test_cli.py diff --git a/pyproject.toml b/pyproject.toml index 0f67a3c..51ad7f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,4 +1,4 @@ [tool.pytest.ini_options] pythonpath = [ - "./respecter" + "respecter" ] diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..e54eedf --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,13 @@ +from cli import app +from typer.testing import CliRunner + +runner = CliRunner() + +def test_help(): + """ Checks if 'respecter --help' commands exists successfully.""" + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + +def test_version(): + result = runner.invoke(app, ["--version"]) + assert result.exit_code == 0 From eadc65be524c2d1ee79ee8fa272d52c796149300 Mon Sep 17 00:00:00 2001 From: Martin Fontanet Date: Mon, 19 Aug 2024 14:42:04 +0200 Subject: [PATCH 7/7] feat: add test for core --- tests/test_core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/test_core.py diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..0b01c07 --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,12 @@ +import pytest + +from core import fix_prefixes + +def test_fix_prefixes(): + input_html = """ + ex:Bob a schema1:Person + """ + expected_html = """ + ex:Bob a schema:Person + """ + assert fix_prefixes(input_html) == expected_html