mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 10:11:09 -05:00
series of tests added with built-in django test support
This commit is contained in:
176
location/tests.py
Normal file
176
location/tests.py
Normal file
@@ -0,0 +1,176 @@
|
||||
from django.test import TestCase
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.contrib.gis.measure import D
|
||||
from .models import Location
|
||||
from companies.models import Company
|
||||
from parks.models import Park
|
||||
|
||||
class LocationModelTests(TestCase):
|
||||
def setUp(self):
|
||||
# Create test company
|
||||
self.company = Company.objects.create(
|
||||
name='Test Company',
|
||||
website='http://example.com'
|
||||
)
|
||||
|
||||
# Create test park
|
||||
self.park = Park.objects.create(
|
||||
name='Test Park',
|
||||
owner=self.company,
|
||||
status='OPERATING'
|
||||
)
|
||||
|
||||
# Create test location for company
|
||||
self.company_location = Location.objects.create(
|
||||
content_type=ContentType.objects.get_for_model(Company),
|
||||
object_id=self.company.pk,
|
||||
name='Test Company HQ',
|
||||
location_type='business',
|
||||
street_address='123 Company St',
|
||||
city='Company 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.company_location.name, 'Test Company HQ')
|
||||
self.assertEqual(self.company_location.location_type, 'business')
|
||||
self.assertEqual(self.company_location.street_address, '123 Company St')
|
||||
self.assertEqual(self.company_location.city, 'Company City')
|
||||
self.assertEqual(self.company_location.state, 'CS')
|
||||
self.assertEqual(self.company_location.country, 'Test Country')
|
||||
self.assertEqual(self.company_location.postal_code, '12345')
|
||||
self.assertIsNotNone(self.company_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 Company HQ (Company City, Test Country)'
|
||||
self.assertEqual(str(self.company_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 Company St, Company City, CS, 12345, Test Country'
|
||||
self.assertEqual(self.company_location.get_formatted_address(), expected_address)
|
||||
|
||||
def test_point_coordinates(self):
|
||||
"""Test point coordinates"""
|
||||
# Test company location point
|
||||
self.assertIsNotNone(self.company_location.point)
|
||||
self.assertAlmostEqual(self.company_location.point.y, 34.0522, places=4) # latitude
|
||||
self.assertAlmostEqual(self.company_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.company_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.company_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(Company),
|
||||
object_id=self.company.pk,
|
||||
name='Nearby Location',
|
||||
location_type='business',
|
||||
street_address='789 Nearby St',
|
||||
city='Company City',
|
||||
country='Test Country',
|
||||
point=Point(-118.2438, 34.0523) # Very close to company location
|
||||
)
|
||||
|
||||
nearby = self.company_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(Company),
|
||||
object_id=self.company.pk
|
||||
)
|
||||
self.assertEqual(company_location, self.company_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.company_location.street_address = 'Updated Address'
|
||||
self.company_location.city = 'Updated City'
|
||||
self.company_location.save()
|
||||
|
||||
updated_location = Location.objects.get(pk=self.company_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(Company),
|
||||
object_id=self.company.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)
|
||||
Reference in New Issue
Block a user