Forward-port lab3 SQLAlchemy and Alembic foundation
Carry sourcef591fa64e5forward from72a1e57. Preserve lab4 attachment transactions, reconciliation locks, quarantine, and existing schema data through verified legacy adoption. Keep report and evidence work in progress untouched.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location(
|
||||
"grade4_probe", Path(__file__).resolve().parents[2] / "scripts/probe.py"
|
||||
)
|
||||
assert SPEC is not None and SPEC.loader is not None
|
||||
probe = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(probe)
|
||||
ARGS = SimpleNamespace(
|
||||
label="old", command=[], expect="replace", max_failed_ratio=0.01, max_recovery=10
|
||||
)
|
||||
|
||||
|
||||
def result_of(probes: list[tuple]) -> dict:
|
||||
result = probe.summarize(
|
||||
ARGS, probes, [(99, 2), (101, 2)], {"start": 100, "end": 101, "exit": 0, "load_end": 120}, 2
|
||||
)
|
||||
result["k6"] = {"exit": 0, "failed_ratio": 0, "requests": 100, "dropped_iterations": 0}
|
||||
result["history"] = {"exact_match": True, "unique": True}
|
||||
return result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("attempts", "recovery", "passed"),
|
||||
[
|
||||
([(100, 503, 20, "old")], None, False),
|
||||
([(100, 503, 20, "old"), (109, 201, 2000, "new")], 11, False),
|
||||
([(100, 503, 20, "old"), (101, 201, 500, "new")], 1.5, True),
|
||||
([(101, 201, 500, "new")], 0, True),
|
||||
([], 0, False),
|
||||
],
|
||||
)
|
||||
def test_recovery_requires_completed_success(attempts: list, recovery: float, passed: bool) -> None:
|
||||
result = result_of([(99, 201, 1, "old"), *attempts])
|
||||
assert result["probe"]["recovery_s"] == recovery
|
||||
assert probe.verdict(ARGS, result)["write_recovery_ok"] is passed
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field", "value", "check"),
|
||||
[("requests", 0, "load_delivered"), ("dropped_iterations", 1, "load_delivered")],
|
||||
)
|
||||
def test_incomplete_load_fails(field: str, value: int, check: str) -> None:
|
||||
result = result_of([(99, 201, 1, "old"), (102, 201, 1, "new")])
|
||||
result["k6"][field] = value
|
||||
assert not probe.verdict(ARGS, result)[check]
|
||||
|
||||
|
||||
def test_missing_lb_sample_fails() -> None:
|
||||
result = result_of([(99, 201, 1, "old"), (102, 201, 1, "new")])
|
||||
result["lb_samples_missing"] = 1
|
||||
assert not probe.verdict(ARGS, result)["lb_observed"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("corruption", ["none", "missing", "duplicate", "changed", "http", "cycle"])
|
||||
def test_full_history_matches_confirmed_bodies(
|
||||
monkeypatch: pytest.MonkeyPatch, corruption: str
|
||||
) -> None:
|
||||
messages = [{"id": str(n), "text": "original"} for n in range(101)]
|
||||
stored = list(reversed(messages))
|
||||
if corruption == "missing":
|
||||
stored.pop()
|
||||
if corruption == "duplicate":
|
||||
stored.append(stored[0])
|
||||
if corruption == "changed":
|
||||
stored[0] = dict(stored[0], text="changed")
|
||||
pages = iter(
|
||||
[
|
||||
(200, "", {"items": stored[:100], "next_cursor": "next"}),
|
||||
(
|
||||
503 if corruption == "http" else 200,
|
||||
"",
|
||||
{"items": stored[100:], "next_cursor": "next" if corruption == "cycle" else None},
|
||||
),
|
||||
]
|
||||
)
|
||||
monkeypatch.setattr(probe, "call", lambda *_a, **_kw: next(pages))
|
||||
result = probe.history_check({"auth": {}}, "chat", messages)
|
||||
assert (result["exact_match"] and result["unique"]) is (corruption == "none")
|
||||
@@ -0,0 +1,80 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def scaling(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
|
||||
scripts = Path(__file__).resolve().parents[2] / "scripts"
|
||||
monkeypatch.syspath_prepend(str(scripts))
|
||||
spec = importlib.util.spec_from_file_location("scaling", scripts / "scaling.py")
|
||||
assert spec is not None and spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_retry_preserves_previous_raw_data(scaling: ModuleType, tmp_path: Path) -> None:
|
||||
base = {"variant": "base", "config": "1x1"}
|
||||
first = scaling.attempt_folder(tmp_path, base, 100, 1)
|
||||
(first / "k6.txt").write_text("failed attempt")
|
||||
second = scaling.attempt_folder(tmp_path, base, 100, 1)
|
||||
assert first != second
|
||||
assert (first / "k6.txt").read_text() == "failed attempt"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("recover", [False, True])
|
||||
def test_disturbed_runs_never_enter_summary(
|
||||
scaling: ModuleType, monkeypatch: pytest.MonkeyPatch, recover: bool
|
||||
) -> None:
|
||||
args = SimpleNamespace(
|
||||
base_env={}, out="unused", retries=1, pause=0, rates="100", rejected=Mock()
|
||||
)
|
||||
rejected = {"raw_dir": "attempt1", "disturbed": True, "saturated": False}
|
||||
accepted = {"raw_dir": "attempt2", "disturbed": False, "saturated": False}
|
||||
monkeypatch.setattr(scaling, "layout", lambda *_: ({}, {}))
|
||||
monkeypatch.setattr(scaling, "apply", lambda *_: {})
|
||||
monkeypatch.setattr(scaling, "deployment", lambda *_: [])
|
||||
monkeypatch.setattr(scaling, "wait_quiet", lambda *_: None)
|
||||
monkeypatch.setattr(
|
||||
scaling, "step", Mock(side_effect=[rejected, accepted if recover else rejected])
|
||||
)
|
||||
output = Mock()
|
||||
if recover:
|
||||
scaling.block(args, output, "1x1", "base", [1])
|
||||
output.writerows.assert_called_once_with([accepted])
|
||||
args.rejected.writerow.assert_called_once_with(rejected)
|
||||
else:
|
||||
with pytest.raises(SystemExit, match="exhausted retries"):
|
||||
scaling.block(args, output, "1x1", "base", [1])
|
||||
output.writerows.assert_not_called()
|
||||
assert args.rejected.writerow.call_count == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("fixed", "expected"),
|
||||
[
|
||||
(True, [("4x1", [2]), ("8x0.5", [2]), ("4x1", [3]), ("8x0.5", [3])]),
|
||||
(False, [("4x1", [2, 3]), ("8x0.5", [2, 3])]),
|
||||
],
|
||||
)
|
||||
def test_series_order(
|
||||
scaling: ModuleType,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
fixed: bool,
|
||||
expected: list[tuple[str, list[int]]],
|
||||
) -> None:
|
||||
args = SimpleNamespace(
|
||||
first_repeat=2,
|
||||
repeats=2,
|
||||
configs="4x1,8x0.5",
|
||||
variants=["pinned"],
|
||||
rates="3040,4560" if fixed else None,
|
||||
)
|
||||
block = Mock()
|
||||
monkeypatch.setattr(scaling, "block", block)
|
||||
scaling.series(args, Mock())
|
||||
assert [(call.args[2], list(call.args[4])) for call in block.call_args_list] == expected
|
||||
Reference in New Issue
Block a user