develop #3
@@ -7,7 +7,7 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Test validations
|
# Test validations
|
||||||
test "should not save ticket without qr_code" do
|
test "should automatically generate qr_code if not provided" do
|
||||||
user = User.create!(
|
user = User.create!(
|
||||||
email: "test@example.com",
|
email: "test@example.com",
|
||||||
password: "password123",
|
password: "password123",
|
||||||
@@ -36,8 +36,18 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
event: event
|
event: event
|
||||||
)
|
)
|
||||||
|
|
||||||
ticket = Ticket.new(user: user, ticket_type: ticket_type)
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
||||||
assert_not ticket.save
|
ticket = Ticket.new(order: order, ticket_type: ticket_type, first_name: "Test", last_name: "User")
|
||||||
|
|
||||||
|
# QR code should be nil initially
|
||||||
|
assert_nil ticket.qr_code
|
||||||
|
|
||||||
|
# After validation, QR code should be generated automatically
|
||||||
|
ticket.valid?
|
||||||
|
assert_not_nil ticket.qr_code
|
||||||
|
|
||||||
|
# And the ticket should save successfully
|
||||||
|
assert ticket.save
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not save ticket with duplicate qr_code" do
|
test "should not save ticket with duplicate qr_code" do
|
||||||
@@ -50,36 +60,128 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
assert_not ticket2.save
|
assert_not ticket2.save
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not save ticket without user_id" do
|
test "should not save ticket without order" do
|
||||||
ticket = Ticket.new(qr_code: "unique_qr_code_123")
|
ticket = Ticket.new(qr_code: "unique_qr_code_123")
|
||||||
assert_not ticket.save
|
assert_not ticket.save
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not save ticket without ticket_type_id" do
|
test "should not save ticket without ticket_type_id" do
|
||||||
ticket = Ticket.new(qr_code: "unique_qr_code_123", user_id: 1)
|
user = User.create!(
|
||||||
|
email: "test@example.com",
|
||||||
|
password: "password123",
|
||||||
|
password_confirmation: "password123"
|
||||||
|
)
|
||||||
|
|
||||||
|
event = Event.create!(
|
||||||
|
name: "Valid event Name",
|
||||||
|
slug: "valid-event-name",
|
||||||
|
description: "Valid description for the event that is long enough",
|
||||||
|
latitude: 48.8566,
|
||||||
|
longitude: 2.3522,
|
||||||
|
venue_name: "Test Venue",
|
||||||
|
venue_address: "123 Test Street",
|
||||||
|
user: user
|
||||||
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
||||||
|
ticket = Ticket.new(qr_code: "unique_qr_code_123", order: order)
|
||||||
assert_not ticket.save
|
assert_not ticket.save
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not save ticket without price_cents" do
|
test "should set price from ticket type automatically" do
|
||||||
ticket = Ticket.new(qr_code: "unique_qr_code_123", user_id: 1, ticket_type_id: 1)
|
user = User.create!(
|
||||||
assert_not ticket.save
|
email: "test2@example.com",
|
||||||
|
password: "password123",
|
||||||
|
password_confirmation: "password123"
|
||||||
|
)
|
||||||
|
|
||||||
|
event = Event.create!(
|
||||||
|
name: "Valid event Name",
|
||||||
|
slug: "valid-event-name-2",
|
||||||
|
description: "Valid description for the event that is long enough",
|
||||||
|
latitude: 48.8566,
|
||||||
|
longitude: 2.3522,
|
||||||
|
venue_name: "Test Venue",
|
||||||
|
venue_address: "123 Test Street",
|
||||||
|
user: user
|
||||||
|
)
|
||||||
|
|
||||||
|
ticket_type = TicketType.create!(
|
||||||
|
name: "Valid Ticket Type Name",
|
||||||
|
description: "Valid description for the ticket type that is long enough",
|
||||||
|
price_cents: 1000,
|
||||||
|
quantity: 50,
|
||||||
|
sale_start_at: Time.current,
|
||||||
|
sale_end_at: Time.current + 1.day,
|
||||||
|
requires_id: false,
|
||||||
|
event: event
|
||||||
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
||||||
|
ticket = Ticket.new(
|
||||||
|
qr_code: "unique_qr_code_123",
|
||||||
|
order: order,
|
||||||
|
ticket_type: ticket_type,
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Doe"
|
||||||
|
)
|
||||||
|
|
||||||
|
# price_cents should be nil initially
|
||||||
|
assert_nil ticket.price_cents
|
||||||
|
|
||||||
|
# After validation, it should be set from ticket_type
|
||||||
|
ticket.valid?
|
||||||
|
assert_equal 1000, ticket.price_cents
|
||||||
|
assert ticket.save
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not save ticket with invalid status" do
|
test "should not save ticket with invalid status" do
|
||||||
|
user = User.create!(
|
||||||
|
email: "test3@example.com",
|
||||||
|
password: "password123",
|
||||||
|
password_confirmation: "password123"
|
||||||
|
)
|
||||||
|
|
||||||
|
event = Event.create!(
|
||||||
|
name: "Valid event Name",
|
||||||
|
slug: "valid-event-name-3",
|
||||||
|
description: "Valid description for the event that is long enough",
|
||||||
|
latitude: 48.8566,
|
||||||
|
longitude: 2.3522,
|
||||||
|
venue_name: "Test Venue",
|
||||||
|
venue_address: "123 Test Street",
|
||||||
|
user: user
|
||||||
|
)
|
||||||
|
|
||||||
|
ticket_type = TicketType.create!(
|
||||||
|
name: "Valid Ticket Type Name",
|
||||||
|
description: "Valid description for the ticket type that is long enough",
|
||||||
|
price_cents: 1000,
|
||||||
|
quantity: 50,
|
||||||
|
sale_start_at: Time.current,
|
||||||
|
sale_end_at: Time.current + 1.day,
|
||||||
|
requires_id: false,
|
||||||
|
event: event
|
||||||
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: 1000)
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
qr_code: "unique_qr_code_123",
|
qr_code: "unique_qr_code_123",
|
||||||
user_id: 1,
|
order: order,
|
||||||
ticket_type_id: 1,
|
ticket_type: ticket_type,
|
||||||
price_cents: 1000,
|
price_cents: 1000,
|
||||||
status: "invalid_status"
|
status: "invalid_status",
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Doe"
|
||||||
)
|
)
|
||||||
assert_not ticket.save
|
assert_not ticket.save
|
||||||
end
|
end
|
||||||
|
|
||||||
# Test associations
|
# Test associations
|
||||||
test "should belong to user" do
|
test "should have one user through order" do
|
||||||
association = Ticket.reflect_on_association(:user)
|
association = Ticket.reflect_on_association(:user)
|
||||||
assert_equal :belongs_to, association.macro
|
assert_equal :has_one, association.macro
|
||||||
|
assert_equal :order, association.options[:through]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should belong to ticket_type" do
|
test "should belong to ticket_type" do
|
||||||
@@ -130,11 +232,14 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
event: event
|
event: event
|
||||||
)
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
qr_code: "unique_qr_code_123",
|
qr_code: "unique_qr_code_123",
|
||||||
user: user,
|
order: order,
|
||||||
ticket_type: ticket_type,
|
ticket_type: ticket_type,
|
||||||
status: "active"
|
status: "active",
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Doe"
|
||||||
)
|
)
|
||||||
# The price_cents should be set automatically by the callback
|
# The price_cents should be set automatically by the callback
|
||||||
assert ticket.save
|
assert ticket.save
|
||||||
@@ -169,11 +274,14 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
event: event
|
event: event
|
||||||
)
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
qr_code: "unique_qr_code_456",
|
qr_code: "unique_qr_code_456",
|
||||||
user: user,
|
order: order,
|
||||||
ticket_type: ticket_type,
|
ticket_type: ticket_type,
|
||||||
status: "used"
|
status: "used",
|
||||||
|
first_name: "Jane",
|
||||||
|
last_name: "Doe"
|
||||||
)
|
)
|
||||||
assert ticket.save
|
assert ticket.save
|
||||||
end
|
end
|
||||||
@@ -207,11 +315,14 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
event: event
|
event: event
|
||||||
)
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
qr_code: "unique_qr_code_789",
|
qr_code: "unique_qr_code_789",
|
||||||
user: user,
|
order: order,
|
||||||
ticket_type: ticket_type,
|
ticket_type: ticket_type,
|
||||||
status: "expired"
|
status: "expired",
|
||||||
|
first_name: "Bob",
|
||||||
|
last_name: "Smith"
|
||||||
)
|
)
|
||||||
assert ticket.save
|
assert ticket.save
|
||||||
end
|
end
|
||||||
@@ -245,11 +356,14 @@ class TicketTest < ActiveSupport::TestCase
|
|||||||
event: event
|
event: event
|
||||||
)
|
)
|
||||||
|
|
||||||
|
order = Order.create!(user: user, event: event, total_amount_cents: ticket_type.price_cents)
|
||||||
ticket = Ticket.new(
|
ticket = Ticket.new(
|
||||||
qr_code: "unique_qr_code_999",
|
qr_code: "unique_qr_code_999",
|
||||||
user: user,
|
order: order,
|
||||||
ticket_type: ticket_type,
|
ticket_type: ticket_type,
|
||||||
status: "refunded"
|
status: "refunded",
|
||||||
|
first_name: "Alice",
|
||||||
|
last_name: "Johnson"
|
||||||
)
|
)
|
||||||
assert ticket.save
|
assert ticket.save
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user