feat(test): Add comprehensive unit tests for all Rails models
- Create detailed unit tests for Party, TicketType, Ticket, User, and ApplicationRecord models - Add fixture files for all models with valid test data - Fix enum syntax in Party model for Rails 8 compatibility - Add 60 total model tests covering validations, associations, and business logic - Ensure all tests pass successfully This provides full test coverage for the application's data models.
This commit is contained in:
137
test/models/party_test.rb
Normal file
137
test/models/party_test.rb
Normal file
@@ -0,0 +1,137 @@
|
||||
require "test_helper"
|
||||
|
||||
class PartyTest < ActiveSupport::TestCase
|
||||
# Test that Party model exists
|
||||
test "should be a class" do
|
||||
assert_kind_of Class, Party
|
||||
end
|
||||
|
||||
# Test validations
|
||||
test "should not save party without name" do
|
||||
party = Party.new(description: "Test party description")
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party without description" do
|
||||
party = Party.new(name: "Test Party")
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party with name less than 3 characters" do
|
||||
party = Party.new(name: "AB", description: "Valid description for the party")
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party with description less than 10 characters" do
|
||||
party = Party.new(name: "Valid Party Name", description: "Too short")
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party without latitude" do
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
longitude: 2.3522
|
||||
)
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party without longitude" do
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
latitude: 48.8566
|
||||
)
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party with invalid latitude" do
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
latitude: 95.0,
|
||||
longitude: 2.3522,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street"
|
||||
)
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should not save party with invalid longitude" do
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
latitude: 48.8566,
|
||||
longitude: 190.0,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street"
|
||||
)
|
||||
assert_not party.save
|
||||
end
|
||||
|
||||
test "should save valid party" do
|
||||
user = User.create!(
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
password_confirmation: "password123"
|
||||
)
|
||||
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
latitude: 48.8566,
|
||||
longitude: 2.3522,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street",
|
||||
user: user
|
||||
)
|
||||
assert party.save
|
||||
end
|
||||
|
||||
# Test enum states
|
||||
test "should have valid states" do
|
||||
assert_equal %w[draft published canceled sold_out], Party.states.keys
|
||||
end
|
||||
|
||||
test "should default to draft state" do
|
||||
party = Party.new(
|
||||
name: "Valid Party Name",
|
||||
description: "Valid description for the party that is long enough",
|
||||
latitude: 48.8566,
|
||||
longitude: 2.3522,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street"
|
||||
)
|
||||
assert_equal "draft", party.state
|
||||
end
|
||||
|
||||
# Test associations
|
||||
test "should belong to user" do
|
||||
association = Party.reflect_on_association(:user)
|
||||
assert_equal :belongs_to, association.macro
|
||||
end
|
||||
|
||||
test "should have many ticket_types" do
|
||||
association = Party.reflect_on_association(:ticket_types)
|
||||
assert_equal :has_many, association.macro
|
||||
end
|
||||
|
||||
test "should have many tickets through ticket_types" do
|
||||
association = Party.reflect_on_association(:tickets)
|
||||
assert_equal :has_many, association.macro
|
||||
assert_equal :ticket_types, association.options[:through]
|
||||
end
|
||||
|
||||
# Test scopes
|
||||
test "should respond to featured scope" do
|
||||
assert_respond_to Party, :featured
|
||||
end
|
||||
|
||||
test "should respond to published scope" do
|
||||
assert_respond_to Party, :published
|
||||
end
|
||||
|
||||
test "should respond to search_by_name scope" do
|
||||
assert_respond_to Party, :search_by_name
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user