Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_cacheable_properties_mixin_get_status.py: 95%

1 from functools import cached_property

2 from mixinforge import CacheablePropertiesMixin

3 import pytest

4

5 class A(CacheablePropertiesMixin):

6 @cached_property

7 def x(self):

8 return 1

9

10 @cached_property

11 def y(self):

12 return 2

13

14 @property

15 def z(self):

16 return 3

17

18 def regular_method(self):

19 pass

20

21 def test_get_cached_property_status_basic():

22 """Test that _get_cached_property_status() returns correct caching status."""

23 a = A()

24 # Initially not cached

25 assert a._get_cached_property_status("x") is False

26 assert a._get_cached_property_status("y") is False

27

28 # Cache x

29 _ = a.x

30 assert a._get_cached_property_status("x") is True

31 assert a._get_cached_property_status("y") is False

32

33 # Cache y

34 _ = a.y

35 assert a._get_cached_property_status("x") is True

36 assert a._get_cached_property_status("y") is True

37

38 def test_get_cached_property_status_invalid_name():

39 """Test that _get_cached_property_status() raises ValueError for invalid names."""

40 a = A()

41

42 # Non-existent property

43 with pytest.raises(ValueError, match="not a cached property"):

44 a._get_cached_property_status("invalid_name")

45

46 # Regular @property (not cached_property)

47 with pytest.raises(ValueError, match="not a cached property"):

48 a._get_cached_property_status("z")

49

50 def test_get_cached_property_status_after_invalidation():

51 """Test that _get_cached_property_status() returns False after invalidation."""

52 a = A()

53 _ = a.x

54 _ = a.y

55 assert a._get_cached_property_status("x") is True

56 assert a._get_cached_property_status("y") is True

57

58 a._invalidate_cache()

59 assert a._get_cached_property_status("x") is False

60 assert a._get_cached_property_status("y") is False

61

62 def test_get_cached_property_status_inheritance():

63 """Test that _get_cached_property_status() works with inherited 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 assert c._get_cached_property_status("base_prop") is False

76 assert c._get_cached_property_status("child_prop") is False

77

78 _ = c.base_prop

79 assert c._get_cached_property_status("base_prop") is True

80 assert c._get_cached_property_status("child_prop") is False

81

82 def test_get_cached_property_status_partial():

83 """Test that _get_cached_property_status() reflects partial caching correctly."""

84 a = A()

85 a._set_cached_properties(x=50)

86

87 assert a._get_cached_property_status("x") is True

88 assert a._get_cached_property_status("y") is False