diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e0c569..a8b5e67 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: pip install -e '.[dev,attr,opyoid]' - name: Test with pytest run: | - py.test --cov-report xml --cov-report term --cov=envipy ./tests + py.test --cov-report xml --cov-report term --cov=enviparse ./tests - name: Codecov if: matrix.python-version == '3.11' uses: codecov/codecov-action@v4 @@ -57,7 +57,7 @@ jobs: black . --check -l 120 - name: Lint with pylint run: | - pylint envipy + pylint enviparse pylint tests --disable=too-many-public-methods,too-many-instance-attributes,too-many-lines deploy: diff --git a/.gitignore b/.gitignore index c7a2628..4f087fb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ __pycache__ coverage.xml .coverage -envipy.egg-info/* \ No newline at end of file +enviparse.egg-info/* \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index f28d9f0..b5e049c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.0 ### Breaking changes -- Update project name to `envipy` +- Update project name to `enviparse` ## 1.1.1 ### Fixes diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f557133..ed940f2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,7 +3,7 @@ We welcome everyone to help us on this project. All bug reports, feature requests and questions should be filed at the -[GitHub issues page](https://github.com/illuin-tech/envipy/issues). +[GitHub issues page](https://github.com/illuin-tech/enviparse/issues). You can open your own Pull Request after discussing the need for those changes in an issue. @@ -21,6 +21,6 @@ Run `python -m unitttest discover` to run the tests. Run these commands to check the files linting: ```shell script -pylint envipy +pylint enviparse pylint tests --disable=too-many-public-methods,too-many-instance-attributes ``` \ No newline at end of file diff --git a/README.md b/README.md index d46f4a8..8936ec6 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -# Envipy +# Enviparse -![CI](https://github.com/illuin-tech/envipy/workflows/CI/badge.svg) -[![codecov](https://codecov.io/gh/illuin-tech/envipy/branch/main/graph/badge.svg)](https://codecov.io/gh/illuin-tech/envipy) +![CI](https://github.com/illuin-tech/enviparse/workflows/CI/badge.svg) +[![codecov](https://codecov.io/gh/illuin-tech/enviparse/branch/main/graph/badge.svg)](https://codecov.io/gh/illuin-tech/enviparse) ## Description -Envipy let you simply create dataclasses from environment variable. +Enviparse let you simply create dataclasses from environment variable. Supported types are : * int @@ -34,7 +34,7 @@ You can parse environment variable with : ```python import dataclasses -from envipy import Envipy +from enviparse import Enviparse @dataclasses.dataclass @@ -46,7 +46,7 @@ class DatabaseConfig: database_name: str -db_config = Envipy().envipy("DATABASE_CONFIG", DatabaseConfig) +db_config = Enviparse().parse("DATABASE_CONFIG", DatabaseConfig) print(db_config) ``` diff --git a/enviparse/__init__.py b/enviparse/__init__.py new file mode 100644 index 0000000..67239dd --- /dev/null +++ b/enviparse/__init__.py @@ -0,0 +1 @@ +from .enviparse import Enviparse diff --git a/envipy/envipy.py b/enviparse/enviparse.py similarity index 93% rename from envipy/envipy.py rename to enviparse/enviparse.py index 5e1e582..46970b2 100644 --- a/envipy/envipy.py +++ b/enviparse/enviparse.py @@ -21,7 +21,7 @@ ClassTypeT = TypeVar("ClassTypeT") -class Envipy: +class Enviparse: def __init__( self, concat_env_name_func: Optional[Callable[[str, str], str]] = None, @@ -30,7 +30,7 @@ def __init__( lambda prefix, suffix: f"{prefix.upper()}_{suffix.upper()}" ) - def envipy( # pylint: disable=too-many-return-statements + def parse( # pylint: disable=too-many-return-statements self, prefix: str, t_type: Type[ClassTypeT], @@ -127,7 +127,7 @@ def _get_list_type_from_env( index = 0 while self._has_env_var_with_prefix(self._concat_env_name_func(prefix, str(index))): list_item_env_var_name_prefix = self._concat_env_name_func(prefix, str(index)) - item_env_var_value = self.envipy(list_item_env_var_name_prefix, list_item_type) + item_env_var_value = self.parse(list_item_env_var_name_prefix, list_item_type) values.append(item_env_var_value) index += 1 return values @@ -140,7 +140,7 @@ def _get_optional_type_from_env( type_hints = get_args(attr_class) optional_type = type_hints[0] if self._has_env_var_with_prefix(prefix): - return self.envipy(prefix, optional_type) + return self.parse(prefix, optional_type) return None def _get_dataclass_from_env( @@ -154,7 +154,7 @@ def _get_dataclass_from_env( field_env_var_prefix = self._concat_env_name_func(prefix, field.name) try: - field_values[field.name] = self.envipy(prefix=field_env_var_prefix, t_type=field.type) + field_values[field.name] = self.parse(prefix=field_env_var_prefix, t_type=field.type) except MissingEnvironmentVariableError as error: if field.default is not None and field.default is not dataclasses.MISSING: field_values[field.name] = field.default @@ -174,7 +174,7 @@ def _get_attr_class_from_env( field_env_var_prefix = self._concat_env_name_func(prefix, field_name) try: - field_values[field_name] = self.envipy(prefix=field_env_var_prefix, t_type=field.type) + field_values[field_name] = self.parse(prefix=field_env_var_prefix, t_type=field.type) except MissingEnvironmentVariableError as error: if field.default is not None and field.default is not attr.NOTHING: field_values[field_name] = field.default diff --git a/envipy/errors.py b/enviparse/errors.py similarity index 71% rename from envipy/errors.py rename to enviparse/errors.py index f446c55..a4d623f 100644 --- a/envipy/errors.py +++ b/enviparse/errors.py @@ -1,27 +1,27 @@ -class EnvipyError(Exception): +class EnviparseError(Exception): pass -class UnexpectedTypeError(EnvipyError): +class UnexpectedTypeError(EnviparseError): def __init__(self, used_type: str, path: str): super().__init__(f'Unsupported type "{used_type}" for property at path "{path}"') -class UnknownTypeError(EnvipyError): +class UnknownTypeError(EnviparseError): def __init__(self, path: str): super().__init__(f'Unknown generic type for property at path "{path}"') -class MissingEnvironmentVariableError(EnvipyError): +class MissingEnvironmentVariableError(EnviparseError): def __init__(self, env_var_name: str): super().__init__(f"Environment variable '{env_var_name}' is not set.") -class NestedMissingEnvironmentVariableError(EnvipyError): +class NestedMissingEnvironmentVariableError(EnviparseError): def __init__(self, env_var_name: str): super().__init__(f"Environment variable '{env_var_name}' is not set.") -class CastError(EnvipyError): +class CastError(EnviparseError): def __init__(self, env_var_name: str, data_type: str): super().__init__(f"Failed to convert '{env_var_name}' to {data_type}.") diff --git a/enviparse/opyoid.py b/enviparse/opyoid.py new file mode 100644 index 0000000..28010e2 --- /dev/null +++ b/enviparse/opyoid.py @@ -0,0 +1,16 @@ +from typing import Type, Optional + +from opyoid import Provider + +from .enviparse import ClassTypeT, Enviparse + + +def enviparse_provider(prefix: str, config_type: Type[ClassTypeT]) -> Type[Provider[ClassTypeT]]: + class EnviparseProvider(Provider[ClassTypeT]): + def __init__(self, enviparse: Optional[Enviparse] = None): + self._parser = enviparse or Enviparse() + + def get(self) -> ClassTypeT: + return self._parser.parse(prefix, config_type) + + return EnviparseProvider diff --git a/envipy/__init__.py b/envipy/__init__.py deleted file mode 100644 index 4b2930d..0000000 --- a/envipy/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .envipy import Envipy diff --git a/envipy/opyoid.py b/envipy/opyoid.py deleted file mode 100644 index 210765f..0000000 --- a/envipy/opyoid.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import Type, Optional - -from opyoid import Provider - -from .envipy import ClassTypeT, Envipy - - -def envipy_provider(prefix: str, config_type: Type[ClassTypeT]) -> Type[Provider[ClassTypeT]]: - class EnvipyProvider(Provider[ClassTypeT]): - def __init__(self, envipy: Optional[Envipy] = None): - self._envipy = envipy or Envipy() - - def get(self) -> ClassTypeT: - return self._envipy.envipy(prefix, config_type) - - return EnvipyProvider diff --git a/pyproject.toml b/pyproject.toml index be03e6e..47332c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] -name = "envipy" +name = "enviparse" dynamic = ["version"] -description = "envipy help you manage your application properties using environment variabl" +description = "enviparse help you manage your application properties using environment variabl" authors = [ {name = "Illuin technology", email = "contact@illuin.tech"}, ] diff --git a/tests/test_envipy.py b/tests/test_enviparse.py similarity index 63% rename from tests/test_envipy.py rename to tests/test_enviparse.py index c6b150b..c284a5e 100644 --- a/tests/test_envipy.py +++ b/tests/test_enviparse.py @@ -6,8 +6,8 @@ import attr -from envipy import Envipy -from envipy.errors import ( +from enviparse import Enviparse +from enviparse.errors import ( CastError, MissingEnvironmentVariableError, UnexpectedTypeError, @@ -16,12 +16,12 @@ ) -class TestEnvipy(unittest.TestCase): +class TestEnviparse(unittest.TestCase): test_prefix = "TEST_ENV_VARS" def setUp(self) -> None: self.clear_test_env_vars() - self.envipy = Envipy() + self.enviparse = Enviparse() def tearDown(self) -> None: self.clear_test_env_vars() @@ -31,7 +31,7 @@ def clear_test_env_vars(self) -> None: if env.startswith(self.test_prefix): del os.environ[env] - def test_envipy_with_attr_class_should_return_using_attr_field_default_value_if_none_env_var(self): + def test_parse_with_attr_class_should_return_using_attr_field_default_value_if_none_env_var(self): @attr.s(auto_attribs=True) class AttrClass: a: int @@ -43,10 +43,10 @@ class AttrClass: a=1, b=5, ), - self.envipy.envipy(self.test_prefix, AttrClass), + self.enviparse.parse(self.test_prefix, AttrClass), ) - def test_envipy_should_return_with_nested_attr_class(self): + def test_parse_should_return_with_nested_attr_class(self): @attr.s(auto_attribs=True) class ChildClass: field: str @@ -57,10 +57,10 @@ class ParentClass: os.environ[f"{self.test_prefix}_CHILD_FIELD"] = "value" self.assertEqual( - ParentClass(child=ChildClass(field="value")), self.envipy.envipy(self.test_prefix, ParentClass) + ParentClass(child=ChildClass(field="value")), self.enviparse.parse(self.test_prefix, ParentClass) ) - def test_envipy_with_attr_class_should_return_using_attr_with_nested_list(self): + def test_parse_with_attr_class_should_return_using_attr_with_nested_list(self): @attr.s(auto_attribs=True) class AttrClass: b: List[str] = None @@ -74,60 +74,60 @@ class AttrClass: "value 2", ] ), - self.envipy.envipy(self.test_prefix, AttrClass), + self.enviparse.parse(self.test_prefix, AttrClass), ) - def test_envipy_with_attr_class_should_return_using_field_with_optional(self): + def test_parse_with_attr_class_should_return_using_field_with_optional(self): @attr.s(auto_attribs=True) class AttrClass: b: Optional[str] - self.assertEqual(AttrClass(b=None), self.envipy.envipy(self.test_prefix, AttrClass)) + self.assertEqual(AttrClass(b=None), self.enviparse.parse(self.test_prefix, AttrClass)) - def test_envipy_with_attr_class_should_return_using_optional_field_with_default_value(self): + def test_parse_with_attr_class_should_return_using_optional_field_with_default_value(self): @attr.s(auto_attribs=True) class AttrClass: b: Optional[str] = attr.Factory(lambda: "value") - self.assertEqual(AttrClass(b=None), self.envipy.envipy(self.test_prefix, AttrClass)) + self.assertEqual(AttrClass(b=None), self.enviparse.parse(self.test_prefix, AttrClass)) - def test_envipy_with_attr_class_should_return_using_attr_with_optional_and_not_optional(self): + def test_parse_with_attr_class_should_return_using_attr_with_optional_and_not_optional(self): @attr.s(auto_attribs=True) class AttrClass: a: str b: Optional[str] os.environ[f"{self.test_prefix}_A"] = "Hello" - self.assertEqual(AttrClass(a="Hello", b=None), self.envipy.envipy(self.test_prefix, AttrClass)) + self.assertEqual(AttrClass(a="Hello", b=None), self.enviparse.parse(self.test_prefix, AttrClass)) - def test_envipy_should_return_for_primitive_types(self): + def test_parse_should_return_for_primitive_types(self): os.environ[f"{self.test_prefix}_BOOL_TRUE"] = "true" - self.assertEqual(True, self.envipy.envipy(f"{self.test_prefix}_BOOL_TRUE", bool)) + self.assertEqual(True, self.enviparse.parse(f"{self.test_prefix}_BOOL_TRUE", bool)) os.environ[f"{self.test_prefix}_BOOL_FALSE"] = "false" - self.assertEqual(False, self.envipy.envipy(f"{self.test_prefix}_BOOL_FALSE", bool)) + self.assertEqual(False, self.enviparse.parse(f"{self.test_prefix}_BOOL_FALSE", bool)) os.environ[f"{self.test_prefix}_STR"] = "str" - self.assertEqual("str", self.envipy.envipy(f"{self.test_prefix}_STR", str)) + self.assertEqual("str", self.enviparse.parse(f"{self.test_prefix}_STR", str)) os.environ[f"{self.test_prefix}_INT"] = "1993" - self.assertEqual(1993, self.envipy.envipy(f"{self.test_prefix}_INT", int)) + self.assertEqual(1993, self.enviparse.parse(f"{self.test_prefix}_INT", int)) os.environ[f"{self.test_prefix}_FLOAT"] = "3.14" - self.assertEqual(3.14, self.envipy.envipy(f"{self.test_prefix}_FLOAT", float)) + self.assertEqual(3.14, self.enviparse.parse(f"{self.test_prefix}_FLOAT", float)) - def test_envipy_should_raise_cast_error_for_primitive_type_if_no_cast(self): + def test_parse_should_raise_cast_error_for_primitive_type_if_no_cast(self): os.environ[f"{self.test_prefix}_BOOL"] = "unknown" with self.assertRaises(CastError) as expected: - self.envipy.envipy(f"{self.test_prefix}_BOOL", bool) + self.enviparse.parse(f"{self.test_prefix}_BOOL", bool) self.assertEqual(f"Failed to convert '{self.test_prefix}_BOOL' to bool.", str(expected.exception)) os.environ[f"{self.test_prefix}_INT"] = "str" with self.assertRaises(CastError) as expected: - self.envipy.envipy(f"{self.test_prefix}_INT", int) + self.enviparse.parse(f"{self.test_prefix}_INT", int) self.assertEqual(f"Failed to convert '{self.test_prefix}_INT' to int.", str(expected.exception)) def test_should_raise_missing_env_error_if_env_var_is_not_set(self): with self.assertRaises(MissingEnvironmentVariableError) as expected: - self.envipy.envipy(f"{self.test_prefix}_BOOL", bool) + self.enviparse.parse(f"{self.test_prefix}_BOOL", bool) self.assertEqual(f"Environment variable '{self.test_prefix}_BOOL' is not set.", str(expected.exception)) with self.assertRaises(MissingEnvironmentVariableError) as expected: - self.envipy.envipy(f"{self.test_prefix}_INT", int) + self.enviparse.parse(f"{self.test_prefix}_INT", int) self.assertEqual(f"Environment variable '{self.test_prefix}_INT' is not set.", str(expected.exception)) @attr.s(auto_attribs=True) @@ -135,7 +135,7 @@ class AttrClass: a: int with self.assertRaises(NestedMissingEnvironmentVariableError) as expected: - self.envipy.envipy(f"{self.test_prefix}_ATTR_CLASS", AttrClass) + self.enviparse.parse(f"{self.test_prefix}_ATTR_CLASS", AttrClass) self.assertEqual(f"Environment variable '{self.test_prefix}_ATTR_CLASS_A' is not set.", str(expected.exception)) @dataclasses.dataclass @@ -143,56 +143,56 @@ class DataClass: a: int with self.assertRaises(NestedMissingEnvironmentVariableError) as expected: - self.envipy.envipy(f"{self.test_prefix}_DATA_CLASS", DataClass) + self.enviparse.parse(f"{self.test_prefix}_DATA_CLASS", DataClass) self.assertEqual(f"Environment variable '{self.test_prefix}_DATA_CLASS_A' is not set.", str(expected.exception)) def test_should_return_from_list_type(self): os.environ[f"{self.test_prefix}_0"] = "str 1" os.environ[f"{self.test_prefix}_1"] = "str 2" - self.assertEqual(["str 1", "str 2"], self.envipy.envipy(self.test_prefix, List[str])) + self.assertEqual(["str 1", "str 2"], self.enviparse.parse(self.test_prefix, List[str])) def test_should_return_empty_list(self): - self.assertEqual([], self.envipy.envipy(self.test_prefix, List[int])) - self.assertEqual([], self.envipy.envipy(self.test_prefix, List[bool])) - self.assertEqual([], self.envipy.envipy(self.test_prefix, List[Optional[int]])) + self.assertEqual([], self.enviparse.parse(self.test_prefix, List[int])) + self.assertEqual([], self.enviparse.parse(self.test_prefix, List[bool])) + self.assertEqual([], self.enviparse.parse(self.test_prefix, List[Optional[int]])) @attr.s(auto_attribs=True) class AttrClass: a: int - self.assertEqual([], self.envipy.envipy(self.test_prefix, List[AttrClass])) + self.assertEqual([], self.enviparse.parse(self.test_prefix, List[AttrClass])) @dataclasses.dataclass class DataClass: a: int - self.assertEqual([], self.envipy.envipy(self.test_prefix, List[DataClass])) + self.assertEqual([], self.enviparse.parse(self.test_prefix, List[DataClass])) def test_should_return_with_optional_unset(self): - self.assertEqual(None, self.envipy.envipy(self.test_prefix, Optional[str])) + self.assertEqual(None, self.enviparse.parse(self.test_prefix, Optional[str])) def test_should_return_with_optional_set(self): os.environ[f"{self.test_prefix}"] = "str" - self.assertEqual("str", self.envipy.envipy(self.test_prefix, Optional[str])) + self.assertEqual("str", self.enviparse.parse(self.test_prefix, Optional[str])) def test_should_return_optional_of_attr_class(self): @attr.s(auto_attribs=True) class AttrClass: a: int - self.assertEqual(None, self.envipy.envipy(self.test_prefix, Optional[AttrClass])) + self.assertEqual(None, self.enviparse.parse(self.test_prefix, Optional[AttrClass])) def test_should_return_attr_class_with_default_field(self): @attr.s(auto_attribs=True) class AttrClass: a: bool = False - self.assertEqual(AttrClass(), self.envipy.envipy(self.test_prefix, AttrClass)) + self.assertEqual(AttrClass(), self.enviparse.parse(self.test_prefix, AttrClass)) def test_should_return_option_list(self): - self.assertIsNone(self.envipy.envipy(self.test_prefix, Optional[List])) + self.assertIsNone(self.enviparse.parse(self.test_prefix, Optional[List])) - def test_envipy_with_dataclass_class_should_return_using_dataclass_default_value_if_none(self): + def test_parse_with_dataclass_class_should_return_using_dataclass_default_value_if_none(self): @dataclasses.dataclass class DataClass: a: int @@ -204,10 +204,10 @@ class DataClass: a=1, b=5, ), - self.envipy.envipy(self.test_prefix, DataClass), + self.enviparse.parse(self.test_prefix, DataClass), ) - def test_envipy_should_return_with_nested_dataclass_class(self): + def test_parse_should_return_with_nested_dataclass_class(self): @dataclasses.dataclass class ChildClass: field: str @@ -218,10 +218,10 @@ class ParentClass: os.environ[f"{self.test_prefix}_CHILD_FIELD"] = "value" self.assertEqual( - ParentClass(child=ChildClass(field="value")), self.envipy.envipy(self.test_prefix, ParentClass) + ParentClass(child=ChildClass(field="value")), self.enviparse.parse(self.test_prefix, ParentClass) ) - def test_envipy_with_dataclass_class_should_return_using_dataclass_with_nested_list(self): + def test_parse_with_dataclass_class_should_return_using_dataclass_with_nested_list(self): @dataclasses.dataclass class DataClass: b: List[str] = None @@ -235,17 +235,17 @@ class DataClass: "value 2", ] ), - self.envipy.envipy(self.test_prefix, DataClass), + self.enviparse.parse(self.test_prefix, DataClass), ) - def test_envipy_with_dataclass_class_should_return_using_dataclass_with_optional(self): + def test_parse_with_dataclass_class_should_return_using_dataclass_with_optional(self): @dataclasses.dataclass class DataClass: b: Optional[str] - self.assertEqual(DataClass(b=None), self.envipy.envipy(self.test_prefix, DataClass)) + self.assertEqual(DataClass(b=None), self.enviparse.parse(self.test_prefix, DataClass)) - def test_envipy_with_dataclass_class_should_return_using_dataclass_with_optional_and_not_optional( + def test_parse_with_dataclass_class_should_return_using_dataclass_with_optional_and_not_optional( self, ): @dataclasses.dataclass @@ -254,7 +254,7 @@ class DataClass: b: Optional[str] os.environ[f"{self.test_prefix}_A"] = "Hello" - self.assertEqual(DataClass(a="Hello", b=None), self.envipy.envipy(self.test_prefix, DataClass)) + self.assertEqual(DataClass(a="Hello", b=None), self.enviparse.parse(self.test_prefix, DataClass)) def test_should_raise_missing_env_error_if_env_var_is_not_set_in_dataclass(self): @dataclasses.dataclass @@ -262,7 +262,7 @@ class DataClass: a: int with self.assertRaises(NestedMissingEnvironmentVariableError) as expected: - self.envipy.envipy(f"{self.test_prefix}_ATTR_CLASS", DataClass) + self.enviparse.parse(f"{self.test_prefix}_ATTR_CLASS", DataClass) self.assertEqual(f"Environment variable '{self.test_prefix}_ATTR_CLASS_A' is not set.", str(expected.exception)) def test_should_raise_error_if_partial_env_var_found_for_data_class(self): @@ -274,7 +274,7 @@ class DataClass: os.environ[f"{self.test_prefix}_A"] = "5" with self.assertRaises(NestedMissingEnvironmentVariableError) as expected: - self.envipy.envipy(self.test_prefix, DataClass) + self.enviparse.parse(self.test_prefix, DataClass) self.assertEqual(f"Environment variable '{self.test_prefix}_B' is not set.", str(expected.exception)) def test_should_return_optional_of_dataclass_class(self): @@ -282,26 +282,26 @@ def test_should_return_optional_of_dataclass_class(self): class DataClass: a: int - self.assertEqual(None, self.envipy.envipy(self.test_prefix, Optional[DataClass])) + self.assertEqual(None, self.enviparse.parse(self.test_prefix, Optional[DataClass])) def test_should_return_dataclass_class_if_only_default_field(self): @dataclasses.dataclass class DataClass: a: bool = False - self.assertEqual(DataClass(), self.envipy.envipy(self.test_prefix, DataClass)) + self.assertEqual(DataClass(), self.enviparse.parse(self.test_prefix, DataClass)) - def test_envipy_on_unsupported_class_should_raise_error(self): + def test_parse_on_unsupported_class_should_raise_error(self): class NotADataClass: pass with self.assertRaises(UnexpectedTypeError) as expected: - self.envipy.envipy(self.test_prefix, NotADataClass) + self.enviparse.parse(self.test_prefix, NotADataClass) self.assertEqual( 'Unsupported type "NotADataClass" for property at path "TEST_ENV_VARS"', str(expected.exception) ) - def test_envipy_list_of_dataclass_should_raise_error_on_nested_missing_variable(self): + def test_parse_list_of_dataclass_should_raise_error_on_nested_missing_variable(self): @dataclasses.dataclass class DataClass: a: bool @@ -310,60 +310,60 @@ class DataClass: os.environ[f"{self.test_prefix}_0_A"] = "TRUE" with self.assertRaises(NestedMissingEnvironmentVariableError) as expected: - self.envipy.envipy(self.test_prefix, List[DataClass]) + self.enviparse.parse(self.test_prefix, List[DataClass]) self.assertEqual(f"Environment variable '{self.test_prefix}_0_B' is not set.", str(expected.exception)) - def test_envipy_should_return_error_if_encounter_list_without_generic_arg(self): + def test_parse_should_return_error_if_encounter_list_without_generic_arg(self): with self.assertRaises(UnknownTypeError) as expected: - self.envipy.envipy(self.test_prefix, List) + self.enviparse.parse(self.test_prefix, List) self.assertEqual(f'Unknown generic type for property at path "{self.test_prefix}"', str(expected.exception)) - def test_envipy_should_return_enum_with_str_values(self): + def test_parse_should_return_enum_with_str_values(self): class MyEnum(enum.Enum): VAL1 = "val1" VAL2 = "val2" os.environ[self.test_prefix] = "val1" - self.assertEqual(MyEnum.VAL1, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL1, self.enviparse.parse(self.test_prefix, MyEnum)) os.environ[self.test_prefix] = "val2" - self.assertEqual(MyEnum.VAL2, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL2, self.enviparse.parse(self.test_prefix, MyEnum)) with self.assertRaises(CastError) as expected: os.environ[self.test_prefix] = "unknown" - self.envipy.envipy(self.test_prefix, MyEnum) + self.enviparse.parse(self.test_prefix, MyEnum) self.assertEqual(f"Failed to convert '{self.test_prefix}' to MyEnum.", str(expected.exception)) - def test_envipy_should_return_enum_with_int_values(self): + def test_parse_should_return_enum_with_int_values(self): class MyEnum(enum.Enum): VAL1 = 1 VAL2 = 2 os.environ[self.test_prefix] = "1" - self.assertEqual(MyEnum.VAL1, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL1, self.enviparse.parse(self.test_prefix, MyEnum)) os.environ[self.test_prefix] = "2" - self.assertEqual(MyEnum.VAL2, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL2, self.enviparse.parse(self.test_prefix, MyEnum)) with self.assertRaises(CastError) as expected: os.environ[self.test_prefix] = "3" - self.envipy.envipy(self.test_prefix, MyEnum) + self.enviparse.parse(self.test_prefix, MyEnum) self.assertEqual(f"Failed to convert '{self.test_prefix}' to MyEnum.", str(expected.exception)) - def test_envipy_should_return_enum_with_auto_values(self): + def test_parse_should_return_enum_with_auto_values(self): class MyEnum(enum.Enum): VAL1 = enum.auto() VAL2 = enum.auto() os.environ[self.test_prefix] = "1" - self.assertEqual(MyEnum.VAL1, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL1, self.enviparse.parse(self.test_prefix, MyEnum)) os.environ[self.test_prefix] = "2" - self.assertEqual(MyEnum.VAL2, self.envipy.envipy(self.test_prefix, MyEnum)) + self.assertEqual(MyEnum.VAL2, self.enviparse.parse(self.test_prefix, MyEnum)) with self.assertRaises(CastError) as expected: os.environ[self.test_prefix] = "3" - self.envipy.envipy(self.test_prefix, MyEnum) + self.enviparse.parse(self.test_prefix, MyEnum) self.assertEqual(f"Failed to convert '{self.test_prefix}' to MyEnum.", str(expected.exception)) - def test_envipy_should_raise_error_if_enum_type_values_are_not_int_or_string(self): + def test_parse_should_raise_error_if_enum_type_values_are_not_int_or_string(self): class CustomObject: def __init__(self, value): self.value = value @@ -377,19 +377,19 @@ class MyEnum(enum.Enum): with self.assertRaises(UnexpectedTypeError) as expected: os.environ[self.test_prefix] = "1" - self.envipy.envipy(self.test_prefix, MyEnum) + self.enviparse.parse(self.test_prefix, MyEnum) self.assertEqual('Unsupported type "MyEnum" for property at path "TEST_ENV_VARS"', str(expected.exception)) - def test_envipy_should_return_exception_if_no_env_value_when_parsing_enum(self): + def test_parse_should_return_exception_if_no_env_value_when_parsing_enum(self): class MyEnum(enum.Enum): VAL1 = 1 VAL2 = 2 with self.assertRaises(MissingEnvironmentVariableError) as expected: - self.envipy.envipy(self.test_prefix, MyEnum) + self.enviparse.parse(self.test_prefix, MyEnum) self.assertEqual("Environment variable 'TEST_ENV_VARS' is not set.", str(expected.exception)) - def test_envipy_with_nested_enum(self): + def test_parse_with_nested_enum(self): class MyEnum(enum.Enum): VAL1 = 1 VAL2 = 2 @@ -405,5 +405,5 @@ class DataClass: a=MyEnum.VAL1, b=MyEnum.VAL2, ), - self.envipy.envipy(self.test_prefix, DataClass), + self.enviparse.parse(self.test_prefix, DataClass), ) diff --git a/tests/test_opyoid.py b/tests/test_opyoid.py index dd44965..83728d9 100644 --- a/tests/test_opyoid.py +++ b/tests/test_opyoid.py @@ -1,10 +1,10 @@ import os import unittest -from envipy.opyoid import envipy_provider +from enviparse.opyoid import enviparse_provider -class TestEnvipyOpyoidProviderFactory(unittest.TestCase): +class TestEnviparseOpyoidProviderFactory(unittest.TestCase): test_prefix = "TEST_ENV_VARS" def setUp(self) -> None: @@ -20,5 +20,5 @@ def clear_test_env_vars(self) -> None: def test_should_provide(self): os.environ[self.test_prefix] = "value" - provider_type = envipy_provider(self.test_prefix, str) + provider_type = enviparse_provider(self.test_prefix, str) self.assertEqual("value", provider_type().get())