Files
aperonight/test/jobs/cleanup_expired_drafts_job_test.rb
2025-09-05 14:38:14 +02:00

137 lines
3.4 KiB
Ruby

require "test_helper"
class CleanupExpiredDraftsJobTest < ActiveJob::TestCase
def setup
@user = User.create!(
email: "test@example.com",
password: "password123",
password_confirmation: "password123"
)
@event = Event.create!(
name: "Test Event",
slug: "test-event",
description: "A valid description for the test event that is long enough",
latitude: 48.8566,
longitude: 2.3522,
venue_name: "Test Venue",
venue_address: "123 Test Street",
user: @user,
start_time: 1.week.from_now,
end_time: 1.week.from_now + 3.hours,
state: :published
)
@ticket_type = TicketType.create!(
name: "General Admission",
description: "General admission tickets with full access to the event",
price_cents: 2500,
quantity: 100,
sale_start_at: Time.current,
sale_end_at: @event.start_time - 1.hour,
requires_id: false,
event: @event
)
@order = Order.create!(
user: @user,
event: @event,
status: "draft",
total_amount_cents: 2500
)
end
test "should be queued on default queue" do
assert_equal "default", CleanupExpiredDraftsJob.queue_name
end
test "should perform job without errors when no tickets exist" do
# Clear all tickets
Ticket.destroy_all
assert_nothing_raised do
CleanupExpiredDraftsJob.perform_now
end
end
test "should handle expired draft tickets" do
# Create an expired draft ticket with expired order
@order.update!(expires_at: 1.hour.ago)
expired_ticket = Ticket.create!(
order: @order,
ticket_type: @ticket_type,
status: "draft",
first_name: "John",
last_name: "Doe"
)
# Job should run without errors
assert_nothing_raised do
CleanupExpiredDraftsJob.perform_now
end
# Basic functional verification
assert_not_nil Ticket.find(expired_ticket.id)
end
test "should handle multiple expired tickets" do
# Create multiple orders with multiple expired tickets
@order.update!(expires_at: 1.hour.ago)
ticket1 = Ticket.create!(
order: @order,
ticket_type: @ticket_type,
status: "draft",
first_name: "John",
last_name: "Doe"
)
ticket2 = Ticket.create!(
order: @order,
ticket_type: @ticket_type,
status: "draft",
first_name: "Jane",
last_name: "Doe"
)
# Job should run without errors
assert_nothing_raised do
CleanupExpiredDraftsJob.perform_now
end
# Verify both tickets still exist (functional test)
assert_not_nil Ticket.find(ticket1.id)
assert_not_nil Ticket.find(ticket2.id)
end
test "should not affect non-expired tickets" do
# Create a non-expired ticket
@order.update!(expires_at: 1.hour.from_now)
ticket = Ticket.create!(
order: @order,
ticket_type: @ticket_type,
status: "draft",
first_name: "John",
last_name: "Doe"
)
# Job should run without errors
assert_nothing_raised do
CleanupExpiredDraftsJob.perform_now
end
# Ticket should remain unchanged
assert_equal "draft", ticket.reload.status
end
test "should handle empty expired tickets list" do
# Ensure no tickets are expired
@order.update!(expires_at: 1.hour.from_now)
# Job should run without errors
assert_nothing_raised do
CleanupExpiredDraftsJob.perform_now
end
end
end