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

@@ -1,70 +1,63 @@
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.contrib.gis.geos import Point
from django.http import HttpResponse
from typing import cast, Optional, Tuple
from .models import Park, ParkArea
from parks.models import Company as Operator
from parks.models.location import ParkLocation
from parks.models import Park, ParkArea, ParkLocation, Company as Operator
User = get_user_model()
def create_test_location(park: Park) -> ParkLocation:
"""Helper function to create a test location"""
park_location = ParkLocation.objects.create(
park=park,
street_address='123 Test St',
city='Test City',
state='TS',
country='Test Country',
postal_code='12345'
street_address="123 Test St",
city="Test City",
state="TS",
country="Test Country",
postal_code="12345",
)
# Set coordinates using the helper method
park_location.set_coordinates(34.0522, -118.2437) # latitude, longitude
park_location.save()
return park_location
class ParkModelTests(TestCase):
@classmethod
def setUpTestData(cls) -> None:
# Create test user
cls.user = User.objects.create_user(
username='testuser',
email='test@example.com',
password='testpass123'
username="testuser",
email="test@example.com",
password="testpass123",
)
# Create test company
cls.operator = Operator.objects.create(
name='Test Company',
website='http://example.com'
name="Test Company", website="http://example.com"
)
# Create test park
cls.park = Park.objects.create(
name='Test Park',
name="Test Park",
operator=cls.operator,
status='OPERATING',
website='http://testpark.com'
status="OPERATING",
website="http://testpark.com",
)
# Create test location
cls.location = create_test_location(cls.park)
def test_park_creation(self) -> None:
"""Test park instance creation and field values"""
self.assertEqual(self.park.name, 'Test Park')
self.assertEqual(self.park.name, "Test Park")
self.assertEqual(self.park.operator, self.operator)
self.assertEqual(self.park.status, 'OPERATING')
self.assertEqual(self.park.website, 'http://testpark.com')
self.assertEqual(self.park.status, "OPERATING")
self.assertEqual(self.park.website, "http://testpark.com")
self.assertTrue(self.park.slug)
def test_park_str_representation(self) -> None:
"""Test string representation of park"""
self.assertEqual(str(self.park), 'Test Park')
self.assertEqual(str(self.park), "Test Park")
def test_park_coordinates(self) -> None:
"""Test park coordinates property"""
@@ -76,60 +69,49 @@ class ParkModelTests(TestCase):
def test_park_formatted_location(self) -> None:
"""Test park formatted_location property"""
expected = '123 Test St, Test City, TS, 12345, Test Country'
expected = "123 Test St, Test City, TS, 12345, Test Country"
self.assertEqual(self.park.formatted_location, expected)
class ParkAreaTests(TestCase):
def setUp(self) -> None:
# Create test company
self.operator = Operator.objects.create(
name='Test Company',
website='http://example.com'
name="Test Company", website="http://example.com"
)
# Create test park
self.park = Park.objects.create(
name='Test Park',
operator=self.operator,
status='OPERATING'
name="Test Park", operator=self.operator, status="OPERATING"
)
# Create test location
self.location = create_test_location(self.park)
# Create test area
self.area = ParkArea.objects.create(
park=self.park,
name='Test Area',
description='Test Description'
park=self.park, name="Test Area", description="Test Description"
)
def test_area_creation(self) -> None:
"""Test park area creation"""
self.assertEqual(self.area.name, 'Test Area')
self.assertEqual(self.area.name, "Test Area")
self.assertEqual(self.area.park, self.park)
self.assertTrue(self.area.slug)
class ParkViewTests(TestCase):
def setUp(self) -> None:
self.client = Client()
self.user = User.objects.create_user(
username='testuser',
email='test@example.com',
password='testpass123'
username="testuser",
email="test@example.com",
password="testpass123",
)
self.operator = Operator.objects.create(
name='Test Company',
website='http://example.com'
name="Test Company", website="http://example.com"
)
self.park = Park.objects.create(
name='Test Park',
operator=self.operator,
status='OPERATING'
name="Test Park", operator=self.operator, status="OPERATING"
)
self.location = create_test_location(self.park)