Back to Repositories

Testing Numeric Slug Generation and Lookup Implementation in friendly_id

This test suite validates the handling of numeric slugs in FriendlyId, ensuring proper generation and lookup functionality for numeric-based friendly URLs. The tests verify core slug behavior when dealing with numeric values as identifiers.

Test Coverage Overview

The test suite provides comprehensive coverage of numeric slug functionality in FriendlyId.

Key areas tested include:
  • Numeric slug generation from numeric names
  • Record lookup using numeric slugs
  • Existence checking with numeric slugs
The suite ensures proper handling of edge cases where model names consist entirely of numbers.

Implementation Analysis

The testing approach uses Minitest’s test case structure with FriendlyId’s test helper modules. The implementation leverages transaction blocks for database isolation and employs FriendlyId’s shared core test functionality.

Key patterns include:
  • Test helper module inclusion for shared functionality
  • Transaction-wrapped test cases
  • Model class abstraction through helper methods

Technical Details

Testing tools and configuration:
  • Minitest as the testing framework
  • FriendlyId::Test module for shared helpers
  • Database transactions for test isolation
  • Article model as the test subject
  • Custom model_class method for flexibility

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

Notable practices include:
  • Isolation of test cases using database transactions
  • Modular test composition through helper inclusion
  • Clear test naming conventions
  • Focused test cases with single assertions
  • Reusable test patterns through shared modules

norman/friendly_id

test/numeric_slug_test.rb

            
require "helper"

class NumericSlugTest < TestCaseClass
  include FriendlyId::Test
  include FriendlyId::Test::Shared::Core

  def model_class
    Article
  end

  test "should generate numeric slugs" do
    transaction do
      record = model_class.create! name: "123"
      assert_equal "123", record.slug
    end
  end

  test "should find by numeric slug" do
    transaction do
      record = model_class.create! name: "123"
      assert_equal model_class.friendly.find("123").id, record.id
    end
  end

  test "should exist? by numeric slug" do
    transaction do
      model_class.create! name: "123"
      assert model_class.friendly.exists?("123")
    end
  end
end