]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-scopes.py
feat(tmdm): readers for the TMDM 7.2/7.3 relationships; tests for both default name...
[topic-maps.git] / scripts / check-scopes.py
1 #!/usr/bin/env python3
2 """Independently compare observed Scope object relations with unchanged baselines.
3 Canonical byte equality is checked separately by check-cxtm.py.
4 """
5 import collections
6 import json
7 import pathlib
8 import sys
9 import xml.etree.ElementTree as ET
10
11 root = pathlib.Path(__file__).resolve().parent.parent
12 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
13 assert report['kind'] == 'scope-evidence' and report['failed'] == 0
14 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
15 assert [c['id'] for c in report['cases']] == ['ctm/in/' + n + '.ctm' for n in
16 ['name-scope', 'occurrence-scope', 'association-scope']]
17 properties = ['itemIdentifiers', 'subjectIdentifiers', 'subjectLocators']
18 for case in report['cases']:
19 assert case['ctmSource'] == (root / 'vendor/cxtm' / case['id']).read_bytes().decode('utf-8')
20 expected = ET.parse(root / 'vendor/cxtm/ctm/baseline' /
21 (pathlib.Path(case['id']).name + '.cxtm')).getroot()
22 def locator(value):
23 return case['documentIRI'] + value if value.startswith('#') else value
24 expected_keys = {t.attrib['number']: tuple(tuple(sorted(locator(v.text) for v in t.findall(p + '/locator')))
25 for p in properties) for t in expected.findall('topic')}
26 actual = case['actualObjects']
27 actual_keys = {str(t['ordinal']): tuple(tuple(sorted(t[p])) for p in properties) for t in actual['topics']}
28 assert collections.Counter(expected_keys.values()) == collections.Counter(actual_keys.values())
29 expected_items = []
30 for owner in expected.findall('topic'):
31 for kind in ['name', 'occurrence']:
32 for i in owner.findall(kind):
33 expected_items.append((kind, expected_keys[owner.attrib['number']],
34 expected_keys[i.find('type').attrib['topicref']], i.find('value').text,
35 i.findtext('datatype'), frozenset(expected_keys[s.attrib['topicref']]
36 for s in i.findall('scope/scopingTopic')), ()))
37 for i in expected.findall('association'):
38 expected_items.append(('association', 'map', expected_keys[i.find('type').attrib['topicref']], None, None,
39 frozenset(expected_keys[s.attrib['topicref']] for s in i.findall('scope/scopingTopic')),
40 tuple(sorted((expected_keys[r.find('type').attrib['topicref']],
41 expected_keys[r.find('player').attrib['topicref']]) for r in i.findall('role')))))
42 actual_items = []
43 for i in actual['items']:
44 scope = i['scopeTopicOrdinals']
45 assert len(scope) == len(set(scope)) == 2
46 assert all(r['parentIsAssociation'] for r in i['roles'])
47 actual_items.append((i['kind'], 'map' if i['parent'] == 'map' else actual_keys[str(i['parent'])],
48 actual_keys[str(i['typeTopicOrdinal'])], i['value'], i['datatype'],
49 frozenset(actual_keys[str(s)] for s in scope),
50 tuple(sorted((actual_keys[str(r['typeTopicOrdinal'])], actual_keys[str(r['playerTopicOrdinal'])])
51 for r in i['roles']))))
52 assert collections.Counter(expected_items) == collections.Counter(actual_items), case['id']
53
54 # A separate construction-order witness exercises equal Value/Type/role signatures.
55 # Inspect XML rather than invoking Lisp's sorting/comparison functions as oracle.
56 xml = ET.fromstring(report['orderingWitnessCXTM'])
57 topics = xml.findall('topic')
58 keys = {int(t.attrib['number']): t.findtext('itemIdentifiers/locator') for t in topics}
59 assert len(keys) == 4 and set(keys.values()) == {'urn:a', 'urn:b', 'urn:owner', 'urn:type'}
60 owner = next(t for t in topics if t.findtext('itemIdentifiers/locator') == 'urn:owner')
61 def scopes(items):
62 return [tuple(keys[int(s.attrib['topicref'])] for s in i.findall('scope/scopingTopic')) for i in items]
63 expected_scopes = [(), ('urn:a',), ('urn:b',), ('urn:a', 'urn:b')]
64 assert scopes(owner.findall('name')) == expected_scopes
65 assert scopes(owner.findall('occurrence')) == expected_scopes
66 assert scopes(xml.findall('association')) == expected_scopes
67 assert [r.attrib['ref'] for r in owner.findall('rolePlayed')] == [f'association.{i}.role.1' for i in range(1,5)]
68 for i in xml.iter():
69 if 'topicref' in i.attrib:
70 assert int(i.attrib['topicref']) in keys
71 if i.tag == 'scope':
72 refs = [int(s.attrib['topicref']) for s in i]
73 assert refs and refs == sorted(set(refs))
74 print('SCOPES-INDEPENDENT-PASS: three original model observations, Topic identity categories and Scope sets; independent XML ordering/Role references')