Files
OBLITERATUS/tests/test_study_presets.py
Joseph Magly 7f999505f0 test(release): restore touched-module coverage and raise the Quality depth timeout to 60 min
Adds focused tests for the lines the release run left uncovered in
evaluation/evaluator.py, recipes/search.py, recipes/search_cli.py and
study_presets.py after #676/#677, so touched-module line and branch coverage
is back at or above the comparison base (run 45690313651). Raises the Quality
depth job timeout from 45 to 60 minutes; the 85% mutation floor and every
other threshold are unchanged.

Refs #679
2026-11-25 16:13:34 -05:00

174 lines
7.0 KiB
Python

"""Tests for ablation presets."""
from __future__ import annotations
from obliteratus.study_presets import (
STUDY_PRESETS,
get_study_preset,
get_preset,
list_study_presets,
list_presets,
)
from obliteratus.config import StudyConfig
class TestPresets:
def test_all_presets_registered(self):
expected_keys = {"quick", "full", "attention", "layers", "knowledge", "pruning", "embeddings", "jailbreak", "guardrail", "robustness"}
assert expected_keys.issubset(set(STUDY_PRESETS.keys()))
def test_get_preset(self):
preset = get_study_preset("quick")
assert preset.name == "Quick Scan"
assert preset.key == "quick"
assert len(preset.strategies) == 2
def test_get_preset_alias(self):
preset = get_preset("quick")
assert preset.name == "Quick Scan"
def test_get_unknown_preset_raises(self):
import pytest
with pytest.raises(KeyError, match="Unknown preset"):
get_study_preset("nonexistent")
def test_list_presets(self):
presets = list_study_presets()
assert len(presets) >= 7
keys = [p.key for p in presets]
assert "quick" in keys
assert "full" in keys
def test_list_presets_alias(self):
assert list_presets() == list_study_presets()
def test_preset_strategies_are_valid(self):
from obliteratus.strategies import STRATEGY_REGISTRY
for preset in list_study_presets():
for s in preset.strategies:
assert s["name"] in STRATEGY_REGISTRY, (
f"Preset {preset.key!r} references unknown strategy {s['name']!r}"
)
class TestConfigWithPreset:
def test_preset_key_in_config(self):
config_dict = {
"preset": "quick",
"model": {"name": "gpt2", "task": "causal_lm", "dtype": "float32", "device": "cpu"},
"dataset": {"name": "wikitext", "subset": "wikitext-2-raw-v1", "split": "test", "text_column": "text"},
}
config = StudyConfig.from_dict(config_dict)
# Should inherit strategies from the quick preset
assert len(config.strategies) == 2
strategy_names = [s.name for s in config.strategies]
assert "layer_removal" in strategy_names
assert "ffn_ablation" in strategy_names
# Should inherit max_samples
assert config.dataset.max_samples == 25
# Should inherit batch_size and max_length
assert config.batch_size == 4
assert config.max_length == 128
def test_legacy_study_preset_key_still_works(self):
config_dict = {
"study_preset": "quick",
"model": {"name": "gpt2", "task": "causal_lm", "dtype": "float32", "device": "cpu"},
"dataset": {"name": "wikitext", "subset": "wikitext-2-raw-v1", "split": "test", "text_column": "text"},
}
config = StudyConfig.from_dict(config_dict)
assert len(config.strategies) == 2
def test_preset_can_be_overridden(self):
config_dict = {
"preset": "quick",
"model": {"name": "gpt2", "task": "causal_lm", "dtype": "float32", "device": "cpu"},
"dataset": {"name": "wikitext", "subset": "wikitext-2-raw-v1", "split": "test", "text_column": "text", "max_samples": 999},
"batch_size": 16,
"strategies": [{"name": "head_pruning", "params": {}}],
}
config = StudyConfig.from_dict(config_dict)
# Explicit strategies should override preset
assert len(config.strategies) == 1
assert config.strategies[0].name == "head_pruning"
# Explicit batch_size should override
assert config.batch_size == 16
# Explicit max_samples in dataset should be kept
assert config.dataset.max_samples == 999
def test_full_preset(self):
config_dict = {
"preset": "full",
"model": {"name": "gpt2", "task": "causal_lm", "dtype": "float32", "device": "cpu"},
"dataset": {"name": "wikitext", "subset": "wikitext-2-raw-v1", "split": "test", "text_column": "text"},
}
config = StudyConfig.from_dict(config_dict)
assert len(config.strategies) == 4
strategy_names = {s.name for s in config.strategies}
assert strategy_names == {"layer_removal", "head_pruning", "ffn_ablation", "embedding_ablation"}
class TestPresetIsolation:
"""#666: callers get copies, so editing one cannot change the shared preset (#637)."""
def test_get_study_preset_returns_an_independent_copy(self):
preset = get_study_preset("quick")
preset.strategies[0]["params"]["layer"] = 0
preset.strategies.append({"name": "head_pruning", "params": {}})
preset.metrics.append("accuracy")
preset.tags.append("edited")
preset.max_samples = 1
fresh = get_study_preset("quick")
assert [s["params"] for s in fresh.strategies] == [{}, {}]
assert fresh.metrics == ["perplexity"]
assert "edited" not in fresh.tags
assert fresh.max_samples == 25
assert fresh == STUDY_PRESETS["quick"]
def test_list_study_presets_returns_independent_copies(self):
for preset in list_study_presets():
preset.strategies.clear()
preset.metrics.clear()
assert all(p.strategies and p.metrics for p in list_study_presets())
assert all(p.strategies and p.metrics for p in STUDY_PRESETS.values())
assert list_presets() == list_study_presets()
class TestPresetRegistryIsReadOnly:
"""#675 B5: the public registry is a read-only mapping that hands out copies."""
def test_registry_rejects_writes(self):
import pytest
with pytest.raises(TypeError):
STUDY_PRESETS["custom"] = get_study_preset("quick")
with pytest.raises(TypeError):
del STUDY_PRESETS["quick"]
assert "custom" not in STUDY_PRESETS
assert "quick" in STUDY_PRESETS
def test_registry_values_cannot_change_the_shared_preset(self):
STUDY_PRESETS["quick"].metrics.append("accuracy")
for preset in STUDY_PRESETS.values():
preset.strategies.clear()
assert STUDY_PRESETS["quick"].metrics == ["perplexity"]
assert get_study_preset("quick").metrics == ["perplexity"]
assert all(p.strategies for p in STUDY_PRESETS.values())
def test_registry_readers_keep_working(self):
assert list(STUDY_PRESETS) == [p.key for p in list_study_presets()]
assert len(STUDY_PRESETS) == len(list_study_presets())
assert dict(STUDY_PRESETS.items()) == {p.key: p for p in list_study_presets()}
assert STUDY_PRESETS.get("missing") is None
assert sorted(STUDY_PRESETS) == sorted(p.key for p in list_presets())
def test_registry_repr_names_the_presets_without_exposing_a_dict(self):
"""#679: the read-only view reports its keys, not a mutable dict or a preset copy."""
text = repr(STUDY_PRESETS)
assert text == f"STUDY_PRESETS({sorted(p.key for p in list_study_presets())!r})"
assert not isinstance(STUDY_PRESETS, dict)