]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-include.py
Qualify closed XML/C14N 1.0 infrastructure for XTM resourceData
[topic-maps.git] / scripts / check-include.py
1 #!/usr/bin/env python3
2 """Check Include model observations separately from the original byte contracts."""
3 import collections
4 import json
5 import pathlib
6 import sys
7 import urllib.parse
8 import xml.etree.ElementTree as ET
9
10 ROOT = pathlib.Path(__file__).resolve().parent.parent
11 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
12 assert report['kind'] == 'ctm-include-execution-evidence' and report['failed'] == 0
13 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
14 assert len(report['cases']) == 9 and len(report['rejections']) == 3
15 sizes = {'include': (1939, 1939), 'include-prefix': (140, 140), 'include-prefix2': (261, 261),
16 'include-template': (353, 401), 'include-template-wildcard': (330, 330),
17 'include-tm-reifier': (375, 375), 'include-topicref': (1490, 1490),
18 'include-wildcard': (499, 499), 'include-wildcard2': (719, 669)}
19 properties = ('itemIdentifiers', 'subjectIdentifiers', 'subjectLocators')
20
21
22 def identity(topic):
23 return tuple(tuple(sorted(topic[k])) for k in properties)
24
25
26 def by_identifier(model, iri):
27 return next(t for t in model['topics'] if iri in t['itemIdentifiers'])
28
29
30 def signature(topic, topics):
31 return (identity(topic),
32 tuple(sorted((n['value'], identity(topics[n['type'] - 1])) for n in topic['names'])),
33 tuple(sorted((o['value'], o['datatype'], identity(topics[o['type'] - 1])) for o in topic['occurrences'])))
34
35
36 for case in report['cases']:
37 name = pathlib.Path(case['id']).stem
38 assert name in sizes
39 assert case['ctmSource'].encode() == (ROOT / 'vendor/cxtm' / case['id']).read_bytes()
40 baseline = (ROOT / 'vendor/cxtm/ctm/baseline' / (name + '.ctm.cxtm')).read_bytes()
41 assert (case['byteCount'], case['expectedByteCount']) == sizes[name]
42 assert len(baseline) == sizes[name][1]
43 known = name in {'include-template', 'include-wildcard2'}
44 assert case['baselineMatch'] is (not known)
45 assert bool(case['discrepancy']) == known
46 doc = case['documentIRI']
47 model = case['model']
48 topics = model['topics']
49 xml = ET.fromstring(baseline)
50 assert model['topicCount'] == len(topics) == len(xml.findall('topic'))
51 assert model['associationCount'] == len(xml.findall('association'))
52 assert case['counter'] == {'include-template-wildcard': 1, 'include-wildcard': 2, 'include-wildcard2': 3}.get(name, 0)
53 for i, topic in enumerate(topics, 1):
54 assert topic['ordinal'] == i
55 for item in topic['names'] + topic['occurrences']:
56 assert item['parent'] == i and 1 <= item['type'] <= len(topics)
57 assert not item['scope']
58 expected_topics = []
59 for i, t in enumerate(xml.findall('topic'), 1):
60 expected_topics.append({**{k: [urllib.parse.urljoin(doc, l.text) for l in t.findall(k + '/locator')]
61 for k in properties},
62 'names': [dict(value=n.findtext('value'), type=int(n.find('type').attrib['topicref'])) for n in t.findall('name')],
63 'occurrences': [dict(value=o.findtext('value'), datatype=o.findtext('datatype'), type=int(o.find('type').attrib['topicref'])) for o in t.findall('occurrence')]})
64 if not known:
65 assert collections.Counter(signature(t, topics) for t in topics) == collections.Counter(signature(t, expected_topics) for t in expected_topics), name
66 elif name == 'include-template':
67 assert len(case['environments']) == 2
68 child = case['environments'][0]
69 assert (child['model']['topicCount'], child['templateCount'], child['initialCounter'], child['finalCounter']) == (0, 1, 0, 0)
70 assert child['wildcardIRI'] == doc
71 owner = by_identifier(model, doc + '#topic')
72 typ = by_identifier(model, doc + '#occtype')
73 assert owner['itemIdentifiers'] == [doc + '#topic']
74 assert typ['itemIdentifiers'] == [doc + '#occtype']
75 assert owner['occurrences'] == [dict(value='Test', datatype='http://www.w3.org/2001/XMLSchema#string', parent=owner['ordinal'], type=typ['ordinal'], scope=[])]
76 assert any('include-template.sub#occtype' in l.text for l in xml.findall('.//locator'))
77 else:
78 b = urllib.parse.urljoin(doc, 'include-wildcard2.sub')
79 c = urllib.parse.urljoin(doc, 'include-wildcard2.sub2')
80 envs = case['environments']
81 assert [(e['documentIRI'], e['wildcardIRI'], e['initialCounter'], e['finalCounter']) for e in envs] == [(c, b, 0, 1), (b, doc, 0, 2), (doc, doc, 0, 3)]
82 assert by_identifier(envs[0]['model'], b + '#$__1.foo')['itemIdentifiers'] == [b + '#$__1.foo']
83 assert set(by_identifier(model, doc + '#$__1.foo')['itemIdentifiers']) == {doc + '#$__1.foo', b + '#$__1.foo'}
84 for i, value in enumerate(['Sub Sub', 'Sub', 'Master'], 1):
85 owner = by_identifier(model, doc + f'#$__{i}.foo')
86 assert len(owner['names']) == 1 and owner['names'][0]['value'] == value
87 assert len({t['names'][0]['type'] for t in topics if t['names']}) == 1
88 assert len(xml.findall('.//itemIdentifiers/locator')) == 3
89 assert sum(len(t['itemIdentifiers']) for t in topics) == 4
90 for transfer in case['aliasTransfers']:
91 child, parent = transfer['childDocumentIRI'], transfer['parentDocumentIRI']
92 before, after = transfer['before']['topics'], transfer['after']['topics']
93 # Independent string-prefix/remapping on the actual pre-transfer sets.
94 expected = []
95 for topic in before:
96 ids = set(topic['itemIdentifiers'])
97 ids.update(urllib.parse.urljoin(parent, i[len(child):]) for i in topic['itemIdentifiers'] if i.startswith(child))
98 expected.append((tuple(sorted(ids)), tuple(sorted(topic['subjectIdentifiers'])), tuple(sorted(topic['subjectLocators']))))
99 assert collections.Counter(expected) == collections.Counter(identity(t) for t in after)
100 assert len(case['aliasTransfers']) == len(case['requests']) == (2 if name == 'include-wildcard2' else 1)
101 for request in case['requests']:
102 assert request['purpose'] == 'INCLUDE' and request['notation'] is None
103 assert request['resourceIRI'] == urllib.parse.urljoin(request['fromDocumentIRI'], pathlib.Path(request['sourceKey']).name)
104 assert (ROOT / 'vendor/cxtm' / request['sourceKey']).is_file()
105
106 negative = {'include-prefix': ('UNDEFINED-PREFIX', 'UNDEFINED-PREFIX', 'ctm/invalid/include-prefix.ctm', 31),
107 'include-prefix2': ('UNDEFINED-PREFIX', 'UNDEFINED-PREFIX', 'ctm/invalid/include-prefix2.sub', 1),
108 'include-xtm': ('CTM-REJECTION', 'INVALID-IRI', 'ctm/invalid/include-xtm.sub', 9)}
109 for case in report['rejections']:
110 name = pathlib.Path(case['id']).stem
111 assert (case['class'], case['code'], case['source'], case['position']) == negative[name]
112 assert len(case['requests']) == 1
113 text = (ROOT / 'vendor/cxtm' / case['source']).read_text()
114 prefix = text[:case['position']]
115 assert case['line'] == prefix.count('\n') + 1
116 assert case['column'] == len(prefix.rsplit('\n', 1)[-1]) + 1
117 print('INCLUDE-INDEPENDENT-PASS: nine model traces, each alias level, separate two baseline discrepancies, three intended negative causes')