@@ -47,17 +47,21 @@ def test_cached(caplog, test_context, tmp_path) -> None:
4747 """:func:`.cached` works as expected."""
4848 # Store in the temporary directory for this session, to avoid collisions across
4949 # sessions
50- test_context .cache_path = tmp_path .joinpath ("cache" )
50+ test_context .core .cache_path = tmp_path .joinpath ("cache" )
51+ test_context .core .cache_path .mkdir (parents = True , exist_ok = True )
5152
5253 # A dummy path to be hashed as an argument
5354 path_foo = tmp_path .joinpath ("foo" , "bar" )
5455
56+ # Number of times each function has executed
57+ count = dict (func0 = 0 , func1 = 0 )
58+
5559 with caplog .at_level (logging .DEBUG , logger = "message_ix_models" ):
5660
5761 @cached
5862 def func0 (ctx , a , path , b = 3 ):
5963 """A test function."""
60- log . info ( "func0 runs" )
64+ count [ "func0" ] += 1
6165 return f"{ id (ctx )} , { a + b } "
6266
6367 # Docstring is modified
@@ -66,20 +70,20 @@ def func0(ctx, a, path, b=3):
6670 @cached
6771 def func1 (x = 1 , y = 2 , ** kwargs ):
6872 # Function with defaults for all arguments
69- log . info ( "func1 runs" )
73+ count [ "func1" ] += 1
7074 return x + y
7175
7276 caplog .clear ()
7377
74- # pathlib.Path argument is serialized to JSON as part of the argument hash;
75- # function runs, messages logged
76- with assert_logs ( caplog , "func0 runs" ):
77- result0 = func0 ( test_context , 1 , path_foo )
78+ # pathlib.Path argument is serialized to JSON as part of the argument hash; function
79+ # runs, messages logged
80+ result0 = func0 ( test_context , 1 , path_foo )
81+ assert 1 == count [ " func0" ]
7882
7983 caplog .clear ()
8084 result1 = func0 (test_context , 1 , path_foo )
8185 # Function does not run
82- assert "func0 runs" not in caplog . messages
86+ assert 1 == count [ "func0" ]
8387 assert caplog .messages [0 ].startswith ("Cache hit for func0" )
8488 # Results identical
8589 assert result0 == result1
@@ -90,16 +94,17 @@ def func1(x=1, y=2, **kwargs):
9094
9195 result2 = func0 (ctx2 , 1 , path_foo )
9296 # Function does not run
93- assert "func0 runs" not in caplog . messages
97+ assert 1 == count [ "func0" ]
9498 # Results are identical, i.e. including the old ID
9599 assert result0 == result2
96100
97101 ctx2 .delete ()
98102 caplog .clear ()
99103
100104 # Hash of no arguments is the same, function only runs once
105+ assert 0 == count ["func1" ]
101106 assert 3 == func1 () == func1 ()
102- assert 1 == sum ( m == "func1 runs" for m in caplog . messages )
107+ assert 1 == count [ "func1" ]
103108
104109 # Warnings logged for unhashables; ScenarioInfo is hashed as dict
105110 caplog .clear ()
@@ -113,7 +118,8 @@ def func1(x=1, y=2, **kwargs):
113118 func1 (ds = xr .Dataset (), mp = test_context .get_platform (), si = ScenarioInfo ())
114119
115120 # Unserializable type raises an exception
116- with pytest .raises (
117- TypeError , match = "Object of type slice is not JSON serializable"
118- ):
119- func1 (arg = slice (None ))
121+ class Foo :
122+ pass
123+
124+ with pytest .raises (TypeError , match = "Object of type Foo is not JSON serializable" ):
125+ func1 (arg = Foo ())
0 commit comments