Refactor test utilities and enhance ASGI settings

- Cleaned up and standardized assertions in ApiTestMixin for API response validation.
- Updated ASGI settings to use os.environ for setting the DJANGO_SETTINGS_MODULE.
- Removed unused imports and improved formatting in settings.py.
- Refactored URL patterns in urls.py for better readability and organization.
- Enhanced view functions in views.py for consistency and clarity.
- Added .flake8 configuration for linting and style enforcement.
- Introduced type stubs for django-environ to improve type checking with Pylance.
This commit is contained in:
pacnpal
2025-08-20 19:51:59 -04:00
parent 69c07d1381
commit 66ed4347a9
230 changed files with 15094 additions and 11578 deletions

View File

@@ -2,33 +2,29 @@
Tests for park models functionality including CRUD operations,
slug handling, status management, and location integration.
"""
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.utils import timezone
from datetime import date
from parks.models import Park, ParkArea, ParkLocation
from parks.models.companies import Company
from django.test import TestCase
from django.db import IntegrityError
from parks.models import Park, ParkArea, ParkLocation, Company
# NOTE: These tests need to be updated to work with the new ParkLocation model
# instead of the generic Location model
class ParkModelTests(TestCase):
def setUp(self):
"""Set up test data"""
self.operator = Company.objects.create(
name="Test Company",
slug="test-company"
)
self.operator = Company.objects.create(name="Test Company", slug="test-company")
# Create a basic park
self.park = Park.objects.create(
name="Test Park",
description="A test park",
status="OPERATING",
operator=self.operator
operator=self.operator,
)
# Create location for the park
self.location = ParkLocation.objects.create(
park=self.park,
@@ -53,7 +49,7 @@ class ParkModelTests(TestCase):
park = Park.objects.create(
name="Another Test Park",
status="OPERATING",
operator=self.operator
operator=self.operator,
)
self.assertEqual(park.slug, "another-test-park")
@@ -62,40 +58,40 @@ class ParkModelTests(TestCase):
from django.db import transaction
from django.contrib.contenttypes.models import ContentType
from core.history import HistoricalSlug
with transaction.atomic():
# Create initial park with a specific name/slug
park = Park.objects.create(
name="Original Park Name",
description="Test description",
status="OPERATING",
operator=self.operator
operator=self.operator,
)
original_slug = park.slug
print(f"\nInitial park created with slug: {original_slug}")
# Ensure we have a save to trigger history
park.save()
# Modify name to trigger slug change
park.name = "Updated Park Name"
park.save()
new_slug = park.slug
print(f"Park updated with new slug: {new_slug}")
# Check HistoricalSlug records
historical_slugs = HistoricalSlug.objects.filter(
content_type=ContentType.objects.get_for_model(Park),
object_id=park.id
object_id=park.id,
)
print(f"Historical slug records: {[h.slug for h in historical_slugs]}")
# Check pghistory records
event_model = getattr(Park, 'event_model', None)
event_model = getattr(Park, "event_model", None)
if event_model:
historical_records = event_model.objects.filter(
pgh_obj_id=park.id
).order_by('-pgh_created_at')
).order_by("-pgh_created_at")
print(f"\nPG History records:")
for record in historical_records:
print(f"- Event ID: {record.pgh_id}")
@@ -104,56 +100,57 @@ class ParkModelTests(TestCase):
print(f" Created At: {record.pgh_created_at}")
else:
print("\nNo pghistory event model available")
# Try to find by old slug
found_park, is_historical = Park.get_by_slug(original_slug)
self.assertEqual(found_park.id, park.id)
print(f"Found park by old slug: {found_park.slug}, is_historical: {is_historical}")
print(
f"Found park by old slug: {
found_park.slug}, is_historical: {is_historical}"
)
self.assertTrue(is_historical)
# Try current slug
found_park, is_historical = Park.get_by_slug(new_slug)
self.assertEqual(found_park.id, park.id)
print(f"Found park by new slug: {found_park.slug}, is_historical: {is_historical}")
print(
f"Found park by new slug: {
found_park.slug}, is_historical: {is_historical}"
)
self.assertFalse(is_historical)
def test_status_color_mapping(self):
"""Test status color class mapping"""
status_tests = {
'OPERATING': 'bg-green-100 text-green-800',
'CLOSED_TEMP': 'bg-yellow-100 text-yellow-800',
'CLOSED_PERM': 'bg-red-100 text-red-800',
'UNDER_CONSTRUCTION': 'bg-blue-100 text-blue-800',
'DEMOLISHED': 'bg-gray-100 text-gray-800',
'RELOCATED': 'bg-purple-100 text-purple-800'
"OPERATING": "bg-green-100 text-green-800",
"CLOSED_TEMP": "bg-yellow-100 text-yellow-800",
"CLOSED_PERM": "bg-red-100 text-red-800",
"UNDER_CONSTRUCTION": "bg-blue-100 text-blue-800",
"DEMOLISHED": "bg-gray-100 text-gray-800",
"RELOCATED": "bg-purple-100 text-purple-800",
}
for status, expected_color in status_tests.items():
self.park.status = status
self.assertEqual(self.park.get_status_color(), expected_color)
def test_absolute_url(self):
"""Test get_absolute_url method"""
expected_url = f"/parks/{self.park.slug}/"
self.assertEqual(self.park.get_absolute_url(), expected_url)
class ParkAreaModelTests(TestCase):
def setUp(self):
"""Set up test data"""
self.operator = Company.objects.create(
name="Test Company 2",
slug="test-company-2"
name="Test Company 2", slug="test-company-2"
)
self.park = Park.objects.create(
name="Test Park",
status="OPERATING",
operator=self.operator
name="Test Park", status="OPERATING", operator=self.operator
)
self.area = ParkArea.objects.create(
park=self.park,
name="Test Area",
description="A test area"
park=self.park, name="Test Area", description="A test area"
)
def test_area_creation(self):
@@ -162,23 +159,18 @@ class ParkAreaModelTests(TestCase):
self.assertEqual(self.area.slug, "test-area")
self.assertEqual(self.area.park, self.park)
def test_unique_together_constraint(self):
"""Test unique_together constraint for park and slug"""
from django.db import transaction
# Try to create area with same slug in same park
with transaction.atomic():
with self.assertRaises(IntegrityError):
ParkArea.objects.create(
park=self.park,
name="Test Area" # Will generate same slug
park=self.park, name="Test Area" # Will generate same slug
)
# Should be able to use same name in different park
other_park = Park.objects.create(name="Other Park", operator=self.operator)
area = ParkArea.objects.create(
park=other_park,
name="Test Area"
)
area = ParkArea.objects.create(park=other_park, name="Test Area")
self.assertEqual(area.slug, "test-area")