Files
cpl/test/core/utils/get_value_test.py
clu ca58f636ee
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 13s
Test before pr merge / test (pull_request) Successful in 40s
test(core): add unit tests for untested core modules
Adds 113 tests covering:
- abc: RegistryABC (concrete implementation + edge cases)
- environment: Environment get/set, EnvironmentEnum
- pipes: BoolPipe, IPAddressPipe (incl. roundtrip + error cases)
- time: Cron (next(), intervals, invalid expression)
- utils: Cache (TTL, expiry, cleanup), get_value (incl. bug
  documentation: cast result not returned for string->typed values),
  JSONProcessor (nested objects, enums, defaults)
- property: classproperty (class access, instance access, subclass)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 18:40:01 +02:00

65 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from typing import List
from cpl.core.utils.get_value import get_value
def test_get_existing_str():
assert get_value({"key": "hello"}, "key", str) == "hello"
def test_get_existing_int_already_typed():
# Value already has the correct type -> returned directly
assert get_value({"count": 42}, "count", int) == 42
def test_get_existing_float_already_typed():
# Value already has the correct type -> returned directly
assert get_value({"pi": 3.14}, "pi", float) == 3.14
# NOTE: get_value calls cast() internally but does NOT return the cast result
# (the return value is lost). String "42" -> int returns None instead of 42.
# This is a known bug in get_value.
def test_get_str_to_int_returns_none_bug():
result = get_value({"count": "42"}, "count", int)
assert result is None # Bug: should be 42
def test_get_missing_key_returns_none():
assert get_value({}, "missing", str) is None
def test_get_missing_key_returns_default():
assert get_value({}, "missing", str, "fallback") == "fallback"
def test_get_value_already_correct_type():
assert get_value({"x": 5}, "x", int) == 5
def test_get_list_already_correct_type():
result = get_value({"items": [1, 2, 3]}, "items", List[int])
assert result == [1, 2, 3]
def test_get_list_of_str_already_correct_type():
result = get_value({"tags": ["a", "b"]}, "tags", List[str])
assert result == ["a", "b"]
def test_get_bool_already_typed():
# Value is already bool -> returned directly
assert get_value({"flag": True}, "flag", bool) is True
assert get_value({"flag": False}, "flag", bool) is False
# NOTE: Same bug string "true" -> bool returns None
def test_get_bool_from_str_returns_none_bug():
result = get_value({"flag": "true"}, "flag", bool)
assert result is None # Bug: should be True
def test_cast_failure_returns_default():
result = get_value({"val": "not_a_number"}, "val", int, -1)
assert result == -1