aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-11 13:22:13 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commitbaa2e4e768f542953144cfa6ebe5713ecad389fc (patch)
tree379ecd51192b78f1cb5fed86621be00219e962b1
parentb0c6047765944609d1eec479898547d792419f39 (diff)
downloadserd-baa2e4e768f542953144cfa6ebe5713ecad389fc.tar.gz
serd-baa2e4e768f542953144cfa6ebe5713ecad389fc.tar.bz2
serd-baa2e4e768f542953144cfa6ebe5713ecad389fc.zip
Factor out common test runner facilities
-rwxr-xr-xtest/run_test_suite.py66
-rw-r--r--test/serd_test_util/__init__.py63
2 files changed, 71 insertions, 58 deletions
diff --git a/test/run_test_suite.py b/test/run_test_suite.py
index f9524a9c..d6772014 100755
--- a/test/run_test_suite.py
+++ b/test/run_test_suite.py
@@ -2,6 +2,8 @@
"""Run an RDF test suite with serdi."""
+import serd_test_util
+
import argparse
import datetime
import difflib
@@ -15,31 +17,6 @@ import tempfile
import urllib.parse
-def earl_assertion(test, passed, asserter):
- """Return a Turtle description of an assertion for the test report."""
-
- asserter_str = ""
- if asserter is not None:
- asserter_str = "\n\tearl:assertedBy <%s> ;" % asserter
-
- return """
-[]
-\ta earl:Assertion ;%s
-\tearl:subject <http://drobilla.net/sw/serd> ;
-\tearl:test <%s> ;
-\tearl:result [
-\t\ta earl:TestResult ;
-\t\tearl:outcome %s ;
-\t\tdc:date "%s"^^xsd:dateTime
-\t] .
-""" % (
- asserter_str,
- test,
- "earl:passed" if passed else "earl:failed",
- datetime.datetime.now().replace(microsecond=0).isoformat(),
- )
-
-
def log_error(message):
"""Log an error message to stderr"""
@@ -163,37 +140,6 @@ def _test_output_syntax(test_class):
raise Exception("Unknown test class <{}>".format(test_class))
-def _load_rdf(filename, base_uri, command_prefix):
- """Load an RDF file as dictionaries via serdi (only supports URIs)."""
-
- rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
- model = {}
- instances = {}
-
- cmd = command_prefix + ["-I", base_uri, filename]
- proc = subprocess.run(cmd, capture_output=True, check=True)
- for line in proc.stdout.splitlines():
- matches = re.match(
- r"<([^ ]*)> <([^ ]*)> <([^ ]*)> \.", line.decode("utf-8")
- )
- if matches:
- s, p, o = (matches.group(1), matches.group(2), matches.group(3))
- if s not in model:
- model[s] = {p: [o]}
- elif p not in model[s]:
- model[s][p] = [o]
- else:
- model[s][p].append(o)
-
- if p == rdf_type:
- if o not in instances:
- instances[o] = set([s])
- else:
- instances[o].update([s])
-
- return model, instances
-
-
def _option_combinations(options):
"""Return an iterator that cycles through all combinations of options."""
@@ -259,7 +205,9 @@ def test_suite(
mf = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#"
test_dir = os.path.dirname(manifest_path)
- model, instances = _load_rdf(manifest_path, base_uri, command_prefix)
+ model, instances = serd_test_util.load_rdf(
+ command_prefix + ["-I", base_uri], manifest_path
+ )
top_dir = os.path.commonpath([os.getcwd(), os.path.abspath(test_dir)])
out_test_dir = os.path.relpath(test_dir, top_dir)
@@ -406,7 +354,9 @@ def test_suite(
# Write test report entry
if report_filename:
with open(report_filename, "a") as report:
- report.write(earl_assertion(test, passed, asserter))
+ report.write(
+ serd_test_util.earl_assertion(test, passed, asserter)
+ )
# Run all test types in the test suite
results = Results()
diff --git a/test/serd_test_util/__init__.py b/test/serd_test_util/__init__.py
new file mode 100644
index 00000000..f0b1c19a
--- /dev/null
+++ b/test/serd_test_util/__init__.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+"""Utilities for data-driven tests."""
+
+import datetime
+import re
+import subprocess
+
+
+def earl_assertion(test, passed, asserter):
+ """Return a Turtle description of an assertion for the test report."""
+
+ asserter_str = ""
+ if asserter is not None:
+ asserter_str = "\n\tearl:assertedBy <%s> ;" % asserter
+
+ return """
+[]
+\ta earl:Assertion ;%s
+\tearl:subject <http://drobilla.net/sw/serd> ;
+\tearl:test <%s> ;
+\tearl:result [
+\t\ta earl:TestResult ;
+\t\tearl:outcome %s ;
+\t\tdc:date "%s"^^xsd:dateTime
+\t] .
+""" % (
+ asserter_str,
+ test,
+ "earl:passed" if passed else "earl:failed",
+ datetime.datetime.now().replace(microsecond=0).isoformat(),
+ )
+
+
+def load_rdf(command_prefix, filename):
+ """Load an RDF file as dictionaries via serdi (only supports URIs)."""
+
+ rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
+ model = {}
+ instances = {}
+
+ cmd = command_prefix + [filename]
+ proc = subprocess.run(cmd, capture_output=True, check=True)
+ for line in proc.stdout.splitlines():
+ matches = re.match(
+ r"<([^ ]*)> <([^ ]*)> <([^ ]*)> \.", line.decode("utf-8")
+ )
+ if matches:
+ s, p, o = (matches.group(1), matches.group(2), matches.group(3))
+ if s not in model:
+ model[s] = {p: [o]}
+ elif p not in model[s]:
+ model[s][p] = [o]
+ else:
+ model[s][p].append(o)
+
+ if p == rdf_type:
+ if o not in instances:
+ instances[o] = set([s])
+ else:
+ instances[o].update([s])
+
+ return model, instances