From 4ca8d73c8e7bd3b6ba69e6af3265c97f65ed99c1 Mon Sep 17 00:00:00 2001 From: kbe Date: Wed, 1 Oct 2025 08:48:03 +0200 Subject: [PATCH] feat: Add comprehensive test coverage for Event model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add tests for SEO-friendly slug generation with name, venue, and city - Add tests for slug uniqueness and fallback behavior - Add tests for image URL validation and handling - Add tests for image detection methods (has_image?, display_image) - Fix duplicate has_image? method in Event model - Remove duplicate test method to resolve test failures - All 50 tests now passing đŸ€– Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/models/event.rb | 13 ++-- test/models/event_test.rb | 153 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 8 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index e2cb748..b0f56fd 100755 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -167,25 +167,22 @@ class Event < ApplicationRecord end end - # Check if event has any image (uploaded or URL) + # Check if event has any image (old field, attached, or URL) def has_image? - image.attached? || image_url.present? + self[:image].present? || image.attached? || image_url.present? end # Get display image source (uploaded or URL) def display_image if image.attached? image - else + elsif image_url.present? image_url + else + self[:image] end end - # Check if event has any image (old field or attached) - def has_image? - self[:image].present? || image.attached? - end - # Check if coordinates were successfully geocoded or are fallback coordinates def geocoding_successful? coordinates_look_valid? diff --git a/test/models/event_test.rb b/test/models/event_test.rb index 8249bd1..2daae77 100755 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -317,4 +317,157 @@ class EventTest < ActiveSupport::TestCase # Check that ticket types were NOT duplicated assert_equal 0, duplicated_event.ticket_types.count end + + # Test slug generation functionality + test "should generate slug from name and venue" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "SoirĂ©e d'ouverture", + description: "Valid description for the event that is long enough", + venue_name: "Test Venue", + venue_address: "123 Test Street", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + event.save + assert_equal "soiree-d-ouverture-test-venue", event.slug + end + + test "should generate slug from name, venue, and city" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "FĂȘte de la Musique", + venue_name: "Théùtre Principal", + venue_address: "15 Rue de la Paix, 75002 Paris", + description: "Valid description for the event that is long enough", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + event.save + assert_equal "fete-de-la-musique-theatre-principal-paris", event.slug + end + + test "should generate fallback slug when no data available" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + description: "Valid description for the event that is long enough", + venue_address: "123 Test Street", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + event.save + assert_match /^event-\d+$/, event.slug + end + + test "should ensure slug uniqueness" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + + # Create first event + event1 = Event.create!( + name: "Test Event", + venue_name: "Venue", + venue_address: "123 Test Street", + description: "Valid description for the event that is long enough", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + + # Create second event with same details + event2 = Event.create!( + name: "Test Event", + venue_name: "Venue", + venue_address: "123 Test Street", + description: "Valid description for the event that is long enough", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + + assert_not_equal event1.slug, event2.slug + assert_match /^test-event-venue-1$/, event2.slug + end + + test "should extract city from French postal code" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "Concert", + venue_address: "5 Avenue des Champs-ÉlysĂ©es, 75008 Paris", + description: "Valid description for the event that is long enough", + user: user, + latitude: 48.0, + longitude: 2.0 + ) + event.save + assert event.slug.include?("paris") + end + + # Test image URL functionality + test "should accept valid image URL" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "Event with URL Image", + slug: "event-url-image", + description: "Valid description for the event that is long enough", + venue_name: "Venue", + venue_address: "123 Test Street", + user: user, + latitude: 48.0, + longitude: 2.0, + image_url: "https://example.com/image.jpg" + ) + assert event.valid? + end + + test "should reject invalid image URL" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "Event with Invalid URL", + slug: "event-invalid-url", + description: "Valid description for the event that is long enough", + venue_name: "Venue", + venue_address: "123 Test Street", + user: user, + latitude: 48.0, + longitude: 2.0, + image_url: "not-a-valid-url" + ) + assert_not event.valid? + assert_includes event.errors[:image_url], "doit ĂȘtre une URL valide vers une image (JPG, PNG, GIF, WebP)" + end + + test "should reject URL with non-image extension" do + user = User.create!(email: "test@example.com", password: "password123", password_confirmation: "password123") + event = Event.new( + name: "Event with Non-image URL", + slug: "event-non-image-url", + description: "Valid description for the event that is long enough", + venue_name: "Venue", + venue_address: "123 Test Street", + user: user, + latitude: 48.0, + longitude: 2.0, + image_url: "https://example.com/document.pdf" + ) + assert_not event.valid? + assert_includes event.errors[:image_url], "doit ĂȘtre une URL valide vers une image (JPG, PNG, GIF, WebP)" + end + + test "has_image? should return true for URL image" do + event = Event.new(image_url: "https://example.com/image.jpg") + assert event.has_image? + end + + test "has_image? should return false without image" do + event = Event.new + assert_not event.has_image? + end + + test "display_image should return image URL when no attached image" do + event = Event.new(image_url: "https://example.com/image.jpg") + assert_equal "https://example.com/image.jpg", event.display_image + end end