-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conformance.py
More file actions
114 lines (95 loc) · 3.26 KB
/
Copy pathtest_conformance.py
File metadata and controls
114 lines (95 loc) · 3.26 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Byte-identical conformance against Swift-generated fixtures.
The fixture suite lives at QueueKit/Tests/QueueKitTests/Fixtures/ and
is generated by the Swift wire-format fixture generator (which constructs
Job and SignalFile values directly and encodes them with deterministic HLCs).
Python must produce byte-for-byte identical output.
"""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from queuekit.job import (
Job,
SignalFile,
HLC,
ArtifactRef,
ObservationStatus,
encode_job,
encode_signal,
filename_for_job,
)
FIXTURES = (
Path(__file__).resolve().parent.parent.parent
/ "Tests" / "QueueKitTests" / "Fixtures"
)
def _signed_node(unsigned: int) -> int:
return unsigned if unsigned < 0x80000000 else unsigned - 0x1_0000_0000
def _load_job_input(name: str) -> Job:
p = FIXTURES / f"{name}_input.json"
obj = json.loads(p.read_bytes())
payload_hex = obj.get("payload_bytes_hex", "")
payload = bytes.fromhex(payload_hex) if payload_hex else b""
sa = obj["submitted_at"]
return Job(
id=obj["id"],
stream_id=obj["stream_id"],
submitted_at=HLC(
physical_time=sa["physical_time"],
logical_count=sa["logical_count"],
node_id=_signed_node(sa["node_id"]),
),
priority=obj["priority"],
payload=payload,
extensions=obj.get("extensions", {}),
)
def _load_signal_input(name: str) -> SignalFile:
p = FIXTURES / f"signal_{name}_input.json"
obj = json.loads(p.read_bytes())
ca = obj["completed_at"]
return SignalFile(
job_id=obj["job_id"],
status=ObservationStatus(obj["status"]),
artifacts=[ArtifactRef(a["type"], a["value"])
for a in obj.get("artifacts", [])],
completed_at=HLC(
physical_time=ca["physical_time"],
logical_count=ca["logical_count"],
node_id=_signed_node(ca["node_id"]),
),
)
@pytest.mark.skipif(
not FIXTURES.exists(),
reason=f"fixtures dir not present at {FIXTURES}",
)
@pytest.mark.parametrize(
"name", ["job_001", "job_002", "job_003", "job_004", "job_005"])
def test_job_byte_identical(name):
job = _load_job_input(name)
expected = (FIXTURES / f"{name}_file.json").read_bytes()
actual = encode_job(job)
assert actual == expected, (
f"Python encoded job differs from Swift fixture {name}:\n"
f" expected: {expected!r}\n"
f" actual: {actual!r}")
@pytest.mark.skipif(
not FIXTURES.exists(),
reason=f"fixtures dir not present at {FIXTURES}",
)
@pytest.mark.parametrize(
"name", ["job_001", "job_002", "job_003", "job_004", "job_005"])
def test_filename_byte_identical(name):
job = _load_job_input(name)
expected = (FIXTURES / f"{name}_filename.txt").read_text().strip()
actual = filename_for_job(job)
assert actual == expected
@pytest.mark.skipif(
not FIXTURES.exists(),
reason=f"fixtures dir not present at {FIXTURES}",
)
@pytest.mark.parametrize(
"name", ["001", "002", "003", "004", "005"])
def test_signal_byte_identical(name):
sig = _load_signal_input(name)
expected = (FIXTURES / f"signal_{name}_output.json").read_bytes()
actual = encode_signal(sig)
assert actual == expected