Spaces:
Running
Running
| """Integration tests against running FastAPI server + SearXNG container.""" | |
| import json | |
| import urllib.request | |
| import time | |
| BASE = "http://localhost:8000/api/v1" | |
| HEADERS = { | |
| "Authorization": "Bearer N9ooESH05AiXrlpEKilv3o7OY1Rl5Pui", | |
| "Content-Type": "application/json", | |
| } | |
| def req(method, path, body=None): | |
| url = f"{BASE}{path}" | |
| data = json.dumps(body).encode() if body else None | |
| r = urllib.request.Request(url, data=data, headers=HEADERS, method=method) | |
| resp = urllib.request.urlopen(r, timeout=30) | |
| return json.loads(resp.read()) | |
| passed = 0 | |
| failed = 0 | |
| def check(name, ok, detail=""): | |
| global passed, failed | |
| if ok: | |
| passed += 1 | |
| print(f" PASS: {name}") | |
| else: | |
| failed += 1 | |
| print(f" FAIL: {name} - {detail}") | |
| # ---- Test 1: Health ---- | |
| print("1. Health check") | |
| r = req("GET", "/web-search/health") | |
| check("health returns dict", isinstance(r, dict)) | |
| # ---- Test 2: Search GET ---- | |
| print("\n2. Search GET") | |
| r = req("GET", "/web-search?q=hello+world&max_results=3") | |
| check("success=True", r["success"] is True) | |
| check("has results", r["number_of_results"] > 0) | |
| check("title is string", isinstance(r["results"][0]["title"], str)) | |
| # ---- Test 3: Search POST ---- | |
| print("\n3. Search POST") | |
| r = req("POST", "/web-search", {"q": "python programming", "categories": "general,it", "max_results": 3}) | |
| check("success=True", r["success"] is True) | |
| check("has results", r["number_of_results"] > 0) | |
| # ---- Test 4: Autocomplete GET ---- | |
| print("\n4. Autocomplete GET") | |
| r = req("GET", "/web-search/autocomplete?q=hello+wor") | |
| check("success=True", r["success"] is True) | |
| check("has suggestions", len(r["suggestions"]) > 0) | |
| # ---- Test 5: Autocomplete POST ---- | |
| print("\n5. Autocomplete POST") | |
| r = req("POST", "/web-search/autocomplete", {"q": "python progr"}) | |
| check("success=True", r["success"] is True) | |
| check("has suggestions", len(r["suggestions"]) > 0) | |
| # ---- Test 6: Config ---- | |
| print("\n6. Config") | |
| r = req("GET", "/web-search/config") | |
| check("success=True", r["success"] is True) | |
| check("has engines list", isinstance(r["engines"], list)) | |
| # ---- Test 7: Engine descriptions ---- | |
| print("\n7. Engine descriptions") | |
| r = req("GET", "/web-search/engine-descriptions") | |
| check("success=True", r["success"] is True) | |
| check("has engines dict", isinstance(r["engines"], dict)) | |
| check("many engines", len(r["engines"]) > 10) | |
| # ---- Test 8: Stats ---- | |
| print("\n8. Stats") | |
| r = req("GET", "/web-search/stats") | |
| check("success=True", r["success"] is True) | |
| # ---- Summary ---- | |
| print(f"\n{'='*50}") | |
| print(f"RESULTS: {passed} passed, {failed} failed out of {passed+failed} tests") | |
| if failed == 0: | |
| print("ALL INTEGRATION TESTS PASSED") | |
| else: | |
| print("SOME TESTS FAILED") | |
| import sys | |
| sys.exit(0 if failed == 0 else 1) | |