]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-variants.py
Qualify pinned CXML encodings with reproducible local patches
[topic-maps.git] / scripts / check-variants.py
1 #!/usr/bin/env python3
2 """Independent model/baseline correspondence and Variant XML ordering checks."""
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'] == 'variant-evidence' and report['failed'] == 0
12 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
13 assert [c['id'] for c in report['cases']] == ['ctm/in/' + n + '.ctm' for n in
14 ['variant', 'variants', 'variant-duplicate']]
15 properties = ['itemIdentifiers', 'subjectIdentifiers', 'subjectLocators']
16 for case in report['cases']:
17 assert case['ctmSource'] == (root / 'vendor/cxtm' / case['id']).read_bytes().decode('utf-8')
18 baseline = ET.parse(root / 'vendor/cxtm/ctm/baseline' /
19 (pathlib.Path(case['id']).name + '.cxtm')).getroot()
20 def absolute(value):
21 return case['documentIRI'] + value if value.startswith('#') else value
22 expected_keys = {t.attrib['number']: tuple(tuple(sorted(absolute(v.text) for v in t.findall(p + '/locator')))
23 for p in properties) for t in baseline.findall('topic')}
24 actual = case['actualObjects']
25 actual_keys = {str(t['ordinal']): tuple(tuple(sorted(t[p])) for p in properties) for t in actual['topics']}
26 assert collections.Counter(expected_keys.values()) == collections.Counter(actual_keys.values())
27 def scope(keys, refs):
28 return tuple(sorted(keys[str(r)] for r in refs))
29 expected = []
30 for topic in baseline.findall('topic'):
31 for n in topic.findall('name'):
32 variants = sorted((v.findtext('value'), v.findtext('datatype'),
33 scope(expected_keys, [t.attrib['topicref'] for t in v.findall('scope/scopingTopic')]))
34 for v in n.findall('variant'))
35 expected.append((expected_keys[topic.attrib['number']], n.findtext('value'),
36 expected_keys[n.find('type').attrib['topicref']],
37 scope(expected_keys, [t.attrib['topicref'] for t in n.findall('scope/scopingTopic')]),
38 tuple(variants)))
39 observed = []
40 for n in actual['names']:
41 assert all(v['parentIsName'] and v['rawParentIsName'] for v in n['variants'])
42 for v in n['variants']:
43 assert len(v['scope']) == len(set(v['scope']))
44 assert set(n['scope']) < set(v['scope'])
45 variants = sorted((v['value'], v['datatype'], scope(actual_keys, v['scope'])) for v in n['variants'])
46 observed.append((actual_keys[str(n['owner'])], n['value'], actual_keys[str(n['type'])],
47 scope(actual_keys, n['scope']), tuple(variants)))
48 assert collections.Counter(expected) == collections.Counter(observed), case['id']
49
50 # Exercise each comparison key using XML independently of the Lisp comparator.
51 xml = ET.fromstring(report['orderingWitnessCXTM'])
52 keys = {t.attrib['number']: t.findtext('itemIdentifiers/locator') for t in xml.findall('topic')}
53 owner = next(t for t in xml.findall('topic') if t.findtext('itemIdentifiers/locator') == 'urn:owner')
54 variants = owner.find('name').findall('variant')
55 assert [v.attrib['number'] for v in variants] == ['1', '2', '3', '4', '5']
56 assert [(v.findtext('value'), v.findtext('datatype'),
57 tuple(keys[t.attrib['topicref']] for t in v.findall('scope/scopingTopic'))) for v in variants] == [
58 ('A', 'http://www.w3.org/2001/XMLSchema#string', ('urn:a',)),
59 ('V', 'urn:a', ('urn:b',)),
60 ('V', 'urn:b', ('urn:a',)),
61 ('V', 'urn:b', ('urn:a', 'urn:b')),
62 ('urn:0', 'http://www.w3.org/2001/XMLSchema#anyURI', ('urn:a',))]
63 for v in variants:
64 assert [c.tag for c in v] == ['value', 'datatype', 'scope']
65 assert v.find('type') is None
66 print('VARIANTS-INDEPENDENT-PASS: three original model observations; Parent identity, datatype locators, Scope sets and XML ordering')