Spaces:
Sleeping
Sleeping
| """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()) | |