2010-06-21 21:33:46 +02:00
|
|
|
require File.expand_path("../../spec_helper", __FILE__)
|
|
|
|
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'article')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'course')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
|
|
|
require File.join(FIXTURE_PATH, 'base')
|
|
|
|
|
|
|
|
# TODO Move validations from other specs to here
|
|
|
|
|
|
|
|
describe "Validations" do
|
|
|
|
|
|
|
|
describe "Uniqueness" do
|
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
context "basic" do
|
|
|
|
before(:all) do
|
|
|
|
@objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)}
|
|
|
|
end
|
2011-05-20 12:21:42 +02:00
|
|
|
|
|
|
|
it "should create a new view if none defined before performing" do
|
|
|
|
WithUniqueValidation.has_view?(:by_title).should be_true
|
|
|
|
end
|
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should validate a new unique document" do
|
|
|
|
@obj = WithUniqueValidation.create(:title => 'title 4')
|
|
|
|
@obj.new?.should_not be_true
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
2010-06-21 21:33:46 +02:00
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should not validate a non-unique document" do
|
|
|
|
@obj = WithUniqueValidation.create(:title => 'title 1')
|
|
|
|
@obj.should_not be_valid
|
|
|
|
@obj.errors[:title].should == ["has already been taken"]
|
|
|
|
end
|
2010-06-21 21:33:46 +02:00
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should save already created document" do
|
|
|
|
@obj = @objs.first
|
|
|
|
@obj.save.should_not be_false
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
2010-06-21 21:33:46 +02:00
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should allow own view to be specified" do
|
|
|
|
# validates_uniqueness_of :code, :view => 'all'
|
|
|
|
WithUniqueValidationView.create(:title => 'title 1', :code => '1234')
|
|
|
|
@obj = WithUniqueValidationView.new(:title => 'title 5', :code => '1234')
|
|
|
|
@obj.should_not be_valid
|
|
|
|
end
|
2010-06-21 23:12:15 +02:00
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should raise an error if specified view does not exist" do
|
|
|
|
WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar'
|
|
|
|
@obj = WithUniqueValidationView.new(:title => 'title 2', :code => '12345')
|
|
|
|
lambda {
|
|
|
|
@obj.valid?
|
|
|
|
}.should raise_error
|
|
|
|
end
|
2010-06-21 23:12:15 +02:00
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
it "should not try to create a defined view" do
|
|
|
|
WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar'
|
|
|
|
WithUniqueValidationView.has_view?('fooobar').should be_false
|
|
|
|
WithUniqueValidationView.has_view?('by_title').should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
it "should not try to create new view when already defined" do
|
2010-06-21 22:37:13 +02:00
|
|
|
@obj = @objs[1]
|
|
|
|
@obj.class.should_not_receive('view_by')
|
|
|
|
@obj.class.should_receive('has_view?').and_return(true)
|
|
|
|
@obj.class.should_receive('view').and_return({'rows' => [ ]})
|
|
|
|
@obj.valid?
|
|
|
|
end
|
|
|
|
end
|
2011-04-08 14:36:29 +02:00
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
context "with a proxy parameter" do
|
2011-05-20 12:21:42 +02:00
|
|
|
|
|
|
|
it "should create a new view despite proxy" do
|
|
|
|
WithUniqueValidationProxy.has_view?(:by_title).should be_true
|
|
|
|
end
|
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
it "should be used" do
|
2010-06-21 23:12:15 +02:00
|
|
|
@obj = WithUniqueValidationProxy.new(:title => 'test 6')
|
2010-06-21 21:33:46 +02:00
|
|
|
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
|
|
|
@obj.valid?.should be_true
|
|
|
|
end
|
2010-06-22 14:15:30 +02:00
|
|
|
|
|
|
|
it "should allow specific view" do
|
|
|
|
@obj = WithUniqueValidationProxy.new(:title => 'test 7')
|
|
|
|
@obj.class.should_not_receive('view_by')
|
|
|
|
proxy = mock('Proxy')
|
|
|
|
@obj.should_receive('proxy').and_return(proxy)
|
|
|
|
proxy.should_receive('has_view?').and_return(true)
|
|
|
|
proxy.should_receive('view').and_return({'rows' => [ ]})
|
|
|
|
@obj.valid?
|
|
|
|
end
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
context "when proxied" do
|
|
|
|
it "should lookup the model_proxy" do
|
|
|
|
mp = mock(:ModelProxy)
|
|
|
|
mp.should_receive(:view).and_return({'rows' => []})
|
|
|
|
@obj = WithUniqueValidation.new(:title => 'test 8')
|
|
|
|
@obj.stub!(:model_proxy).twice.and_return(mp)
|
|
|
|
@obj.valid?
|
|
|
|
end
|
2011-04-08 14:36:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a scope" do
|
|
|
|
before(:all) do
|
|
|
|
@objs = [['title 1', 1], ['title 2', 1], ['title 3', 1]].map{|t| WithScopedUniqueValidation.create(:title => t[0], :parent_id => t[1])}
|
|
|
|
@objs_nil = [['title 1', nil], ['title 2', nil], ['title 3', nil]].map{|t| WithScopedUniqueValidation.create(:title => t[0], :parent_id => t[1])}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create the view" do
|
|
|
|
@objs.first.class.has_view?('by_parent_id_and_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate unique document" do
|
|
|
|
@obj = WithScopedUniqueValidation.create(:title => 'title 4', :parent_id => 1)
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate unique document outside of scope" do
|
|
|
|
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => 2)
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate non-unique document" do
|
|
|
|
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => 1)
|
|
|
|
@obj.should_not be_valid
|
|
|
|
@obj.errors[:title].should == ["has already been taken"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate unique document will nil scope" do
|
|
|
|
@obj = WithScopedUniqueValidation.create(:title => 'title 4', :parent_id => nil)
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate non-unique document with nil scope" do
|
|
|
|
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => nil)
|
|
|
|
@obj.should_not be_valid
|
|
|
|
@obj.errors[:title].should == ["has already been taken"]
|
|
|
|
end
|
2011-04-05 20:41:24 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|