Coverage for tests / unit / utils / unified_config / test_nested_value.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Tests for _get_nested_value function.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7import pytest 

8from assertpy import assert_that 

9 

10from lintro.utils.unified_config import _get_nested_value 

11 

12 

13@pytest.mark.parametrize( 

14 ("config", "key_path", "expected"), 

15 [ 

16 ({"line-length": 100}, "line-length", 100), 

17 ({"rules": {"line-length": {"max": 100}}}, "rules.line-length.max", 100), 

18 ({"a": {"b": {"c": "deep"}}}, "a.b.c", "deep"), 

19 ({"key": "value"}, "key", "value"), 

20 ], 

21 ids=["simple_key", "nested_three_levels", "deeply_nested", "string_value"], 

22) 

23def test_get_nested_value_returns_expected_value( 

24 config: dict[str, Any], 

25 key_path: str, 

26 expected: Any, 

27) -> None: 

28 """Verify _get_nested_value retrieves values at various nesting depths. 

29 

30 Args: 

31 config: Configuration object. 

32 key_path: Path to configuration key. 

33 expected: Expected value. 

34 """ 

35 result = _get_nested_value(config, key_path) 

36 assert_that(result).is_equal_to(expected) 

37 

38 

39@pytest.mark.parametrize( 

40 ("config", "key_path"), 

41 [ 

42 ({"other": "value"}, "line-length"), 

43 ({"rules": {"other": "value"}}, "rules.line-length.max"), 

44 ({}, "any.key"), 

45 ({"rules": "not a dict"}, "rules.line-length.max"), 

46 ], 

47 ids=[ 

48 "missing_top_level", 

49 "missing_nested", 

50 "empty_config", 

51 "non_dict_intermediate", 

52 ], 

53) 

54def test_get_nested_value_returns_none_for_missing_keys( 

55 config: dict[str, Any], 

56 key_path: str, 

57) -> None: 

58 """Verify _get_nested_value returns None when path doesn't exist. 

59 

60 Args: 

61 config: Configuration object. 

62 key_path: Path to configuration key. 

63 """ 

64 result = _get_nested_value(config, key_path) 

65 assert_that(result).is_none()