mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 11:51:10 -05:00
Add comprehensive tests for Parks API and models
- Implemented extensive test cases for the Parks API, covering endpoints for listing, retrieving, creating, updating, and deleting parks. - Added tests for filtering, searching, and ordering parks in the API. - Created tests for error handling in the API, including malformed JSON and unsupported methods. - Developed model tests for Park, ParkArea, Company, and ParkReview models, ensuring validation and constraints are enforced. - Introduced utility mixins for API and model testing to streamline assertions and enhance test readability. - Included integration tests to validate complete workflows involving park creation, retrieval, updating, and deletion.
This commit is contained in:
@@ -17,6 +17,11 @@ if TYPE_CHECKING:
|
||||
|
||||
@pghistory.track()
|
||||
class Park(TrackedModel):
|
||||
|
||||
# Import managers
|
||||
from ..managers import ParkManager
|
||||
|
||||
objects = ParkManager()
|
||||
id: int # Type hint for Django's automatic id field
|
||||
STATUS_CHOICES = [
|
||||
("OPERATING", "Operating"),
|
||||
@@ -81,6 +86,43 @@ class Park(TrackedModel):
|
||||
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
constraints = [
|
||||
# Business rule: Closing date must be after opening date
|
||||
models.CheckConstraint(
|
||||
name="park_closing_after_opening",
|
||||
check=models.Q(closing_date__isnull=True) | models.Q(opening_date__isnull=True) | models.Q(closing_date__gte=models.F("opening_date")),
|
||||
violation_error_message="Closing date must be after opening date"
|
||||
),
|
||||
# Business rule: Size must be positive
|
||||
models.CheckConstraint(
|
||||
name="park_size_positive",
|
||||
check=models.Q(size_acres__isnull=True) | models.Q(size_acres__gt=0),
|
||||
violation_error_message="Park size must be positive"
|
||||
),
|
||||
# Business rule: Rating must be between 1 and 10
|
||||
models.CheckConstraint(
|
||||
name="park_rating_range",
|
||||
check=models.Q(average_rating__isnull=True) | (models.Q(average_rating__gte=1) & models.Q(average_rating__lte=10)),
|
||||
violation_error_message="Average rating must be between 1 and 10"
|
||||
),
|
||||
# Business rule: Counts must be non-negative
|
||||
models.CheckConstraint(
|
||||
name="park_ride_count_non_negative",
|
||||
check=models.Q(ride_count__isnull=True) | models.Q(ride_count__gte=0),
|
||||
violation_error_message="Ride count must be non-negative"
|
||||
),
|
||||
models.CheckConstraint(
|
||||
name="park_coaster_count_non_negative",
|
||||
check=models.Q(coaster_count__isnull=True) | models.Q(coaster_count__gte=0),
|
||||
violation_error_message="Coaster count must be non-negative"
|
||||
),
|
||||
# Business rule: Coaster count cannot exceed ride count
|
||||
models.CheckConstraint(
|
||||
name="park_coaster_count_lte_ride_count",
|
||||
check=models.Q(coaster_count__isnull=True) | models.Q(ride_count__isnull=True) | models.Q(coaster_count__lte=models.F("ride_count")),
|
||||
violation_error_message="Coaster count cannot exceed total ride count"
|
||||
),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
Reference in New Issue
Block a user