Skip to content

Commit

Permalink
Add test to assert exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
Trung Lê committed Jan 19, 2016
1 parent bac33a0 commit 0e150f3
Showing 1 changed file with 62 additions and 16 deletions.
78 changes: 62 additions & 16 deletions test/model/adapters/sql/command_test.rb
Expand Up @@ -14,61 +14,107 @@
end

describe '#create' do
describe 'when a Sequel::ForeignKeyConstraintViolation is raised' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:insert) do |_|
raise ::Sequel::ForeignKeyConstraintViolation.new('fkey constraint error')
end
exception = -> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('fkey constraint error')
end
end

describe 'when a Sequel::CheckConstraintViolation is raised' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:insert) do |_|
raise ::Sequel::CheckConstraintViolation.new('check constraint error')
end
exception = -> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('check constraint error')
end
end

describe 'when a Sequel::NotNullConstraintViolation is raised' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:insert) do |_|
raise ::Sequel::NotNullConstraintViolation.new('not null constraint error')
end
exception = -> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('not null constraint error')
end
end

describe 'when a Sequel::UniqueConstraintViolation is raised' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:insert) do |_|
raise ::Sequel::UniqueConstraintViolation.new('unique constraint error')
end
exception = -> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('unique constraint error')
end
end

describe 'when a Sequel::DatabaseError is raised' do
it 'raises a lotus error' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:insert) do |_|
raise Sequel::DatabaseError.new
raise ::Sequel::DatabaseError.new('db error')
end
-> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception = -> { @command.create(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('db error')
end
end

describe 'when a different error is raised' do
describe 'when an error that is not inherited from Sequel::DatabaseError is raised' do
it 'bubbles the error up' do
collection.define_singleton_method(:insert) do |_|
raise Sequel::Error.new
raise Sequel::Error.new('constraint error')
end
-> { @command.create(Object.new) }.must_raise(Sequel::Error)
exception = -> { @command.create(Object.new) }.must_raise(Sequel::Error)
exception.message.must_equal('constraint error')
end
end
end

describe '#update' do
describe 'when a Sequel::DatabaseError is raised' do
it 'raises a lotus error' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:update) do |_|
raise Sequel::DatabaseError.new
raise Sequel::DatabaseError.new('db error')
end
-> { @command.update(Object.new) }.must_raise(Lotus::Model::Error)
exception = -> { @command.update(Object.new) }.must_raise(Lotus::Model::Error)
exception.message.must_equal('db error')
end
end

describe 'when a different error is raised' do
it 'bubbles the error up' do
collection.define_singleton_method(:update) do |_|
raise Sequel::Error.new
raise Sequel::Error.new('constraint error')
end
-> { @command.update(Object.new) }.must_raise(Sequel::Error)
exception = -> { @command.update(Object.new) }.must_raise(Sequel::Error)
exception.message.must_equal('constraint error')
end
end
end

describe '#delete' do
describe 'when a Sequel::DatabaseError is raised' do
it 'raises a lotus error' do
it 'raises Lotus::Model::Error exception' do
collection.define_singleton_method(:delete) do
raise Sequel::DatabaseError.new
raise Sequel::DatabaseError.new('db error')
end
-> { @command.delete }.must_raise(Lotus::Model::Error)
exception = -> { @command.delete }.must_raise(Lotus::Model::Error)
exception.message.must_equal('db error')
end
end

describe 'when a different error is raised' do
it 'bubbles the error up' do
collection.define_singleton_method(:delete) do
raise Sequel::Error.new
raise Sequel::Error.new('constraint error')
end
-> { @command.delete }.must_raise(Sequel::Error)
exception = -> { @command.delete }.must_raise(Sequel::Error)
exception.message.must_equal('constraint error')
end
end
end
Expand Down

0 comments on commit 0e150f3

Please sign in to comment.