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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
require_relative "spec_helper"
describe "throw_failures plugin" do
before do
@c = Class.new(Sequel::Model(:items)) do
plugin :throw_failures
columns :x
set_primary_key :x
unrestrict_primary_key
def before_create
super
cancel_action 'bc' if x == 2
end
def before_destroy
super
cancel_action 'bd' if x == 2
end
def validate
super
errors.add(:x, "3") if x == 3
end
end
DB.reset
end
it "should work normally if no exceptions are thrown/raised" do
o = @c.create(:x=>1)
o.must_be_kind_of @c
o.valid?.must_equal true
o.destroy.must_equal o
end
it "should work normally when not rescuing exceptions internally when calling save" do
@c.new.set(:x => 2).save(:raise_on_failure=>false).must_be_nil
@c.raise_on_save_failure = false
@c.create(:x => 2).must_be_nil
@c.load(:x => 2).destroy(:raise_on_failure=>false).must_be_nil
end
it "should work normally when not rescuing exceptions internally when calling valid?" do
@c.send(:define_method, :before_validation){cancel_action "bv"}
@c.new(:x => 2).valid?.must_equal false
end
it "should raise exceptions if no catch blocks have been setup and set to raise on failure" do
begin
@c.create(:x => 2)
rescue Sequel::HookFailed => e
e.backtrace.wont_be_empty
1
end.must_equal 1
begin
@c.create(:x => 3)
rescue Sequel::ValidationFailed => e
e.backtrace.wont_be_empty
1
end.must_equal 1
end
it "should allow catching exceptions instead of rescuing them" do
e = catch(Sequel::HookFailed){@c.create(:x => 2)}
e.must_be_kind_of Sequel::HookFailed
e.backtrace.must_be_nil
e = catch(Sequel::ValidationFailed){@c.create(:x => 3)}
e.must_be_kind_of Sequel::ValidationFailed
e.backtrace.must_be_nil
e = catch(Sequel::HookFailed){@c.load(:x => 2).destroy}
e.must_be_kind_of Sequel::HookFailed
e.backtrace.must_be_nil
end
end
|