Spaces:
Running
Running
File size: 7,733 Bytes
9e62548 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | #!/usr/bin/env python3
"""
๐งช Test Script for Knowledge Base Endpoints
Run this to verify all 3 new FREE endpoints are working
"""
import sys
sys.path.insert(0, '.')
from knowledge_base import (
get_zodiac_profile,
get_planet_in_sign,
get_planet_in_house,
clear_cache,
ZODIAC_SIGNS,
AVAILABLE_PLANETS_SIGNS,
AVAILABLE_PLANETS_HOUSES
)
import time
def test_zodiac_profiles():
"""Test all 12 zodiac sign profiles"""
print("\n" + "="*80)
print("1๏ธโฃ Testing Zodiac Profiles (12 signs)")
print("="*80)
for sign in ZODIAC_SIGNS:
try:
start = time.time()
result = get_zodiac_profile(sign)
elapsed = (time.time() - start) * 1000
assert result['sign'].lower() == sign
assert result['symbol'] in ['โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ']
assert result['element'] in ['Fire', 'Earth', 'Air', 'Water']
assert len(result['essence']) > 100
assert len(result['keywords']) > 0
print(f" โ
{sign.capitalize():12} - {result['symbol']} {result['element']:5} - "
f"{elapsed:5.1f}ms - {len(result['essence'])} chars")
except Exception as e:
print(f" โ {sign.capitalize():12} - Error: {e}")
return False
print(f"\n ๐ All 12 zodiac profiles working!")
return True
def test_planets_in_signs():
"""Test Sun and Moon in all 12 signs"""
print("\n" + "="*80)
print(f"2๏ธโฃ Testing Planets in Signs ({len(AVAILABLE_PLANETS_SIGNS)} planets ร 12 signs = {len(AVAILABLE_PLANETS_SIGNS) * 12} combinations)")
print("="*80)
for planet in AVAILABLE_PLANETS_SIGNS:
print(f"\n {planet.upper()} in Signs:")
for sign in ZODIAC_SIGNS:
try:
start = time.time()
result = get_planet_in_sign(planet, sign)
elapsed = (time.time() - start) * 1000
assert result['planet'].lower() == planet
assert result['sign'].lower() == sign
assert len(result['full_content']) > 100
status = "โ
"
if result.get('core_traits'):
status += f" ({len(result['core_traits'])} traits)"
print(f" {status:20} {sign.capitalize():12} - {elapsed:5.1f}ms")
except Exception as e:
print(f" โ {sign.capitalize():12} - Error: {e}")
return False
print(f"\n ๐ All 24 planet-in-sign combinations working!")
return True
def test_planets_in_houses():
"""Test Sun in all 12 houses (Moon not yet available)"""
print("\n" + "="*80)
print(f"3๏ธโฃ Testing Planets in Houses ({len(AVAILABLE_PLANETS_HOUSES)} planet ร 12 houses = {len(AVAILABLE_PLANETS_HOUSES) * 12} combinations)")
print("="*80)
for planet in AVAILABLE_PLANETS_HOUSES:
print(f"\n {planet.upper()} in Houses:")
for house in range(1, 13):
try:
start = time.time()
result = get_planet_in_house(planet, house)
elapsed = (time.time() - start) * 1000
assert result['planet'].lower() == planet
assert result['house'] == house
assert len(result['full_content']) > 100
assert 'life_focus' in result
ordinal = result['ordinal']
focus = result['life_focus'][:40]
print(f" โ
House {house:2} ({ordinal:3}) - {elapsed:5.1f}ms - {focus}...")
except Exception as e:
print(f" โ House {house:2} - Error: {e}")
return False
print(f"\n ๐ All {len(AVAILABLE_PLANETS_HOUSES) * 12} planet-in-house combinations working!")
return True
def test_error_handling():
"""Test error cases"""
print("\n" + "="*80)
print("4๏ธโฃ Testing Error Handling")
print("="*80)
tests_passed = 0
tests_total = 4
# Test 1: Invalid zodiac sign
try:
get_zodiac_profile('invalid_sign')
print(" โ Should have raised error for invalid sign")
except ValueError:
print(" โ
Invalid zodiac sign - correctly raised ValueError")
tests_passed += 1
# Test 2: Planet not available
try:
get_planet_in_sign('venus', 'aries')
print(" โ Should have raised error for unavailable planet")
except ValueError as e:
if 'not yet available' in str(e):
print(" โ
Unavailable planet - correctly raised ValueError with helpful message")
tests_passed += 1
# Test 3: Invalid house
try:
get_planet_in_house('sun', 15)
print(" โ Should have raised error for invalid house")
except ValueError:
print(" โ
Invalid house - correctly raised ValueError")
tests_passed += 1
# Test 4: Invalid house (0)
try:
get_planet_in_house('sun', 0)
print(" โ Should have raised error for house 0")
except ValueError:
print(" โ
House 0 - correctly raised ValueError")
tests_passed += 1
print(f"\n ๐ Error handling: {tests_passed}/{tests_total} tests passed")
return tests_passed == tests_total
def test_caching():
"""Test caching performance"""
print("\n" + "="*80)
print("5๏ธโฃ Testing Cache Performance")
print("="*80)
# Clear cache first
clear_cache()
# First call (should be slower)
start = time.time()
result1 = get_zodiac_profile('aries')
time1 = (time.time() - start) * 1000
# Second call (should be much faster from cache)
start = time.time()
result2 = get_zodiac_profile('aries')
time2 = (time.time() - start) * 1000
# Verify results are identical
assert result1 == result2
speedup = time1 / time2 if time2 > 0 else 0
print(f" First call: {time1:6.2f}ms (file read + parse)")
print(f" Second call: {time2:6.2f}ms (cached)")
print(f" Speedup: {speedup:6.1f}x faster")
if speedup > 5:
print(" โ
Caching is working excellently!")
return True
elif speedup > 2:
print(" โ
Caching is working!")
return True
else:
print(" โ ๏ธ Cache speedup lower than expected")
return False
def main():
"""Run all tests"""
print("\n" + "="*80)
print("๐ฎ KNOWLEDGE BASE API - COMPREHENSIVE TEST SUITE")
print("="*80)
print("Testing all 3 FREE endpoints with 72+ combinations")
print("="*80)
results = {}
# Run all tests
results['zodiac_profiles'] = test_zodiac_profiles()
results['planets_in_signs'] = test_planets_in_signs()
results['planets_in_houses'] = test_planets_in_houses()
results['error_handling'] = test_error_handling()
results['caching'] = test_caching()
# Summary
print("\n" + "="*80)
print("๐ TEST SUMMARY")
print("="*80)
total = len(results)
passed = sum(1 for v in results.values() if v)
for name, passed_test in results.items():
status = "โ
PASS" if passed_test else "โ FAIL"
print(f" {status:10} - {name.replace('_', ' ').title()}")
print("\n" + "="*80)
print(f" TOTAL: {passed}/{total} test suites passed")
print("="*80)
if passed == total:
print("\n ๐ ALL TESTS PASSED! Ready for deployment!")
print(" ๐ฐ Zero AI cost, <100ms response time")
print(" ๐ Deploy to Hugging Face Spaces now!")
return 0
else:
print("\n โ ๏ธ Some tests failed. Please review and fix.")
return 1
if __name__ == "__main__":
exit(main())
|