mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:11:09 -05:00
fix: Update import paths to use 'apps' prefix for models and services
This commit is contained in:
@@ -9,14 +9,21 @@ This script tests all functionality of the OSM Road Trip Service including:
|
||||
- Integration with existing Park models
|
||||
"""
|
||||
|
||||
from typing import cast
|
||||
from apps.parks.services import RoadTripService
|
||||
from apps.parks.services.roadtrip import Coordinates
|
||||
from apps.parks.models import Park
|
||||
from django.core.cache import cache
|
||||
from parks.models import Park
|
||||
from parks.services.roadtrip import Coordinates
|
||||
from parks.services import RoadTripService
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
|
||||
# Setup Django
|
||||
# Ensure project root is on sys.path so imports like `parks.models` resolve.
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if PROJECT_ROOT not in sys.path:
|
||||
sys.path.insert(0, PROJECT_ROOT)
|
||||
|
||||
# Setup Django before importing project modules that depend on settings.
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "thrillwiki.settings")
|
||||
django.setup()
|
||||
|
||||
@@ -40,16 +47,13 @@ def test_geocoding():
|
||||
print(f"\nGeocoding: {address}")
|
||||
coords = service.geocode_address(address)
|
||||
if coords:
|
||||
print(
|
||||
f" ✅ Success: {
|
||||
coords.latitude:.6f}, {
|
||||
coords.longitude:.6f}"
|
||||
)
|
||||
# fixed: single-line f-string formatting
|
||||
print(f" ✅ Success: {coords.latitude:.6f}, {coords.longitude:.6f}")
|
||||
else:
|
||||
print(f" ❌ Failed")
|
||||
print(" ❌ Failed")
|
||||
|
||||
# Test cache functionality
|
||||
print(f"\nTesting cache...")
|
||||
print("\nTesting cache...")
|
||||
coords1 = service.geocode_address("Cedar Point, Sandusky, Ohio")
|
||||
coords2 = service.geocode_address("Cedar Point, Sandusky, Ohio")
|
||||
if coords1 and coords2:
|
||||
@@ -66,30 +70,30 @@ def test_route_calculation():
|
||||
cedar_point = Coordinates(41.4793, -82.6833)
|
||||
magic_kingdom = Coordinates(28.4177, -81.5812)
|
||||
|
||||
print(f"Calculating route from Cedar Point to Magic Kingdom...")
|
||||
print("Calculating route from Cedar Point to Magic Kingdom...")
|
||||
route = service.calculate_route(cedar_point, magic_kingdom)
|
||||
|
||||
if route:
|
||||
print(f" ✅ Success:")
|
||||
print(" ✅ Success:")
|
||||
print(f" Distance: {route.formatted_distance}")
|
||||
print(f" Duration: {route.formatted_duration}")
|
||||
print(f" Geometry: {'Yes' if route.geometry else 'No'}")
|
||||
else:
|
||||
print(f" ❌ Failed")
|
||||
print(" ❌ Failed")
|
||||
|
||||
# Test short distance (should use OSRM)
|
||||
disneyland = Coordinates(33.8121, -117.9190)
|
||||
knotts = Coordinates(33.8442, -118.0000)
|
||||
|
||||
print(f"\nCalculating route from Disneyland to Knott's Berry Farm...")
|
||||
print("\nCalculating route from Disneyland to Knott's Berry Farm...")
|
||||
route = service.calculate_route(disneyland, knotts)
|
||||
|
||||
if route:
|
||||
print(f" ✅ Success:")
|
||||
print(" ✅ Success:")
|
||||
print(f" Distance: {route.formatted_distance}")
|
||||
print(f" Duration: {route.formatted_duration}")
|
||||
else:
|
||||
print(f" ❌ Failed")
|
||||
print(" ❌ Failed")
|
||||
|
||||
|
||||
def test_park_integration():
|
||||
@@ -108,24 +112,25 @@ def test_park_integration():
|
||||
print(f"Found {len(parks)} parks to test with:")
|
||||
for park in parks:
|
||||
print(f" - {park.name}")
|
||||
if hasattr(park, "location") and park.location:
|
||||
coords = park.coordinates
|
||||
# Use getattr to safely access the optional related attribute and avoid static type warnings.
|
||||
location = getattr(park, "location", None)
|
||||
if location:
|
||||
coords = getattr(park, "coordinates", None)
|
||||
if coords:
|
||||
print(f" 📍 {coords[0]:.4f}, {coords[1]:.4f}")
|
||||
else:
|
||||
print(f" 📍 No coordinates, will try to geocode...")
|
||||
print(" 📍 No coordinates, will try to geocode...")
|
||||
success = service.geocode_park_if_needed(park)
|
||||
if success:
|
||||
coords = park.coordinates
|
||||
print(
|
||||
f" ✅ Geocoded to: {
|
||||
coords[0]:.4f}, {
|
||||
coords[1]:.4f}"
|
||||
)
|
||||
coords = getattr(park, "coordinates", None)
|
||||
if coords:
|
||||
print(f" ✅ Geocoded to: {coords[0]:.4f}, {coords[1]:.4f}")
|
||||
else:
|
||||
print(" ❌ Geocoding succeeded but coordinates still missing")
|
||||
else:
|
||||
print(f" ❌ Geocoding failed")
|
||||
print(" ❌ Geocoding failed")
|
||||
else:
|
||||
print(f" ❌ No location data")
|
||||
print(" ❌ No location data")
|
||||
|
||||
|
||||
def test_nearby_parks():
|
||||
@@ -153,13 +158,9 @@ def test_nearby_parks():
|
||||
for result in nearby_parks[:5]: # Show first 5
|
||||
park = result["park"]
|
||||
print(
|
||||
f" {
|
||||
park.name}: {
|
||||
result['formatted_distance']}, {
|
||||
result['formatted_duration']}"
|
||||
)
|
||||
f" {park.name}: {result['formatted_distance']}, {result['formatted_duration']}")
|
||||
else:
|
||||
print(f" ❌ No nearby parks found")
|
||||
print(" ❌ No nearby parks found")
|
||||
|
||||
|
||||
def test_route_park_discovery():
|
||||
@@ -180,11 +181,7 @@ def test_route_park_discovery():
|
||||
start_park = parks_with_location[0]
|
||||
end_park = parks_with_location[1]
|
||||
|
||||
print(
|
||||
f"Finding parks along route from {
|
||||
start_park.name} to {
|
||||
end_park.name}..."
|
||||
)
|
||||
print(f"Finding parks along route from {start_park.name} to {end_park.name}...")
|
||||
|
||||
parks_along_route = service.find_parks_along_route(
|
||||
start_park, end_park, max_detour_km=100
|
||||
@@ -195,7 +192,7 @@ def test_route_park_discovery():
|
||||
for park in parks_along_route[:3]: # Show first 3
|
||||
print(f" - {park.name}")
|
||||
else:
|
||||
print(f" ❌ No parks found along route")
|
||||
print(" ❌ No parks found along route")
|
||||
|
||||
|
||||
def test_multi_park_trip():
|
||||
@@ -221,19 +218,16 @@ def test_multi_park_trip():
|
||||
trip = service.create_multi_park_trip(parks_list)
|
||||
|
||||
if trip:
|
||||
print(f" ✅ Trip planned successfully:")
|
||||
print(" ✅ Trip planned successfully:")
|
||||
print(f" Total Distance: {trip.formatted_total_distance}")
|
||||
print(f" Total Duration: {trip.formatted_total_duration}")
|
||||
print(f" Route:")
|
||||
print(" Route:")
|
||||
for i, leg in enumerate(trip.legs, 1):
|
||||
print(f" {i}. {leg.from_park.name} → {leg.to_park.name}")
|
||||
print(
|
||||
f" {
|
||||
leg.route.formatted_distance}, {
|
||||
leg.route.formatted_duration}"
|
||||
)
|
||||
f" {leg.route.formatted_distance}, {leg.route.formatted_duration}")
|
||||
else:
|
||||
print(f" ❌ Trip planning failed")
|
||||
print(" ❌ Trip planning failed")
|
||||
|
||||
|
||||
def test_error_handling():
|
||||
@@ -249,28 +243,28 @@ def test_error_handling():
|
||||
|
||||
route = service.calculate_route(invalid_coords, valid_coords)
|
||||
if route:
|
||||
print(
|
||||
f" ⚠️ Got route with invalid coords: {
|
||||
route.formatted_distance}"
|
||||
)
|
||||
print(f" ⚠️ Got route with invalid coords: {route.formatted_distance}")
|
||||
else:
|
||||
print(f" ✅ Correctly handled invalid coordinates")
|
||||
print(" ✅ Correctly handled invalid coordinates")
|
||||
|
||||
# Test with empty address
|
||||
print("Testing empty address geocoding...")
|
||||
coords = service.geocode_address("")
|
||||
if coords:
|
||||
print(f" ⚠️ Got coordinates for empty address")
|
||||
print(" ⚠️ Got coordinates for empty address")
|
||||
else:
|
||||
print(f" ✅ Correctly handled empty address")
|
||||
print(" ✅ Correctly handled empty address")
|
||||
|
||||
# Test with None values
|
||||
print("Testing None coordinates...")
|
||||
route = service.calculate_route(None, valid_coords)
|
||||
# cast(None, Coordinates) is used only to satisfy static typing checks
|
||||
# while keeping the runtime intent of passing None to the service.
|
||||
route = service.calculate_route(cast(Coordinates, None), valid_coords)
|
||||
|
||||
if route:
|
||||
print(f" ⚠️ Got route with None coordinates")
|
||||
print(" ⚠️ Got route with None coordinates")
|
||||
else:
|
||||
print(f" ✅ Correctly handled None coordinates")
|
||||
print(" ✅ Correctly handled None coordinates")
|
||||
|
||||
|
||||
def test_rate_limiting():
|
||||
@@ -294,19 +288,15 @@ def test_rate_limiting():
|
||||
for address in addresses:
|
||||
coords = service.geocode_address(address)
|
||||
if coords:
|
||||
print(
|
||||
f" ✅ {address}: {
|
||||
coords.latitude:.4f}, {
|
||||
coords.longitude:.4f}"
|
||||
)
|
||||
print(f" ✅ {address}: {coords.latitude:.4f}, {coords.longitude:.4f}")
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
print(f" Total time for 3 requests: {elapsed:.2f} seconds")
|
||||
|
||||
if elapsed >= 2.0: # Should take at least 2 seconds with 1 req/sec limit
|
||||
print(f" ✅ Rate limiting appears to be working")
|
||||
print(" ✅ Rate limiting appears to be working")
|
||||
else:
|
||||
print(f" ⚠️ Requests may have been cached or rate limiting not working")
|
||||
print(" ⚠️ Requests may have been cached or rate limiting not working")
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user