Skip to content

Commit

Permalink
Merge pull request #2 from RDF-STaX/28-document-competency-question-t…
Browse files Browse the repository at this point in the history
…ests-1

Add code for generating CQ test docs
  • Loading branch information
Ostrzyciel authored Apr 10, 2024
2 parents 1dfb749 + e220034 commit 66c94e5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tasks/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@


def main():
if len(sys.argv) != 3:
print('Runs the competency question tests\n'
'Args:\n'
'- in_file: input RDF file\n'
'- test_dir: directory containing the test files')
sys.exit(1)

in_file = Path(sys.argv[1])
test_dir = Path(sys.argv[2])

Expand Down Expand Up @@ -45,10 +52,10 @@ def run_test(test_file: Path, g: Graph) -> bool:
print(f" Got {results} result(s)")

success = False
if test['expectation'] == 'empty' and results == 0:
success = True
elif test['expectation'] == 'nonempty' and results > 0:
success = True
if test['expectation'] == 'empty':
success = results == 0
elif test['expectation'] == 'nonEmpty':
success = results > 0
elif test['expectation']['resultsCount'] == results:
success = True

Expand Down
73 changes: 73 additions & 0 deletions tasks/tests_doc_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import yaml
from pathlib import Path

import sys
import textwrap


def main():
if len(sys.argv) != 3:
print('Generates the docs for competency question tests\n'
'Args:\n'
'- test_dir: directory containing the test files\n'
'- output_path: the file to write the docs to (will be truncated)')
sys.exit(1)

test_dir = Path(sys.argv[1])
output_path = Path(sys.argv[2])

out_f = open(output_path, 'wt')

# check all files in direct subdirectories of test_dir
for uc_dir in test_dir.iterdir():
if not uc_dir.is_dir():
continue
write_use_case(uc_dir, out_f)

for test_file in uc_dir.iterdir():
if not test_file.is_file() or test_file.suffix != '.yaml':
continue
write_test(test_file, out_f)

print('Done.')


def write_use_case(uc_dir, out_file):
uc_num = uc_dir.stem.replace('uc', '')
print(f'Generating use case {uc_num}')
out_file.write(f'## Use case {uc_num}\n\n')
with open(uc_dir / 'index.md', 'r') as f:
out_file.write(f.read())
out_file.write('\n')


def write_test(test_file, out_file):
with open(test_file, 'r') as f:
test = yaml.safe_load(f)

with open(test_file.with_suffix('.rq'), 'r') as f:
query = f.read()

def_link = f"https://github.com/RDF-STaX/rdf-stax.github.io/blob/main/tests/uc{test['useCase']}/{test_file.name}"
out_file.write(f"### Test CQ{test['useCase']}.{test['testNumber']} ([definition]({def_link}))\n\n")
out_file.write(f"**Question:** {test['text']}\n\n")
out_file.write(f"**Expectation:** ")
if test['expectation'] == 'nonEmpty':
out_file.write('query results are not empty')
elif test['expectation'] == 'empty':
out_file.write('query results are empty')
else:
c = test['expectation']['resultsCount']
if c == 1:
out_file.write(f'there is 1 query result')
else:
out_file.write(f'there are {c} query results')

out_file.write('\n\n??? example "SPARQL query of the test"\n\n')
out_file.write(' ```sparql\n')
out_file.write(textwrap.indent(query.strip(), 4 * ' '))
out_file.write('\n ```\n\n')


if __name__ == '__main__':
main()

0 comments on commit 66c94e5

Please sign in to comment.