]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-typed-names.py
Implement CTM Mergemap with explicit byte resources and isolated environments
[topic-maps.git] / scripts / check-typed-names.py
1 #!/usr/bin/env python3
2 """Independent model-property checks against the four unchanged Name baselines.
3 The separate check-cxtm.py checks canonical bytes; these checks compare objects.
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'] == 'typed-name-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', 'name2', 'name-type', 'name-duplicate']]
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 assert actual['topicCount'] == len(expected_keys) == 2 and actual['nameCount'] == 1
30 expected_names = collections.Counter((n.find('value').text, expected_keys[t.attrib['number']],
31 expected_keys[n.find('type').attrib['topicref']])
32 for t in expected.findall('topic') for n in t.findall('name'))
33 actual_names = collections.Counter((n['value'], actual_keys[str(n['parentTopicOrdinal'])],
34 actual_keys[str(n['typeTopicOrdinal'])])
35 for t in actual['topics'] for n in t['names'])
36 assert expected_names == actual_names, case['id']
37 assert all(t['parentIsMap'] for t in actual['topics'])
38 print('TYPED-NAMES-INDEPENDENT-PASS: four original sources, exact identifier sets and Name value/Parent/Type relations')