]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-templates.py
docs(tmdm): coverage matrix, relationship readers, default name types; run report
[topic-maps.git] / scripts / check-templates.py
1 #!/usr/bin/env python3
2 """Independent JSON/model-reference checks against unchanged original XML."""
3 import collections
4 import json
5 import pathlib
6 import sys
7 import xml.etree.ElementTree as ET
8
9 ROOT = pathlib.Path(__file__).resolve().parent.parent
10 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
11 assert report['kind'] == 'template-execution-evidence' and report['failed'] == 0
12 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
13 assert len(report['cases']) == 57 and len(report['rejections']) == 10
14 assert len({c['id'] for c in report['cases']}) == 57
15 for case in report['cases']:
16 original = ROOT / 'vendor/cxtm/ctm/baseline' / (pathlib.Path(case['id']).name + '.cxtm')
17 xml = ET.fromstring(original.read_bytes())
18 model = case['model']
19 assert model['topicCount'] == len(model['topics']) == len(xml.findall('topic')), case['id']
20 assert model['associationCount'] == len(xml.findall('association')), case['id']
21 topics = model['topics']
22 assert [t['ordinal'] for t in topics] == list(range(1, len(topics) + 1))
23
24 def identity(values):
25 # The actual byte comparison is separate. These examples use local
26 # fragment IIs or absolute external locators. Relate observed absolute
27 # locators to the baseline's CXTM 3.18 base-relative representations.
28 return tuple(sorted(v[len(case['documentIRI']):] if v.startswith(case['documentIRI']) else v for v in values))
29
30 def model_key(t):
31 return tuple(identity(t[k]) for k in ('itemIdentifiers', 'subjectIdentifiers', 'subjectLocators'))
32
33 def xml_key(t):
34 return tuple(tuple(sorted(x.text or '' for x in t.findall(k + '/locator')))
35 for k in ('itemIdentifiers', 'subjectIdentifiers', 'subjectLocators'))
36
37 xml_by_key = {xml_key(t): t for t in xml.findall('topic')}
38 assert len(xml_by_key) == len(topics)
39 refs = {t['ordinal']: xml_by_key[model_key(t)].attrib['number'] for t in topics}
40 observed_names = []
41 observed_occurrences = []
42 for topic in topics:
43 for name in topic['names']:
44 assert name['parent'] == topic['ordinal']
45 observed_names.append((refs[name['parent']], refs[name['type']], name['value'],
46 tuple(sorted(refs[s] for s in name['scope'])), name['variantCount']))
47 for occurrence in topic['occurrences']:
48 assert occurrence['parent'] == topic['ordinal']
49 observed_occurrences.append((refs[occurrence['parent']], refs[occurrence['type']], occurrence['value'],
50 occurrence['datatype'], tuple(sorted(refs[s] for s in occurrence['scope']))))
51 expected_names = []
52 expected_occurrences = []
53 for topic in xml.findall('topic'):
54 for name in topic.findall('name'):
55 expected_names.append((topic.attrib['number'], name.find('type').attrib['topicref'], name.findtext('value') or '',
56 tuple(sorted(s.attrib['topicref'] for s in name.findall('scope/scopingTopic'))), len(name.findall('variant'))))
57 for occurrence in topic.findall('occurrence'):
58 expected_occurrences.append((topic.attrib['number'], occurrence.find('type').attrib['topicref'], occurrence.findtext('value') or '',
59 occurrence.findtext('datatype'), tuple(sorted(s.attrib['topicref'] for s in occurrence.findall('scope/scopingTopic')))))
60 assert collections.Counter(observed_names) == collections.Counter(expected_names), case['id']
61 assert collections.Counter(observed_occurrences) == collections.Counter(expected_occurrences), case['id']
62 expected_rejections = {
63 'embedded-topic-template': ('INVALID-TEMPLATE-BODY', 'INVALID-TEMPLATE-BODY'),
64 'occurrence-explicit-datatype-as-parameter': ('INVALID-TEMPLATE-BODY', 'VARIABLE-DATATYPE'),
65 'prefix-template-scope': ('INVALID-TEMPLATE-BODY', 'INVALID-TEMPLATE-BODY'),
66 'template-bad-type': ('INVALID-TEMPLATE-ARGUMENT', 'INVALID-TEMPLATE-ARGUMENT'),
67 'template-missing-args': ('TEMPLATE-ARITY-ERROR', 'TEMPLATE-ARITY'),
68 'template-missing-def': ('UNDEFINED-TEMPLATE', 'UNDEFINED-TEMPLATE'),
69 'template-name-collision': ('DUPLICATE-TEMPLATE', 'DUPLICATE-TEMPLATE'),
70 'tm-reifier-in-template': ('INVALID-TEMPLATE-BODY', 'INVALID-TEMPLATE-BODY'),
71 'variable-undefined': ('UNBOUND-TEMPLATE-VARIABLE', 'VARIABLE-OUTSIDE-TEMPLATE'),
72 'variable-undefined2': ('UNBOUND-TEMPLATE-VARIABLE', 'VARIABLE-OUTSIDE-TEMPLATE'),
73 }
74 for rejection in report['rejections']:
75 assert (rejection['class'], rejection['code']) == expected_rejections[pathlib.Path(rejection['id']).stem]
76 assert rejection['source'] == rejection['id']
77 text = (ROOT / 'vendor/cxtm' / rejection['id']).read_text()
78 assert 0 <= rejection['position'] <= len(text)
79 before = text[:rejection['position']]
80 assert rejection['line'] == before.count('\n') + 1
81 assert rejection['column'] == len(before.rsplit('\n', 1)[-1]) + 1
82 print('TEMPLATES-INDEPENDENT-PASS: 57 original model graphs and 10 specific rejection contracts')