Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests #13

Open
wants to merge 7 commits into
base: local-links
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
debug/*
.venv
venv
__pycache__/
*.html
*.html
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tool.pytest.ini_options]
pythonpath = [
"respecter"
]
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 21 additions & 19 deletions respecter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,43 @@ 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 '<a href="' + value_uri + '">' + value_string + "</a>"

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 '<a href="' + value_uri + '">' + value_string + "</a>"
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 ""
else: # FIXME: handle other types
# 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="#"):
Expand All @@ -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 ""


Expand Down
34 changes: 34 additions & 0 deletions tests/config/sparql_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ontology: # Replace with the actual ontology base URI
uri_base: "https://example.com"
separator: "/"

type:
class: <http://www.w3.org/2000/01/rdf-schema#Class> #the class type in the ontology (could also be skos:Concept, owl:Class, etc.)
property: <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>
enumeration: <https://epfl.ch/example/EnumerationType>

predicate:
definition: <http://www.w3.org/2000/01/rdf-schema#comment>
label: <http://www.w3.org/2000/01/rdf-schema#label>
example: <http://www.w3.org/2004/02/skos/core#example>


prefix:
sh: <http://www.w3.org/ns/shacl#>
rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
skos: <http://www.w3.org/2004/02/skos/core#>
owl: <http://www.w3.org/2002/07/owl#>
dct: <http://purl.org/dc/terms/>
dcat: <https://www.w3.org/TR/vocab-dcat-2/#>
vann: <http://purl.org/vocab/vann/>
schema: <http://schema.org/>
sd: <https://w3id.org/okn/o/sd#>
bio: <https://bioschemas.org/>
spe: <https://openschemas.github.io/spec-container/specifications/>
rdfs: <http://www.w3.org/2000/01/rdf-schema#>
xsd: <http://www.w3.org/2001/XMLSchema#>
shsh: <http://www.w3.org/ns/shacl-shacl#>
dcterms: <http://purl.org/dc/terms/>
ex: <https://epfl.ch/example/>
md4i: <http://w3id.org/nfdi4ing/metadata4ing#>
dpv: <https://w3id.org/dpv#>
7 changes: 7 additions & 0 deletions tests/data/languages.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<https://example.com/Alice> a <https://schema.org/Person> ;
<https://schema.org/knowsLanguage> "English" .
<https://example.com/Bob> a <https://schema.org/Person> ;
<https://schema.org/knowsLanguage> "French" .

<https://example.com/SDSC> a <https://schema.org/Organization> ;
<https://schema.org/knowsLanguage> "English" .
4 changes: 4 additions & 0 deletions tests/sparql/languages.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT ?subject WHERE {
?subject a <https://schema.org/Person> .
?subject <https://schema.org/knowsLanguage> "English" .
}
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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/") == '<a href="#respecter">https://example.com/respecter</a>'
assert format_value({"type": "uri", "value": "https://example.com/respecter"},
current_ontology_url="https://my-other-example.com/") == '<a href="https://example.com/respecter">https://example.com/respecter</a>'
assert format_value({"type": "litteral", "value": "Respecter"}) == "Respecter"
47 changes: 47 additions & 0 deletions tests/test_sparql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
import rdflib

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"
SPARQL_CONFIG_FILE_PATH = "tests/config/sparql_config.yaml"

SAMPLE_SPARQL = """
SELECT ?subject WHERE {
?subject a <https://schema.org/Person> .
?subject <https://schema.org/knowsLanguage> "English" .
}
"""

def load_graph():
graph = rdflib.Graph()
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"

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") == "<http://www.w3.org/2000/01/rdf-schema#Class>"
assert sparql_config.get_type("property") == "<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>"
assert sparql_config.get_type("enumeration") == "<https://epfl.ch/example/EnumerationType>"
assert sparql_config.get_predicate("definition") == "<http://www.w3.org/2000/01/rdf-schema#comment>"
assert sparql_config.get_predicate("example") == "<http://www.w3.org/2004/02/skos/core#example>"
assert sparql_config.get_predicate("label") == "<http://www.w3.org/2000/01/rdf-schema#label>"