|
| 1 | +import json |
| 2 | +from datetime import datetime, timedelta |
1 | 3 | import unittest |
2 | 4 | from unittest.mock import MagicMock, patch |
3 | 5 |
|
@@ -38,6 +40,77 @@ def test_disable_and_reenable_on_solr_failure(self): |
38 | 40 | # One call for the health check, one for the cache query itself |
39 | 41 | self.assertGreaterEqual(get.call_count, 1) |
40 | 42 |
|
| 43 | + def test_cache_invalidated_on_major_version_change(self): |
| 44 | + cache = SolrResultCache() |
| 45 | + cache._solr_available = MagicMock(return_value=True) |
| 46 | + cache._package_version = "1.8.1" |
| 47 | + |
| 48 | + cached_data = { |
| 49 | + "result": {"foo": "bar"}, |
| 50 | + "cached_at": datetime.now().isoformat(), |
| 51 | + "expires_at": (datetime.now() + timedelta(hours=1)).isoformat(), |
| 52 | + "params": {"limit": -1}, |
| 53 | + "hit_count": 0, |
| 54 | + "cache_version": "1.0", |
| 55 | + "package_version": "1.8.0", |
| 56 | + "ttl_hours": 2160, |
| 57 | + } |
| 58 | + |
| 59 | + response_doc = {"response": {"docs": [{"cache_data": json.dumps(cached_data)}]}} |
| 60 | + |
| 61 | + with patch("vfbquery.solr_result_cache.requests.get") as get: |
| 62 | + get.return_value = MagicMock(status_code=200, json=lambda: response_doc) |
| 63 | + result = cache.get_cached_result("term_info", "FBbt_00000000") |
| 64 | + self.assertIsNone(result) |
| 65 | + self.assertEqual(get.call_count, 1) |
| 66 | + |
| 67 | + def test_cache_retained_on_patch_version_change(self): |
| 68 | + cache = SolrResultCache() |
| 69 | + cache._solr_available = MagicMock(return_value=True) |
| 70 | + cache._package_version = "1.8.2" |
| 71 | + |
| 72 | + cached_data = { |
| 73 | + "result": {"foo": "bar"}, |
| 74 | + "cached_at": datetime.now().isoformat(), |
| 75 | + "expires_at": (datetime.now() + timedelta(hours=1)).isoformat(), |
| 76 | + "params": {"limit": -1}, |
| 77 | + "hit_count": 0, |
| 78 | + "cache_version": "1.0", |
| 79 | + "package_version": "1.8.1", |
| 80 | + "ttl_hours": 2160, |
| 81 | + } |
| 82 | + |
| 83 | + response_doc = {"response": {"docs": [{"cache_data": json.dumps(cached_data)}]}} |
| 84 | + |
| 85 | + with patch("vfbquery.solr_result_cache.requests.get") as get: |
| 86 | + get.return_value = MagicMock(status_code=200, json=lambda: response_doc) |
| 87 | + result = cache.get_cached_result("term_info", "FBbt_00000000") |
| 88 | + self.assertEqual(result, {"foo": "bar"}) |
| 89 | + self.assertEqual(get.call_count, 1) |
| 90 | + |
| 91 | + def test_cache_invalidated_when_cached_version_missing(self): |
| 92 | + cache = SolrResultCache() |
| 93 | + cache._solr_available = MagicMock(return_value=True) |
| 94 | + cache._package_version = "1.8.2" |
| 95 | + |
| 96 | + cached_data = { |
| 97 | + "result": {"foo": "bar"}, |
| 98 | + "cached_at": datetime.now().isoformat(), |
| 99 | + "expires_at": (datetime.now() + timedelta(hours=1)).isoformat(), |
| 100 | + "params": {"limit": -1}, |
| 101 | + "hit_count": 0, |
| 102 | + "cache_version": "1.0", |
| 103 | + "ttl_hours": 2160, |
| 104 | + } |
| 105 | + |
| 106 | + response_doc = {"response": {"docs": [{"cache_data": json.dumps(cached_data)}]}} |
| 107 | + |
| 108 | + with patch("vfbquery.solr_result_cache.requests.get") as get: |
| 109 | + get.return_value = MagicMock(status_code=200, json=lambda: response_doc) |
| 110 | + result = cache.get_cached_result("term_info", "FBbt_00000000") |
| 111 | + self.assertIsNone(result) |
| 112 | + self.assertEqual(get.call_count, 1) |
| 113 | + |
41 | 114 |
|
42 | 115 | if __name__ == "__main__": |
43 | 116 | unittest.main(verbosity=2) |
0 commit comments