From 8261de11cc35ad179966ba557cf38faa4cf49378 Mon Sep 17 00:00:00 2001 From: David Liu Date: Sun, 1 Nov 2026 12:28:35 -0500 Subject: [PATCH] Added tests for create_autotest_settings_class create_autotest_settings_class is the main entry point for building the settings type but had no tests. Calling it with no tester classes failed with a confusing "reduce() of empty iterable" TypeError; it now raises a ValueError with a clear message. Also narrowed the tester_classes type hint to list[type[BaseTesterSettings]]. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/markus_autotesting_core/types/types.py | 8 ++- tests/test_types/test_core.py | 82 +++++++++++++++++++++- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad69cc..17044f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Changed `BaseTesterSettings.tester_type` to return the struct's msgspec tag, so it matches the encoded `tester_type` when a subclass sets `tag=` explicitly. - Rewrote the README with usage and development documentation. - Ran the tests in CI on Python 3.11 to 3.15, and added a ruff formatting check to CI. +- Made `create_autotest_settings_class` raise `ValueError` when given no tester classes, and added tests for it. ## [0.1.0] diff --git a/src/markus_autotesting_core/types/types.py b/src/markus_autotesting_core/types/types.py index a75a792..5e6cd40 100644 --- a/src/markus_autotesting_core/types/types.py +++ b/src/markus_autotesting_core/types/types.py @@ -30,12 +30,18 @@ class _AutotestSettings(Struct, kw_only=True): """An error message, populated when creating a test environment fails.""" -def create_autotest_settings_class(tester_classes: list[type]) -> type[Struct]: +def create_autotest_settings_class( + tester_classes: list[type[BaseTesterSettings]], +) -> type[Struct]: """Dynamically create an AutotestSettings data type. This uses _AutotestSettings as the base class, replacing the `testers` annotation with a union of the provided tester_classes. + + Raises ValueError if tester_classes is empty. """ + if not tester_classes: + raise ValueError("At least one tester class is required") return type( "AutotestSettings", (_AutotestSettings,), diff --git a/tests/test_types/test_core.py b/tests/test_types/test_core.py index 2d69d87..e6176aa 100644 --- a/tests/test_types/test_core.py +++ b/tests/test_types/test_core.py @@ -3,7 +3,11 @@ import msgspec import pytest -from markus_autotesting_core.types import BaseTestData, BaseTesterSettings +from markus_autotesting_core.types import ( + BaseTestData, + BaseTesterSettings, + create_autotest_settings_class, +) def test_base_test_data_fields(): @@ -138,3 +142,79 @@ def test_base_tester_settings_tester_type_matches_tag(cls, expected): assert settings.tester_type == expected assert msgspec.to_builtins(settings)["tester_type"] == expected + + +class AlphaTesterSettings(BaseTesterSettings): + """A tester class used to test create_autotest_settings_class.""" + + test_data: list[BaseTestData] + + +class BetaTesterSettings(BaseTesterSettings): + """A second tester class used to test create_autotest_settings_class.""" + + test_data: list[BaseTestData] + + +def test_create_autotest_settings_class_decodes_union(): + """Test that each tester is decoded as the class matching its tester_type.""" + settings_class = create_autotest_settings_class( + [AlphaTesterSettings, BetaTesterSettings] + ) + + decoded = msgspec.convert( + { + "testers": [ + {"tester_type": "beta", "test_data": [base_test_data()]}, + {"tester_type": "alpha", "test_data": [base_test_data(timeout=10)]}, + ], + "_user": "instructor", + }, + type=settings_class, + ) + + assert [type(tester) for tester in decoded.testers] == [ + BetaTesterSettings, + AlphaTesterSettings, + ] + assert decoded.testers[1].test_data[0].timeout == 10 + assert decoded._user == "instructor" + + +def test_create_autotest_settings_class_single_tester_class(): + """Test that a single tester class can be used.""" + settings_class = create_autotest_settings_class([AlphaTesterSettings]) + + decoded = msgspec.convert( + {"testers": [{"tester_type": "alpha", "test_data": [base_test_data()]}]}, + type=settings_class, + ) + + assert isinstance(decoded.testers[0], AlphaTesterSettings) + + +def test_create_autotest_settings_class_empty_testers_rejected(): + """Test that settings with an empty testers list are rejected.""" + settings_class = create_autotest_settings_class([AlphaTesterSettings]) + + with pytest.raises(msgspec.ValidationError): + msgspec.convert({"testers": []}, type=settings_class) + + +def test_create_autotest_settings_class_unknown_tester_type_rejected(): + """Test that a tester with a tester_type not in the union is rejected.""" + settings_class = create_autotest_settings_class( + [AlphaTesterSettings, BetaTesterSettings] + ) + + with pytest.raises(msgspec.ValidationError): + msgspec.convert( + {"testers": [{"tester_type": "gamma", "test_data": [base_test_data()]}]}, + type=settings_class, + ) + + +def test_create_autotest_settings_class_no_tester_classes(): + """Test that creating a settings class with no tester classes raises ValueError.""" + with pytest.raises(ValueError, match="At least one tester class is required"): + create_autotest_settings_class([])