@@ -31,3 +31,48 @@ def bar(self):
3131 assert foo .call_count == 1
3232 assert foo .bar == 42
3333 assert foo .call_count == 1
34+
35+
36+ def test_generation_cache_context ():
37+ assert cache .generation_cache .get () is None
38+ with cache .generation_cache_context ():
39+ assert isinstance (cache .generation_cache .get (), dict )
40+ cache .generation_cache .get ()["foo" ] = "bar"
41+ assert cache .generation_cache .get ()["foo" ] == "bar"
42+ assert cache .generation_cache .get () is None
43+
44+
45+ def test_cached_proto_context ():
46+ class Foo :
47+ def __init__ (self ):
48+ self .call_count = 0
49+
50+ @cache .cached_proto_context
51+ def bar (self , * , collisions , other = None ):
52+ self .call_count += 1
53+ return f"baz-{ self .call_count } "
54+
55+ foo = Foo ()
56+
57+ # Without context, no caching
58+ assert foo .bar (collisions = {"a" , "b" }) == "baz-1"
59+ assert foo .bar (collisions = {"a" , "b" }) == "baz-2"
60+
61+ # With context, caching works
62+ with cache .generation_cache_context ():
63+ assert foo .bar (collisions = {"a" , "b" }) == "baz-3"
64+ assert foo .bar (collisions = {"a" , "b" }) == "baz-3"
65+ assert foo .call_count == 3
66+
67+ # Different collisions, different cache entry
68+ assert foo .bar (collisions = {"c" }) == "baz-4"
69+ assert foo .bar (collisions = {"c" }) == "baz-4"
70+ assert foo .call_count == 4
71+
72+ # Same collisions again, still cached
73+ assert foo .bar (collisions = {"a" , "b" }) == "baz-3"
74+ assert foo .call_count == 4
75+
76+ # Context cleared
77+ assert cache .generation_cache .get () is None
78+ assert foo .bar (collisions = {"a" , "b" }) == "baz-5"
0 commit comments