diff options
| -rw-r--r-- | tests/bot/.testenv | 2 | ||||
| -rw-r--r-- | tests/bot/test_constants.py | 64 | 
2 files changed, 28 insertions, 38 deletions
| diff --git a/tests/bot/.testenv b/tests/bot/.testenv new file mode 100644 index 000000000..484c8809d --- /dev/null +++ b/tests/bot/.testenv @@ -0,0 +1,2 @@ +unittests_goat=volcyy +unittests_nested__server_name=pydis diff --git a/tests/bot/test_constants.py b/tests/bot/test_constants.py index 3492021ce..87933d59a 100644 --- a/tests/bot/test_constants.py +++ b/tests/bot/test_constants.py @@ -1,53 +1,41 @@ -import inspect -import typing -import unittest +import os +from pathlib import Path +from unittest import TestCase, mock -from bot import constants +from pydantic import BaseModel +from bot.constants import EnvConfig -def is_annotation_instance(value: typing.Any, annotation: typing.Any) -> bool: -    """ -    Return True if `value` is an instance of the type represented by `annotation`. +current_path = Path(__file__) +env_file_path = current_path.parent / ".testenv" -    This doesn't account for things like Unions or checking for homogenous types in collections. -    """ -    origin = typing.get_origin(annotation) -    # This is done in case a bare e.g. `typing.List` is used. -    # In such case, for the assertion to pass, the type needs to be normalised to e.g. `list`. -    # `get_origin()` does this normalisation for us. -    type_ = annotation if origin is None else origin +class TestEnvConfig( +    EnvConfig, +    env_file=env_file_path, +): +    """Our default configuration for models that should load from .env files.""" -    return isinstance(value, type_) +class NestedModel(BaseModel): +    server_name: str -def is_any_instance(value: typing.Any, types: typing.Collection) -> bool: -    """Return True if `value` is an instance of any type in `types`.""" -    return any(is_annotation_instance(value, type_) for type_ in types) +class _TestConfig(TestEnvConfig, env_prefix="unittests_"): -class ConstantsTests(unittest.TestCase): +    goat: str +    execution_env: str = "local" +    nested: NestedModel + + +class ConstantsTests(TestCase):      """Tests for our constants.""" +    @mock.patch.dict(os.environ, {"UNITTESTS_EXECUTION_ENV": "production"})      def test_section_configuration_matches_type_specification(self):          """"The section annotations should match the actual types of the sections.""" -        sections = ( -            cls -            for (name, cls) in inspect.getmembers(constants) -            if hasattr(cls, "section") and isinstance(cls, type) -        ) -        for section in sections: -            for name, annotation in section.__annotations__.items(): -                with self.subTest(section=section.__name__, name=name, annotation=annotation): -                    value = getattr(section, name) -                    origin = typing.get_origin(annotation) -                    annotation_args = typing.get_args(annotation) -                    failure_msg = f"{value} is not an instance of {annotation}" - -                    if origin is typing.Union: -                        is_instance = is_any_instance(value, annotation_args) -                        self.assertTrue(is_instance, failure_msg) -                    else: -                        is_instance = is_annotation_instance(value, annotation) -                        self.assertTrue(is_instance, failure_msg) +        testconfig = _TestConfig() +        self.assertEqual("volcyy", testconfig.goat) +        self.assertEqual("pydis", testconfig.nested.server_name) +        self.assertEqual("production", testconfig.execution_env) | 
