Files
mtusi2026lab4/student-tests/unit/test_scaling.py
T
Artyom 8e4532f6f5 Forward-port lab3 SQLAlchemy and Alembic foundation
Carry source f591fa64e525679a31c9db7f942d36c789e1a12f forward from 72a1e57. Preserve lab4 attachment transactions, reconciliation locks, quarantine, and existing schema data through verified legacy adoption. Keep report and evidence work in progress untouched.
2026-11-06 01:47:30 +03:00

81 lines
2.9 KiB
Python

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