Files
thrillwiki_django_no_react/backend/apps/location/tests.py
pacnpal d504d41de2 feat: complete monorepo structure with frontend and shared resources
- Add complete backend/ directory with full Django application
- Add frontend/ directory with Vite + TypeScript setup ready for Next.js
- Add comprehensive shared/ directory with:
  - Complete documentation and memory-bank archives
  - Media files and avatars (letters, park/ride images)
  - Deployment scripts and automation tools
  - Shared types and utilities
- Add architecture/ directory with migration guides
- Configure pnpm workspace for monorepo development
- Update .gitignore to exclude .django_tailwind_cli/ build artifacts
- Preserve all historical documentation in shared/docs/memory-bank/
- Set up proper structure for full-stack development with shared resources
2025-08-23 18:40:07 -04:00

182 lines
7.4 KiB
Python

from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.geos import Point
from .models import Location
from apps.parks.models import Park, Company as Operator
class LocationModelTests(TestCase):
def setUp(self):
# Create test company
self.operator = Operator.objects.create(
name="Test Operator", website="http://example.com"
)
# Create test park
self.park = Park.objects.create(
name="Test Park", owner=self.operator, status="OPERATING"
)
# Create test location for company
self.operator_location = Location.objects.create(
content_type=ContentType.objects.get_for_model(Operator),
object_id=self.operator.pk,
name="Test Operator HQ",
location_type="business",
street_address="123 Operator St",
city="Operator City",
state="CS",
country="Test Country",
postal_code="12345",
point=Point(-118.2437, 34.0522), # Los Angeles coordinates
)
# Create test location for park
self.park_location = Location.objects.create(
content_type=ContentType.objects.get_for_model(Park),
object_id=self.park.pk,
name="Test Park Location",
location_type="park",
street_address="456 Park Ave",
city="Park City",
state="PC",
country="Test Country",
postal_code="67890",
point=Point(-111.8910, 40.7608), # Park City coordinates
)
def test_location_creation(self):
"""Test location instance creation and field values"""
# Test company location
self.assertEqual(self.operator_location.name, "Test Operator HQ")
self.assertEqual(self.operator_location.location_type, "business")
self.assertEqual(self.operator_location.street_address, "123 Operator St")
self.assertEqual(self.operator_location.city, "Operator City")
self.assertEqual(self.operator_location.state, "CS")
self.assertEqual(self.operator_location.country, "Test Country")
self.assertEqual(self.operator_location.postal_code, "12345")
self.assertIsNotNone(self.operator_location.point)
# Test park location
self.assertEqual(self.park_location.name, "Test Park Location")
self.assertEqual(self.park_location.location_type, "park")
self.assertEqual(self.park_location.street_address, "456 Park Ave")
self.assertEqual(self.park_location.city, "Park City")
self.assertEqual(self.park_location.state, "PC")
self.assertEqual(self.park_location.country, "Test Country")
self.assertEqual(self.park_location.postal_code, "67890")
self.assertIsNotNone(self.park_location.point)
def test_location_str_representation(self):
"""Test string representation of location"""
expected_company_str = "Test Operator HQ (Operator City, Test Country)"
self.assertEqual(str(self.operator_location), expected_company_str)
expected_park_str = "Test Park Location (Park City, Test Country)"
self.assertEqual(str(self.park_location), expected_park_str)
def test_get_formatted_address(self):
"""Test get_formatted_address method"""
expected_address = "123 Operator St, Operator City, CS, 12345, Test Country"
self.assertEqual(
self.operator_location.get_formatted_address(), expected_address
)
def test_point_coordinates(self):
"""Test point coordinates"""
# Test company location point
self.assertIsNotNone(self.operator_location.point)
self.assertAlmostEqual(
self.operator_location.point.y, 34.0522, places=4
) # latitude
self.assertAlmostEqual(
self.operator_location.point.x, -118.2437, places=4
) # longitude
# Test park location point
self.assertIsNotNone(self.park_location.point)
self.assertAlmostEqual(
self.park_location.point.y, 40.7608, places=4
) # latitude
self.assertAlmostEqual(
self.park_location.point.x, -111.8910, places=4
) # longitude
def test_coordinates_property(self):
"""Test coordinates property"""
company_coords = self.operator_location.coordinates
self.assertIsNotNone(company_coords)
self.assertAlmostEqual(company_coords[0], 34.0522, places=4) # latitude
self.assertAlmostEqual(company_coords[1], -118.2437, places=4) # longitude
park_coords = self.park_location.coordinates
self.assertIsNotNone(park_coords)
self.assertAlmostEqual(park_coords[0], 40.7608, places=4) # latitude
self.assertAlmostEqual(park_coords[1], -111.8910, places=4) # longitude
def test_distance_calculation(self):
"""Test distance_to method"""
distance = self.operator_location.distance_to(self.park_location)
self.assertIsNotNone(distance)
self.assertGreater(distance, 0)
def test_nearby_locations(self):
"""Test nearby_locations method"""
# Create another location near the company location
nearby_location = Location.objects.create(
content_type=ContentType.objects.get_for_model(Operator),
object_id=self.operator.pk,
name="Nearby Location",
location_type="business",
street_address="789 Nearby St",
city="Operator City",
country="Test Country",
point=Point(-118.2438, 34.0523), # Very close to company location
)
nearby = self.operator_location.nearby_locations(distance_km=1)
self.assertEqual(nearby.count(), 1)
self.assertEqual(nearby.first(), nearby_location)
def test_content_type_relations(self):
"""Test generic relations work correctly"""
# Test company location relation
company_location = Location.objects.get(
content_type=ContentType.objects.get_for_model(Operator),
object_id=self.operator.pk,
)
self.assertEqual(company_location, self.operator_location)
# Test park location relation
park_location = Location.objects.get(
content_type=ContentType.objects.get_for_model(Park),
object_id=self.park.pk,
)
self.assertEqual(park_location, self.park_location)
def test_location_updates(self):
"""Test location updates"""
# Update company location
self.operator_location.street_address = "Updated Address"
self.operator_location.city = "Updated City"
self.operator_location.save()
updated_location = Location.objects.get(pk=self.operator_location.pk)
self.assertEqual(updated_location.street_address, "Updated Address")
self.assertEqual(updated_location.city, "Updated City")
def test_point_sync_with_lat_lon(self):
"""Test point synchronization with latitude/longitude fields"""
location = Location.objects.create(
content_type=ContentType.objects.get_for_model(Operator),
object_id=self.operator.pk,
name="Test Sync Location",
location_type="business",
latitude=34.0522,
longitude=-118.2437,
)
self.assertIsNotNone(location.point)
self.assertAlmostEqual(location.point.y, 34.0522, places=4)
self.assertAlmostEqual(location.point.x, -118.2437, places=4)