Files
mtusi2026lab5/student-unit/test_job_envelope.py
T

69 lines
1.8 KiB
Python

import copy
from datetime import UTC, datetime
from uuid import uuid4
import pytest
from jsonschema import Draft202012Validator, FormatChecker
from src.pipeline import validate_job
def example():
return dict(
schema_version=1,
job_id=str(uuid4()),
type="attachment.process.v1",
attachment_id=str(uuid4()),
pipeline_version=1,
attempt=1,
created_at=datetime.now(UTC).isoformat(),
)
def test_schema_and_runtime_agree_on_valid_job():
import json
from pathlib import Path
schema = json.loads(Path("contracts/jobs.schema.json").read_text())
for version in [1, 2]:
data = example()
data["pipeline_version"] = version
data.update(
traceparent="00-" + "a" * 32 + "-" + "b" * 16 + "-01",
tracestate="vendor=opaque",
)
Draft202012Validator(schema, format_checker=FormatChecker()).validate(
data
)
assert validate_job(data) == data
@pytest.mark.parametrize(
"field,value",
[
("schema_version", True),
("schema_version", 2),
("type", "other"),
("job_id", 123),
("job_id", "broken"),
("attachment_id", None),
("pipeline_version", 0),
("pipeline_version", True),
("attempt", 0),
("attempt", 1.0),
("created_at", "2026-01-01"),
("created_at", "2026-01-01T12:00:00"),
("created_at", None),
("traceparent", "not-w3c"),
("tracestate", "x" * 513),
("url", "http://invalid.example"),
("bytes", "secret-contents"),
("filename", "../../bad"),
],
)
def test_invalid_notification_is_rejected(field, value):
data = copy.deepcopy(example())
data[field] = value
with pytest.raises((ValueError, TypeError)):
validate_job(data)