Skip to content

Commit 5d0d8a6

Browse files
committed
Refactor SOLR caching implementation to use separate cache documents, improving data integrity and retrieval efficiency. Add production tests for cache functionality and field preservation.
1 parent 0fdbdca commit 5d0d8a6

3 files changed

Lines changed: 369 additions & 212 deletions

File tree

production_cache_test.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Production test for VFBquery SOLR caching system
5+
6+
Verifies that:
7+
1. Cache data is properly stored and retrieved
8+
2. Original VFB fields are preserved
9+
3. Cache expiration works correctly
10+
"""
11+
12+
import sys
13+
import os
14+
sys.path.insert(0, 'src')
15+
16+
from vfbquery.solr_result_cache import SolrResultCache
17+
import json
18+
import requests
19+
20+
def test_production_cache():
21+
"""Test production cache functionality with field preservation"""
22+
23+
cache = SolrResultCache()
24+
test_term_id = "FBbt_00003686"
25+
26+
print("🧪 Testing VFBquery SOLR Cache System")
27+
print("=" * 50)
28+
29+
# Step 1: Check original VFB data exists
30+
print(f"1. Verifying original VFB data exists for {test_term_id}...")
31+
32+
response = requests.get(f"{cache.cache_url}/select", params={
33+
"q": f"id:{test_term_id}",
34+
"fl": "id,anat_query,anat_2_ep_query,ep_2_anat_query,term_info",
35+
"wt": "json"
36+
}, timeout=5)
37+
38+
if response.status_code == 200:
39+
data = response.json()
40+
docs = data.get("response", {}).get("docs", [])
41+
42+
if docs:
43+
original_doc = docs[0]
44+
required_fields = ['id', 'anat_query', 'anat_2_ep_query', 'ep_2_anat_query', 'term_info']
45+
missing_fields = [field for field in required_fields if field not in original_doc]
46+
47+
if missing_fields:
48+
print(f" ❌ Missing original VFB fields: {missing_fields}")
49+
return False
50+
else:
51+
print(f" ✅ All original VFB fields present: {required_fields}")
52+
else:
53+
print(f" ❌ Document {test_term_id} not found")
54+
return False
55+
else:
56+
print(f" ❌ Failed to query document: HTTP {response.status_code}")
57+
return False
58+
59+
# Step 2: Test caching
60+
print("\n2. Testing cache storage...")
61+
62+
test_result = {
63+
"label": "Kenyon cell",
64+
"short_form": "FBbt_00003686",
65+
"iri": "http://purl.obolibrary.org/obo/FBbt_00003686",
66+
"cached": True,
67+
"test_timestamp": "2025-09-09T20:00:00+01:00"
68+
}
69+
70+
success = cache.cache_result("term_info", test_term_id, test_result)
71+
72+
if success:
73+
print(" ✅ Cache storage successful")
74+
else:
75+
print(" ❌ Cache storage failed")
76+
return False
77+
78+
# Step 3: Verify both original fields AND cache field are present
79+
print("\n3. Verifying field preservation after caching...")
80+
81+
response = requests.get(f"{cache.cache_url}/select", params={
82+
"q": f"id:{test_term_id}",
83+
"wt": "json"
84+
}, timeout=5)
85+
86+
if response.status_code == 200:
87+
data = response.json()
88+
docs = data.get("response", {}).get("docs", [])
89+
90+
if docs:
91+
updated_doc = docs[0]
92+
93+
# Check original VFB fields still exist
94+
original_fields_intact = all(field in updated_doc for field in required_fields)
95+
96+
# Check cache field exists
97+
cache_field_name = "vfb_query_term_info_ss"
98+
cache_field_exists = cache_field_name in updated_doc
99+
100+
print(f" Original VFB fields intact: {'✅' if original_fields_intact else '❌'}")
101+
print(f" Cache field added: {'✅' if cache_field_exists else '❌'}")
102+
103+
if original_fields_intact and cache_field_exists:
104+
print(f" 📊 Total fields in document: {len(updated_doc)}")
105+
106+
# Verify cache field content
107+
if cache_field_exists:
108+
cache_data_raw = updated_doc[cache_field_name][0] if isinstance(updated_doc[cache_field_name], list) else updated_doc[cache_field_name]
109+
cache_data = json.loads(cache_data_raw)
110+
111+
print(f" 📋 Cache metadata keys: {list(cache_data.keys())}")
112+
print(f" ⏰ Cached at: {cache_data.get('cached_at', 'Unknown')}")
113+
print(f" 📏 Cache size: {cache_data.get('result_size', 0)/1024:.1f}KB")
114+
else:
115+
print(" ❌ Field preservation failed!")
116+
return False
117+
else:
118+
print(" ❌ Document not found after caching")
119+
return False
120+
else:
121+
print(f" ❌ Failed to verify document: HTTP {response.status_code}")
122+
return False
123+
124+
# Step 4: Test cache retrieval
125+
print("\n4. Testing cache retrieval...")
126+
127+
retrieved_result = cache.get_cached_result("term_info", test_term_id)
128+
129+
if retrieved_result:
130+
if isinstance(retrieved_result, dict) and retrieved_result.get("label") == "Kenyon cell":
131+
print(" ✅ Cache retrieval successful")
132+
print(f" 📄 Retrieved result: {retrieved_result.get('label')} ({retrieved_result.get('short_form')})")
133+
else:
134+
print(f" ❌ Retrieved unexpected result: {retrieved_result}")
135+
return False
136+
else:
137+
print(" ❌ Cache retrieval failed")
138+
return False
139+
140+
# Step 5: Test cache age information
141+
print("\n5. Testing cache metadata...")
142+
143+
cache_age = cache.get_cache_age("term_info", test_term_id)
144+
145+
if cache_age:
146+
print(f" ✅ Cache age retrieved")
147+
print(f" ⏱️ Age: {cache_age.get('age_minutes', 0):.1f} minutes")
148+
print(f" 📅 Expires in: {cache_age.get('days_until_expiration', 0):.1f} days")
149+
print(f" 👁️ Hit count: {cache_age.get('hit_count', 0)}")
150+
else:
151+
print(" ❌ Cache age retrieval failed")
152+
return False
153+
154+
print("\n" + "=" * 50)
155+
print("🎉 ALL TESTS PASSED - Production cache system is working correctly!")
156+
print("\n✅ Verified capabilities:")
157+
print(" • Original VFB data preservation")
158+
print(" • Cache data storage and retrieval")
159+
print(" • Metadata tracking and expiration")
160+
print(" • Field coexistence in single document")
161+
162+
return True
163+
164+
if __name__ == "__main__":
165+
success = test_production_cache()
166+
exit(0 if success else 1)

0 commit comments

Comments
 (0)