Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
wrote some tests
  • Loading branch information
brendan6 committed Oct 14, 2011
1 parent de7111a commit 815d982
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion spec/has_unique_slug_spec.rb
@@ -1,5 +1,104 @@
require 'spec_helper'

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")

def setup_db
ActiveRecord::Schema.define(:version => 1) do
create_table :standards do |t|
t.column :title, :string
t.column :slug, :string
end

create_table :customs do |t|
t.column :name, :string
t.column :permalink, :string
end
end
end

def teardown_db
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.drop_table t
end
end

class Standard < ActiveRecord::Base
has_unique_slug

def self.table_name
"standards"
end
end

class Custom < ActiveRecord::Base
has_unique_slug :permalink, :name

def self.table_name
"customs"
end
end

class Custom2 < ActiveRecord::Base
has_unique_slug {|record| "zcvf #{record.title} zxvf"}

def self.table_name
"standards"
end
end

describe HasUniqueSlug do
pending "Write test simulating active record"

before(:all) do
setup_db
end

after(:all) do
teardown_db
end

it "creates a unique slug" do
r = Standard.create! :title => "Sample Record"
r.slug.should == "sample-record"
end

it "should add incremental column if not unique" do
2.upto 5 do |i|
r = Standard.create! :title => "Sample Record"
r.slug.should == "sample-record-#{i}"
end
end

it "should not increment the slug if the duplicate is itself" do
r = Standard.last
slug = r.slug
r.save.should be_true
r.slug.should == slug
end

it "should update slugs for non-standard implementation" do
r = Custom.create! :name => "Sample Record"
r.permalink.should == "sample-record"
2.upto 5 do |i|
r = Custom.create! :name => "Sample Record"
r.permalink.should == "sample-record-#{i}"
end
r = Custom.last
slug = r.permalink
r.save.should be_true
r.permalink.should == slug
end

it "should update slugs based on the block if a block is provided" do
r = Custom2.create! :title => "Sample Record"
r.slug.should == "zcvf-sample-record-zxvf"
2.upto 5 do |i|
r = Custom2.create! :title => "Sample Record"
r.slug.should == "zcvf-sample-record-zxvf-#{i}"
end
r = Custom2.last
slug = r.slug
r.save.should be_true
r.slug.should == slug
end

end

0 comments on commit 815d982

Please sign in to comment.