Skip to content

Commit d23a861

Browse files
committed
test: enhance caching tests by mocking streamlit.session_state
1 parent d29fa3d commit d23a861

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

tests/unit/test_analyze_financial_asset_cache.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,21 @@ def test_cache_miss_calls_functions(
135135
"total_return": 10.0,
136136
}
137137

138-
# First call should miss cache and call functions
139-
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
138+
# Mock streamlit.session_state to support dynamic attributes
139+
mock_session_state = MagicMock()
140+
with patch("streamlit.session_state", mock_session_state):
141+
# First call should miss cache and call functions
142+
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
140143

141-
# Verify functions were called
142-
assert mock_search_symbol.called
143-
assert mock_get_prices.called
144-
assert mock_calc_returns.called
144+
# Verify functions were called
145+
assert mock_search_symbol.called
146+
assert mock_get_prices.called
147+
assert mock_calc_returns.called
145148

146-
# Verify result is valid JSON
147-
data = json.loads(result1)
148-
assert data["success"] is True
149-
assert data["ticker"] == "SWDA"
149+
# Verify result is valid JSON
150+
data = json.loads(result1)
151+
assert data["success"] is True
152+
assert data["ticker"] == "SWDA"
150153

151154
@patch("src.tools.analyze_financial_asset._search_and_resolve_symbol")
152155
@patch("src.tools.analyze_financial_asset._get_historical_prices_internal")
@@ -176,24 +179,27 @@ def test_cache_hit_skips_functions(
176179
"total_return": 10.0,
177180
}
178181

179-
# First call - cache miss
180-
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
182+
# Mock streamlit.session_state to support dynamic attributes
183+
mock_session_state = MagicMock()
184+
with patch("streamlit.session_state", mock_session_state):
185+
# First call - cache miss
186+
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
181187

182-
# Reset mock call counts
183-
mock_search_symbol.reset_mock()
184-
mock_get_prices.reset_mock()
185-
mock_calc_returns.reset_mock()
188+
# Reset mock call counts
189+
mock_search_symbol.reset_mock()
190+
mock_get_prices.reset_mock()
191+
mock_calc_returns.reset_mock()
186192

187-
# Second call - should hit cache
188-
result2 = analyze_financial_asset("SWDA", years=10, use_cache=True)
193+
# Second call - should hit cache
194+
result2 = analyze_financial_asset("SWDA", years=10, use_cache=True)
189195

190-
# Verify functions were NOT called on second attempt
191-
assert not mock_search_symbol.called
192-
assert not mock_get_prices.called
193-
assert not mock_calc_returns.called
196+
# Verify functions were NOT called on second attempt
197+
assert not mock_search_symbol.called
198+
assert not mock_get_prices.called
199+
assert not mock_calc_returns.called
194200

195-
# Results should be identical
196-
assert result1 == result2
201+
# Results should be identical
202+
assert result1 == result2
197203

198204
@patch("src.tools.analyze_financial_asset._search_and_resolve_symbol")
199205
@patch("src.tools.analyze_financial_asset._get_historical_prices_internal")
@@ -223,21 +229,24 @@ def test_use_cache_false_bypasses_cache(
223229
"total_return": 10.0,
224230
}
225231

226-
# First call with caching
227-
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
228-
229-
# Reset mocks
230-
mock_search_symbol.reset_mock()
231-
mock_get_prices.reset_mock()
232-
mock_calc_returns.reset_mock()
233-
234-
# Second call with use_cache=False should call functions again
235-
result2 = analyze_financial_asset("SWDA", years=10, use_cache=False)
236-
237-
# Verify functions WERE called even though data is cached
238-
assert mock_search_symbol.called
239-
assert mock_get_prices.called
240-
assert mock_calc_returns.called
232+
# Mock streamlit.session_state to support dynamic attributes
233+
mock_session_state = MagicMock()
234+
with patch("streamlit.session_state", mock_session_state):
235+
# First call with caching
236+
result1 = analyze_financial_asset("SWDA", years=10, use_cache=True)
237+
238+
# Reset mocks
239+
mock_search_symbol.reset_mock()
240+
mock_get_prices.reset_mock()
241+
mock_calc_returns.reset_mock()
242+
243+
# Second call with use_cache=False should call functions again
244+
result2 = analyze_financial_asset("SWDA", years=10, use_cache=False)
245+
246+
# Verify functions WERE called even though data is cached
247+
assert mock_search_symbol.called
248+
assert mock_get_prices.called
249+
assert mock_calc_returns.called
241250

242251
def test_cache_respects_case_insensitive_ticker(self):
243252
"""Test that cache works with case-insensitive ticker symbols."""

0 commit comments

Comments
 (0)