| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import csv |
| 20 |
import os.path |
| 21 |
from functools import partial |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from pyfactxx import coras |
| 26 |
from pyfactxx.coras.cli import load_and_parse |
| 27 |
|
| 28 |
import rdflib |
| 29 |
|
| 30 |
|
| 31 |
TESTS = [ |
| 32 |
('foaf', '01-foaf.sq', ['foaf.rdf', 'tbl-foaf-card.n3'], '01-result.csv'), |
| 33 |
('foaf', '02-foaf.sq', ['foaf.rdf', 'tbl-foaf-card.n3'], '02-result.csv'), |
| 34 |
('foaf', '03-foaf.sq', ['foaf.rdf', '03-foaf-ex.n3'], '03-result.csv'), |
| 35 |
('zebra', '01-zebra.sq', ['zebra.n3'], '01-result.csv'), |
| 36 |
('zebra', '02-zebra.sq', ['zebra.n3'], '02-result.csv'), |
| 37 |
('tsars', '01-tsars.sq', ['tsars.rdf'], '01-result.csv'), |
| 38 |
('tsars', '02-tsars.sq', ['tsars.rdf'], '02-result.csv'), |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize( |
| 43 |
'prefix,query,ontologies,expected', |
| 44 |
TESTS, |
| 45 |
) |
| 46 |
def test_coras(prefix, query, ontologies, expected): |
| 47 |
"""Test coras RDFlib interface""" |
| 48 |
crs = coras.Coras() |
| 49 |
|
| 50 |
f_path = partial(os.path.join, os.path.dirname(__file__), prefix) |
| 51 |
f_open = lambda fn: open(f_path(fn)) |
| 52 |
|
| 53 |
files = (f_path(fn) for fn in ontologies) |
| 54 |
load_and_parse(crs, *files) |
| 55 |
crs.realise() |
| 56 |
|
| 57 |
with f_open(expected) as f: |
| 58 |
expected = list(csv.reader(f)) |
| 59 |
|
| 60 |
with f_open(query) as f: |
| 61 |
sq = f.read() |
| 62 |
|
| 63 |
result = crs.query(sq) |
| 64 |
result = [[str(s) for s in row] for row in result if all(not isinstance(s, rdflib.BNode) for s in row)] |
| 65 |
|
| 66 |
assert sorted(expected) == sorted(result) |
| 67 |
|
| 68 |
|