api_verify.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import time
  2. import httpx
  3. import asyncio
  4. import json
  5. async def run_tests():
  6. url = "http://127.0.0.1:8000"
  7. async with httpx.AsyncClient() as client:
  8. # Create test user for token
  9. try:
  10. await client.post(f"{url}/api/register", json={"username":"validator","password":"pwdtest123","confirmPassword":"pwdtest123"})
  11. except:
  12. pass
  13. res = await client.post(f"{url}/api/login", json={"username":"validator","password":"pwdtest123"})
  14. token = res.json().get("token", "")
  15. headers = {"Authorization": f"Bearer {token}"}
  16. print("=== TEST 1: Empty Query ===")
  17. t0 = time.perf_counter()
  18. r1 = await client.get(f"{url}/api/food/search?q=", headers=headers)
  19. t1 = time.perf_counter()
  20. print(f"Timing: {(t1-t0)*1000:.2f} ms")
  21. print(f"Payload snippet: {r1.text[:100]}")
  22. print("\n=== TEST 2: Single Letter ('c') ===")
  23. t0 = time.perf_counter()
  24. r2 = await client.get(f"{url}/api/food/search?q=c", headers=headers)
  25. t1 = time.perf_counter()
  26. print(f"Timing: {(t1-t0)*1000:.2f} ms")
  27. res2 = r2.json().get('results', [])
  28. print(f"Returned Items: {len(res2)}")
  29. if res2:
  30. print(f"Expected Field Verification: {list(res2[0].keys())}")
  31. print("\n=== TEST 3: No Match ('xyzabcd') ===")
  32. t0 = time.perf_counter()
  33. r3 = await client.get(f"{url}/api/food/search?q=xyzabcd", headers=headers)
  34. t1 = time.perf_counter()
  35. print(f"Timing: {(t1-t0)*1000:.2f} ms")
  36. print(f"Response: {r3.json()}")
  37. asyncio.run(run_tests())