Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
Improve coverage for candidates.
  • Loading branch information
Adrian Mugnolo committed Jul 2, 2014
1 parent fdbe416 commit 51f4c09
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
117 changes: 117 additions & 0 deletions test/candidates_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require "helper"

class CandidatesTest < MiniTest::Unit::TestCase

include FriendlyId::Test

class City < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
alias_attribute :slug_candidates, :name
end

def model_class
City
end

def with_instances_of(klass = model_class, &block)
transaction do
city1 = klass.create! :name => "New York", :code => "JFK"
city2 = klass.create! :name => "New York", :code => "EWR"
yield city1, city2
end
end
alias_method :with_instances, :with_instances_of

test "resolves conflict with candidate" do
with_instances do |city1, city2|
assert_equal "new-york", city1.slug
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city2.slug)
end
end

test "accepts candidate as symbol" do
klass = Class.new model_class do
def slug_candidates
:name
end
end
with_instances_of klass do |_, city|
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
end
end

test "accepts multiple candidates" do
klass = Class.new model_class do
def slug_candidates
[name, code]
end
end
with_instances_of klass do |_, city|
assert_equal "ewr", city.slug
end
end

test "ignores blank candidate" do
klass = Class.new model_class do
def slug_candidates
[name, ""]
end
end
with_instances_of klass do |_, city|
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
end
end

test "ignores nil candidate" do
klass = Class.new model_class do
def slug_candidates
[name, nil]
end
end
with_instances_of klass do |_, city|
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
end
end

test "accepts candidate with nested array" do
klass = Class.new model_class do
def slug_candidates
[name, [name, code]]
end
end
with_instances_of klass do |_, city|
assert_equal "new-york-ewr", city.slug
end
end

test "accepts candidate with lambda" do
klass = Class.new City do
def slug_candidates
[name, [name, ->{ rand 1000 }]]
end
end
with_instances_of klass do |_, city|
assert_match(/\Anew-york-\d{,3}\z/, city.friendly_id)
end
end

test "accepts candidate with object" do
klass = Class.new City do
class Airport
def initialize(code)
@code = code
end
attr_reader :code
alias_method :to_s, :code
end
def slug_candidates
[name, [name, Airport.new(code)]]
end
end
with_instances_of klass do |_, city|
assert_equal "new-york-ewr", city.friendly_id
end
end

end
7 changes: 5 additions & 2 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ def up
# Used to test :scoped and :history together
add_column :restaurants, :city_id, :integer

# Used to test candidates
add_column :cities, :code, :string, :limit => 3

@done = true
end

private

def slugged_tables
%w[journalists articles novelists novels manuals]
%w[journalists articles novelists novels manuals cities]
end

def tables_with_uuid_primary_key
Expand All @@ -82,7 +85,7 @@ def scoped_tables
end

def simple_tables
%w[authors books publishers cities]
%w[authors books publishers]
end

def tables
Expand Down
1 change: 0 additions & 1 deletion test/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,3 @@ module Core
end
end
end

0 comments on commit 51f4c09

Please # to comment.