Skip to content

Commit 868eef8

Browse files
committed
add tests for environment mapping
1 parent e3aa2bd commit 868eef8

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tests/units/test_environment.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,87 @@ def test_interpret_optional_type(self):
196196
result = interpret_env_var_value("42", int | None, "TEST_FIELD")
197197
assert result == 42
198198

199+
def test_interpret_mapping_string_int(self):
200+
"""Test mapping interpretation with str:int types."""
201+
from collections.abc import Mapping
202+
203+
result = interpret_env_var_value(
204+
"key1:1:key2:2", Mapping[str, int], "TEST_FIELD"
205+
)
206+
assert result == {"key1": 1, "key2": 2}
207+
208+
def test_interpret_mapping_int_string(self):
209+
"""Test mapping interpretation with int:str types."""
210+
from collections.abc import Mapping
211+
212+
result = interpret_env_var_value(
213+
"1:value1:2:value2", Mapping[int, str], "TEST_FIELD"
214+
)
215+
assert result == {1: "value1", 2: "value2"}
216+
217+
def test_interpret_mapping_empty(self):
218+
"""Test mapping interpretation with empty value."""
219+
from collections.abc import Mapping
220+
221+
result = interpret_env_var_value("", Mapping[str, str], "TEST_FIELD")
222+
assert result == {}
223+
224+
def test_interpret_mapping_single_pair(self):
225+
"""Test mapping interpretation with single key-value pair."""
226+
from collections.abc import Mapping
227+
228+
result = interpret_env_var_value("key:value", Mapping[str, str], "TEST_FIELD")
229+
assert result == {"key": "value"}
230+
231+
def test_interpret_mapping_bool_values(self):
232+
"""Test mapping interpretation with boolean values."""
233+
from collections.abc import Mapping
234+
235+
result = interpret_env_var_value(
236+
"key1:true:key2:false", Mapping[str, bool], "TEST_FIELD"
237+
)
238+
assert result == {"key1": True, "key2": False}
239+
240+
def test_interpret_mapping_odd_length_error(self):
241+
"""Test mapping interpretation with odd number of items raises error."""
242+
from collections.abc import Mapping
243+
244+
with pytest.raises(
245+
EnvironmentVarValueError,
246+
match="Invalid mapping value.*Must be in the format key1:value1:key2:value2",
247+
):
248+
interpret_env_var_value("key1:value1:key2", Mapping[str, str], "TEST_FIELD")
249+
250+
def test_interpret_mapping_invalid_type_args(self):
251+
"""Test mapping interpretation with invalid number of type arguments."""
252+
from collections.abc import Mapping
253+
254+
with pytest.raises(
255+
ValueError,
256+
match="Invalid mapping type.*Must have exactly two type arguments",
257+
):
258+
interpret_env_var_value("key:value", Mapping[str], "TEST_FIELD") # pyright: ignore[reportInvalidTypeArguments]
259+
260+
def test_interpret_mapping_nested_types(self):
261+
"""Test mapping interpretation with path types."""
262+
from collections.abc import Mapping
263+
from pathlib import Path
264+
265+
result = interpret_env_var_value(
266+
"key1:/path/one:key2:/path/two", Mapping[str, Path], "TEST_FIELD"
267+
)
268+
assert result == {"key1": Path("/path/one"), "key2": Path("/path/two")}
269+
assert all(isinstance(v, Path) for v in result.values())
270+
271+
def test_interpret_mapping_enum_keys(self):
272+
"""Test mapping interpretation with enum keys."""
273+
from collections.abc import Mapping
274+
275+
result = interpret_env_var_value(
276+
"value1:test1:value2:test2", Mapping[_TestEnum, str], "TEST_FIELD"
277+
)
278+
assert result == {_TestEnum.VALUE1: "test1", _TestEnum.VALUE2: "test2"}
279+
199280

200281
class TestEnvVar:
201282
"""Test the EnvVar class."""

0 commit comments

Comments
 (0)