-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_loader.py
More file actions
148 lines (112 loc) · 5.06 KB
/
Copy pathtest_loader.py
File metadata and controls
148 lines (112 loc) · 5.06 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Loading valid and invalid documents: fail closed, aggregate errors, migrate versions."""
from __future__ import annotations
import importlib.resources
import json
from pathlib import Path
from typing import Any
import pytest
from openjobradar.config import ConfigError, ConfigValidationError, load_profile, load_settings
from openjobradar.config.defaults import PLATFORM_SETTINGS_DEFAULTS
from openjobradar.config.loader import normalize
EXAMPLES = Path(__file__).resolve().parents[1] / "examples"
def _schema(kind: str) -> dict:
raw = (importlib.resources.files("openjobradar.schemas") / f"{kind}.v2.json").read_text(
encoding="utf-8"
)
return json.loads(raw)
def test_loads_current_example_profile() -> None:
profile = load_profile(EXAMPLES / "profiles" / "jordan-lee.v2.yaml")
assert profile["candidate"]["display_name"] == "Jordan Lee"
assert profile["targeting"]["disciplines"] == ["data"]
assert profile["location_policy"]["remote_only"] is True
def test_loads_design_and_research_example() -> None:
profile = load_profile(EXAMPLES / "profiles" / "priya-sharma.v2.yaml")
assert set(profile["targeting"]["disciplines"]) == {"design", "research"}
assert profile["candidate"]["skills"]
assert profile["hard_filters"]["comp_floor_usd"] == 110000
def test_loads_current_example_settings() -> None:
settings = load_settings(EXAMPLES / "settings" / "settings.example.yaml")
assert settings["alerts"]["alert_threshold"] == 82
def test_platform_defaults_validate_against_settings_schema() -> None:
normalize("settings", dict(PLATFORM_SETTINGS_DEFAULTS))
def test_migrates_prototype_v1_profile() -> None:
migrated = load_profile(EXAMPLES / "profiles" / "alex-rivera.v1.yaml")
assert migrated["schema_version"] == 2
assert migrated["candidate"]["display_name"] == "Alex Rivera"
assert migrated["targeting"]["seniority_targets"] == [
"Director of Engineering",
"VP of Engineering",
]
assert migrated["legacy_hard_limits"]["location"] == "Remote (US) preferred."
assert "current_employers" not in migrated.get("hard_filters", {})
def test_rejects_unknown_top_level_key() -> None:
document: dict[str, Any] = {
"schema_version": 2,
"candidate": {"display_name": "Sam Query", "profile_summary": "x" * 40},
"targeting": {"target_domains": ["anything"]},
"totally_unknown": True,
}
with pytest.raises(ConfigValidationError) as excinfo:
normalize("profile", document)
assert "totally_unknown" in str(excinfo.value)
def test_aggregates_multiple_errors_in_one_message() -> None:
with pytest.raises(ConfigValidationError) as excinfo:
normalize(
"profile",
{
"schema_version": 2,
"candidate": {"display_name": "", "profile_summary": "x" * 200, "tech_stack": [1]},
"targeting": {},
},
)
assert len(excinfo.value.errors) >= 3
def test_future_schema_version_names_the_fix() -> None:
with pytest.raises(ConfigError, match="upgrade openjobradar"):
normalize(
"profile",
{
"schema_version": 99,
"candidate": {"display_name": "A", "profile_summary": "B" * 30},
"targeting": {"target_domains": ["d"]},
},
)
def test_string_schema_version_gets_migration_hint() -> None:
with pytest.raises(ConfigValidationError, match="migrate automatically"):
normalize("profile", {"schema_version": "example-v1.0"})
def test_invalid_yaml_reports_file_and_reason(tmp_path: Path) -> None:
bad = tmp_path / "broken.yaml"
bad.write_text("alerts: [unclosed", encoding="utf-8")
with pytest.raises(ConfigError, match=r"broken\.yaml"):
load_settings(bad)
def test_non_mapping_top_level_fails_closed(tmp_path: Path) -> None:
listy = tmp_path / "listy.yaml"
listy.write_text("- a\n- b\n", encoding="utf-8")
with pytest.raises(ConfigValidationError, match="expected a mapping"):
load_profile(listy)
def test_rejects_unknown_discipline() -> None:
with pytest.raises(ConfigValidationError, match="disciplines"):
normalize(
"profile",
{
"schema_version": 2,
"candidate": {"display_name": "A", "profile_summary": "B" * 30},
"targeting": {"target_domains": ["d"], "disciplines": ["ninja_dev"]},
},
)
def test_rejects_boolean_schema_version() -> None:
with pytest.raises(ConfigValidationError, match="must be an integer"):
normalize("profile", {"schema_version": True})
def test_settings_v1_has_no_migration_path() -> None:
with pytest.raises(ConfigError, match="no migration path"):
normalize("settings", {"legacy": True})
def test_profile_schema_declares_expected_sections() -> None:
schema = _schema("profile")
assert set(schema["properties"]) == {
"schema_version",
"candidate",
"targeting",
"location_policy",
"hard_filters",
"values_screening",
"legacy_hard_limits",
}