Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
8 changes: 7 additions & 1 deletion src/markus_autotesting_core/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,),
Expand Down
82 changes: 81 additions & 1 deletion tests/test_types/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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([])
Loading