Skip to content

Commit

Permalink
Merge 1f611b0 into 4d222cc
Browse files Browse the repository at this point in the history
  • Loading branch information
SushieeK committed Nov 10, 2023
2 parents 4d222cc + 1f611b0 commit 02904c2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
48 changes: 47 additions & 1 deletion events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,42 @@ def test_update_event_view_post(self):
Event.objects.get(pk=self.event.id).event_name, "Updated Event"
) # Verify data was updated

def test_update_event_capacity_too_low(self):
self.client.login(username="testuser", password="testpassword")

# Set up approved participants
for i in range(10): # Assuming 10 participants have joined
user = User.objects.create_user(
username=f"user{i}", password="testpassword"
)
EventJoin.objects.create(user=user, event=self.event, status=APPROVED)

# Prepare update data with a capacity lower than the number of approved participants
updated_data = {
"event_name": "Updated Event",
"start_time": self.event.start_time,
"end_time": self.event.end_time,
"capacity": 5, # Less than the 10 approved participants
"event_location_id": self.location.id,
}

# Perform the update
url = reverse("events:update-event", args=(self.event.id,))
response = self.client.post(url, updated_data, follow=True)
# Assert the response status code if you expect a 200 from a template render or a 302 from a redirect
self.assertEqual(response.status_code, 400)

# Check for error message in response
error_message = (
"Capacity cannot be less than the number of approved participants"
)
self.assertEqual(response["Content-Type"], "application/json")
content = json.loads(response.content)
self.assertEqual(
content["capacity"],
error_message,
) # Replace with expected content


class DuplicateEventTests(TestCase):
def setUp(self):
Expand All @@ -183,12 +219,22 @@ def setUp(self):
is_active=True,
creator=self.user,
)
self.event2 = Event.objects.create(
event_name="Test Event2",
event_location=self.location,
start_time=self.start_time,
end_time=self.end_time,
capacity=100,
is_active=True,
creator=self.user,
)

def test_update_event_with_duplicate_data(self):
self.client.login(username="testuser", password="testpassword")
existing_event = Event.objects.get(pk=self.event.id)
existing_event2 = Event.objects.get(pk=self.event2.id)
response = self.client.post(
reverse("events:update-event", args=[existing_event.id]),
reverse("events:update-event", args=[existing_event2.id]),
{
"event_location_id": self.location.id,
"event_name": existing_event.event_name,
Expand Down
31 changes: 22 additions & 9 deletions events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,18 @@ def updateEvent(request, event_id):
errors["capacity"] = "Capacity is required."
else:
try:
capacity = int(capacity)
if capacity < 0:
new_capacity = int(capacity)
if new_capacity < 0:
errors["capacity"] = "Capacity must be a non-negative number."
else:
# New capacity check against approved participants
approved_participants_count = EventJoin.objects.filter(
event=event, status=APPROVED
).count()
if new_capacity < approved_participants_count:
errors[
"capacity"
] = "Capacity cannot be less than the number of approved participants"
except ValueError:
errors["capacity"] = "Capacity must be a valid number."

Expand All @@ -173,13 +182,17 @@ def updateEvent(request, event_id):
errors["event_location_id"] = "Event location must be selected."
else:
location_object = Location.objects.get(id=event_location_id)
if Event.objects.filter(
event_name=event_name,
event_location=location_object,
start_time=start_time,
end_time=end_time,
is_active=True,
).exists():
if (
Event.objects.filter(
event_name=event_name,
event_location=location_object,
start_time=start_time,
end_time=end_time,
is_active=True,
)
.exclude(pk=event_id)
.exists()
):
errors[
"similar_event_error"
] = "An event with these details already exists."
Expand Down

0 comments on commit 02904c2

Please sign in to comment.