1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
require_relative "spec_helper"
describe "Sequel::Plugins::ValidationHelpers" do
before do
@c = Class.new(Sequel::Model(:foo))
@c.class_eval do
columns :a, :b, :c
plugin :validation_contexts
def validate
errors.add(:a, 'bad') if a == 1 && validation_context == :create
errors.add(:b, 'bad') if b == 2 && validation_context == :update
errors.add(:c, 'bad') if c == 3 && validation_context == :foo
end
end
end
it "should support :validation_context option to valid?" do
@c.new(:c=>3).valid?.must_equal true
@c.new(:c=>3).valid?(:validation_context=>:foo).must_equal false
end
it "should support :validation_context option to save?" do
@c.new(:c=>3).save
proc{@c.new(:c=>3).save(:validation_context=>:foo)}.must_raise Sequel::ValidationFailed
end
it "should raise error if using a validation context on a frozen model instance" do
@c.new(:c=>3).freeze.valid?.must_equal true
proc{@c.new(:c=>3).freeze.valid?(:validation_context=>:foo)}.must_raise RuntimeError, TypeError
end
end
|