Sha256: 5890076424dde008dc769c01e87a5977c7966d55e4fde49a77e356ba366726bf

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require File.expand_path("../test_helper", __FILE__)

class ModelTest < Test::Unit::TestCase

  include FriendlyId::Test::Generic

  def instance(name = "user")
    @instance ||= klass.create!(:name => name)
  end

  # @TODO - this kind of setup is repeated in a few places and should probably
  # be abstracted.
  def klass
    @@klass ||= make_model("core", :name) do |t|
      t.string  :name,   :unique  => true
      t.boolean :active, :default => true
    end
    @@klass
  end

  def other_class
    @@other_class ||= make_model("core_two", :name) do |t|
      t.string  :name,   :unique  => true
      t.boolean :active, :default => true
    end
  end

  def setup
    klass.stubs(:name).returns("Cores")
    other_class.stubs(:name).returns("CoreTwos")
  end

  test "models don't use friendly_id by default" do
    assert !Class.new(ActiveRecord::Base).uses_friendly_id?
  end

  test "integers should be unfriendly ids" do
    assert 1.unfriendly_id?
  end

  test "ActiveRecord::Base instances should be unfriendly_ids" do
    assert klass.new.unfriendly_id?
  end

  test "numeric strings are neither friendly nor unfriendly" do
    assert_equal nil, "1".friendly_id?
    assert_equal nil, "1".unfriendly_id?
  end

  test "strings with letters are friendly_ids" do
    assert "a".friendly_id?
  end

  test "should raise error when bad config options are set" do
    assert_raises ArgumentError do
      klass.has_friendly_id :smeg, :garbage => :in
    end
  end

  test "reserves 'new' and 'edit' by default" do
    FriendlyId::Configuration::DEFAULTS[:reserved_words].each do |word|
      assert_raises ActiveRecord::RecordInvalid do
        klass.create! :name => word
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_id4-4.0.0.pre test/core_test.rb