Coverage for Users / vladimirpavlov / PycharmProjects / parameterizable / tests / test_cacheable_properties_mixin_get_status.py: 95%
60 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-01 16:37 -0600
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-01 16:37 -0600
1from functools import cached_property
2from mixinforge import CacheablePropertiesMixin
3import pytest
5class A(CacheablePropertiesMixin):
6 @cached_property
7 def x(self):
8 return 1
10 @cached_property
11 def y(self):
12 return 2
14 @property
15 def z(self):
16 return 3
18 def regular_method(self):
19 pass
21def 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
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
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
38def test_get_cached_property_status_invalid_name():
39 """Test that _get_cached_property_status() raises ValueError for invalid names."""
40 a = A()
42 # Non-existent property
43 with pytest.raises(ValueError, match="not a cached property"):
44 a._get_cached_property_status("invalid_name")
46 # Regular @property (not cached_property)
47 with pytest.raises(ValueError, match="not a cached property"):
48 a._get_cached_property_status("z")
50def 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
58 a._invalidate_cache()
59 assert a._get_cached_property_status("x") is False
60 assert a._get_cached_property_status("y") is False
62def 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"
69 class Child(Base):
70 @cached_property
71 def child_prop(self):
72 return "child"
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
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
82def 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)
87 assert a._get_cached_property_status("x") is True
88 assert a._get_cached_property_status("y") is False