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
|
|
|
|
|
|
|
|
before(:all) do
|
|
|
|
@objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)}
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should not validate a non-unique document" do
|
|
|
|
@obj = WithUniqueValidation.create(:title => 'title 1')
|
|
|
|
@obj.should_not be_valid
|
2011-02-21 11:41:40 +01:00
|
|
|
@obj.errors[:title].should == ["has already been taken"]
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should save already created document" do
|
|
|
|
@obj = @objs.first
|
|
|
|
@obj.save.should_not be_false
|
|
|
|
@obj.should be_valid
|
|
|
|
end
|
|
|
|
|
2010-06-21 23:12:15 +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
|
|
|
|
|
|
|
|
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 22:37:13 +02:00
|
|
|
context "with a pre-defined view" do
|
2010-06-21 23:12:15 +02:00
|
|
|
it "should not try to create new view" 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
|
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
context "with a proxy parameter" do
|
|
|
|
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
|
|
|
|
|
2010-06-21 22:37:13 +02:00
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|