Spaces:
Sleeping
Sleeping
File size: 2,919 Bytes
1d32142 | 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 | """Test the lookalike API endpoint."""
import asyncio
from app import find_lookalike_clients, LookalikeRequest
async def test_lookalike_endpoint():
"""Test the lookalike endpoint with hybrid matching."""
print("=" * 60)
print("Testing Lookalike API Endpoint")
print("=" * 60)
# Test 1: Basic request
print("\nTest 1: Basic request with education cause")
req = LookalikeRequest(
seed_causes=["education"],
seed_interests=["sustainability"],
limit=15,
include_geojson=True,
)
result = await find_lookalike_clients(req)
print(f"Total found: {result.total_found}")
print(
f"Tiers: T1={len(result.tiers['tier_1'])}, T2={len(result.tiers['tier_2'])}, T3={len(result.tiers['tier_3'])}"
)
print(
f"GeoJSON features: {len(result.geojson['features']) if result.geojson else 0}"
)
if result.tiers["tier_1"]:
top = result.tiers["tier_1"][0]
print(f"\nTop match: {top.user_id}")
print(f" Score: {top.final_score:.3f}")
print(f" Causes: {top.causes}")
print(f" Area: {top.planning_area}")
# Test 2: With planning area filter
print("\n" + "-" * 60)
print("Test 2: With planning area filter (bishan)")
req2 = LookalikeRequest(
seed_causes=["education", "children"],
seed_interests=["community"],
planning_area_filter="bishan",
limit=10,
include_geojson=False,
)
result2 = await find_lookalike_clients(req2)
print(f"Total found in Bishan: {result2.total_found}")
# Test 3: With housing type filter
print("\n" + "-" * 60)
print("Test 3: With housing type filter (condo, landed)")
req3 = LookalikeRequest(
seed_causes=["environment"],
seed_interests=["technology"],
housing_type_filter=["condo", "landed"],
limit=10,
include_geojson=False,
)
result3 = await find_lookalike_clients(req3)
print(f"Total found (high-income housing): {result3.total_found}")
for client in result3.tiers["tier_1"][:3]:
print(
f" - {client.user_id}: {client.housing_type}, score={client.final_score:.3f}"
)
# Test 4: Low minimum score to get all matches
print("\n" + "-" * 60)
print("Test 4: Relaxed min_score (0.0)")
req4 = LookalikeRequest(
seed_causes=["health"],
seed_interests=[],
min_score=0.0,
limit=30,
include_geojson=True,
)
result4 = await find_lookalike_clients(req4)
print(f"Total found: {result4.total_found}")
print(
f"Score range: {min(c.final_score for t in result4.tiers.values() for c in t):.3f} - {max(c.final_score for t in result4.tiers.values() for c in t):.3f}"
)
print("\n" + "=" * 60)
print("All API tests passed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(test_lookalike_endpoint())
|