forked from ChelseaKR/ctdl-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_determinism.py
More file actions
71 lines (58 loc) · 2.41 KB
/
Copy pathtest_determinism.py
File metadata and controls
71 lines (58 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Same input, byte-identical findings output. Twice."""
from __future__ import annotations
import json
import subprocess
import sys
from ctdl_validate import __version__, validate_document
from ctdl_validate.findings import render_findings_json
from .conftest import fixture_path, load_fixture, page_path
FIXTURES = [
"clean_framework.json",
"bug_class_250_bare_uuid_for_ctid.json",
"bug_class_252_wrong_framework_identifier.json",
"inverse_mismatch.json",
]
def test_validate_twice_gives_identical_findings() -> None:
for name in FIXTURES:
first = validate_document(load_fixture(name))
second = validate_document(load_fixture(name))
assert first == second, name
def test_json_rendering_is_byte_identical_across_runs() -> None:
for name in FIXTURES:
first = render_findings_json(validate_document(load_fixture(name)), __version__)
second = render_findings_json(validate_document(load_fixture(name)), __version__)
assert first.encode("utf-8") == second.encode("utf-8"), name
def test_extraction_output_is_byte_identical_across_processes() -> None:
# Extraction fetches, so its determinism claim is narrower and worth
# stating exactly: given the same page bytes, the same document and the
# same notes come out. --from-file is that claim, made checkable.
page = str(page_path("course_jsonld.html"))
command = [
sys.executable,
"-m",
"ctdl_validate",
"extract",
"https://example.edu/courses/weld-101",
"--from-file",
page,
"--format",
"json",
]
runs = [subprocess.run(command, capture_output=True, check=False) for _ in range(2)]
assert runs[0].stdout == runs[1].stdout
assert json.loads(runs[0].stdout)["summary"]["entities"] == 2
def test_cli_output_is_byte_identical_across_processes() -> None:
# Two separate interpreter processes: catches any hidden ordering that
# depends on hash randomization or import order.
path = str(fixture_path("bug_class_252_wrong_framework_identifier.json"))
runs = [
subprocess.run(
[sys.executable, "-m", "ctdl_validate", path, "--format", "json"],
capture_output=True,
check=False,
)
for _ in range(2)
]
assert runs[0].stdout == runs[1].stdout
assert runs[0].stdout, "expected findings output"
json.loads(runs[0].stdout) # and it is valid JSON