heretic/tests/test_config.py
Andrew Barnes e7b783ed85
Some checks are pending
CI / Check and build (Python 3.10) (push) Waiting to run
CI / Check and build (Python 3.11) (push) Waiting to run
CI / Check and build (Python 3.12) (push) Waiting to run
CI / Check and build (Python 3.13) (push) Waiting to run
fix: validate scorer instance names in config (#407)
* fix: validate scorer instance names in config

* fix: run scorer config tests in CI

* fix: drop redundant UV_PYTHON env and quote unittest pattern

* fix: report precise scorer name validation errors
2026-07-15 20:28:10 +05:30

51 lines
1.8 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
import unittest
from pydantic import ValidationError
from heretic.config import ScorerConfig
class ScorerConfigTests(unittest.TestCase):
def test_accepts_slug_like_instance_name(self) -> None:
config = ScorerConfig(
plugin="heretic.scorers.keyword_rate.KeywordRate",
optimization="minimize",
instance_name="small-1",
)
self.assertEqual(config.instance_name, "small-1")
def test_rejects_empty_instance_name(self) -> None:
with self.assertRaises(ValidationError):
ScorerConfig(
plugin="heretic.scorers.keyword_rate.KeywordRate",
optimization="minimize",
instance_name=" \t",
)
def test_rejects_whitespace_in_instance_name(self) -> None:
for instance_name in ["small name", "small\tname", "small\nname"]:
with self.subTest(instance_name=instance_name):
with self.assertRaisesRegex(
ValidationError, "whitespace is not allowed"
):
ScorerConfig(
plugin="heretic.scorers.keyword_rate.KeywordRate",
optimization="minimize",
instance_name=instance_name,
)
def test_rejects_dot_in_instance_name(self) -> None:
with self.assertRaisesRegex(ValidationError, "'\\.' is not allowed"):
ScorerConfig(
plugin="heretic.scorers.keyword_rate.KeywordRate",
optimization="minimize",
instance_name="small.name",
)
if __name__ == "__main__":
unittest.main()