Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_cacheable_properties_mixin_get_all.py: 96%

1 from functools import cached_property

2 from mixinforge import CacheablePropertiesMixin

3

4 class A(CacheablePropertiesMixin):

5 @cached_property

6 def x(self):

7 return 1

8

9 @cached_property

10 def y(self):

11 return 2

12

13 @property

14 def z(self):

15 return 3

16

17 def regular_method(self):

18 pass

19

20 def test_get_cached_values_basic():

21 """Test that _get_cached_values() returns dict with cached property values."""

22 a = A()

23 # Cache properties

24 _ = a.x

25 _ = a.y

26

27 cached_values = a._get_all_cached_properties()

28

29 assert isinstance(cached_values, dict)

30 assert cached_values == {"x": 1, "y": 2}

31

32 def test_get_cached_values_partial():

33 """Test that _get_cached_values() only includes actually cached properties."""

34 a = A()

35 # Cache only x, not y

36 _ = a.x

37

38 cached_values = a._get_all_cached_properties()

39

40 assert cached_values == {"x": 1}

41 assert "y" not in cached_values

42

43 def test_get_cached_values_empty():

44 """Test that _get_cached_values() returns empty dict when nothing is cached."""

45 a = A()

46

47 cached_values = a._get_all_cached_properties()

48

49 assert cached_values == {}

50

51 def test_get_cached_values_after_invalidation():

52 """Test that _get_cached_values() returns empty dict after invalidation."""

53 a = A()

54 _ = a.x

55 _ = a.y

56

57 a._invalidate_cache()

58 cached_values = a._get_all_cached_properties()

59

60 assert cached_values == {}

61

62 def test_get_cached_values_inheritance():

63 """Test that _get_cached_values() works with inherited cached properties."""

64 class Base(CacheablePropertiesMixin):

65 @cached_property

66 def base_prop(self):

67 return "base"

68

69 class Child(Base):

70 @cached_property

71 def child_prop(self):

72 return "child"

73

74 c = Child()

75 _ = c.base_prop

76 _ = c.child_prop

77

78 cached_values = c._get_all_cached_properties()

79

80 assert cached_values == {"base_prop": "base", "child_prop": "child"}