-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,71 @@ | ||
require 'test_helper' | ||
|
||
SEQUEL_TO_HANAMI_MAPPING = { | ||
'Sequel::UniqueConstraintViolation' => 'Hanami::Model::UniqueConstraintViolationError', | ||
'Sequel::ForeignKeyConstraintViolation' => 'Hanami::Model::ForeignKeyConstraintViolationError', | ||
'Sequel::NotNullConstraintViolation' => 'Hanami::Model::NotNullConstraintViolationError', | ||
'Sequel::CheckConstraintViolation' => 'Hanami::Model::CheckConstraintViolationError' | ||
} | ||
|
||
describe Hanami::Model::Adapters::Sql::Command do | ||
let(:collection) { Object.new } | ||
let(:query) do | ||
query = Object.new | ||
query.instance_variable_set(:@collection, collection) | ||
query.define_singleton_method(:scoped, -> { @collection }) | ||
query | ||
end | ||
describe 'when #create raises Sequel Database Violation Error' do | ||
SEQUEL_TO_HANAMI_MAPPING.each do |sequel_error, hanami_error| | ||
|
||
before do | ||
@command = Hanami::Model::Adapters::Sql::Command.new(query) | ||
end | ||
it "raises #{hanami_error} instead of #{sequel_error}" do | ||
query = SqlQueryFake.new | ||
command = Hanami::Model::Adapters::Sql::Command.new(query) | ||
|
||
describe '#create' do | ||
describe 'when a Sequel::ForeignKeyConstraintViolation is raised' do | ||
it 'raises Hanami::Model::Error exception' do | ||
collection.define_singleton_method(:insert) do |_| | ||
raise ::Sequel::ForeignKeyConstraintViolation.new('fkey constraint error') | ||
query.scoped.stub(:insert, ->(entity) { raise Object.const_get(sequel_error).new }) do | ||
assert_raises(Object.const_get(hanami_error)) { command.create(Object.new) } | ||
end | ||
exception = -> { @command.create(Object.new) }.must_raise(Hanami::Model::Error) | ||
exception.message.must_equal('fkey constraint error') | ||
end | ||
end | ||
|
||
describe 'when a Sequel::CheckConstraintViolation is raised' do | ||
it 'raises Hanami::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(Hanami::Model::Error) | ||
exception.message.must_equal('check constraint error') | ||
end | ||
end | ||
end | ||
|
||
describe 'when a Sequel::NotNullConstraintViolation is raised' do | ||
it 'raises Hanami::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(Hanami::Model::Error) | ||
exception.message.must_equal('not null constraint error') | ||
end | ||
end | ||
describe 'when #update raises Sequel Database Violation Error' do | ||
SEQUEL_TO_HANAMI_MAPPING.each do |sequel_error, hanami_error| | ||
|
||
describe 'when a Sequel::UniqueConstraintViolation is raised' do | ||
it 'raises Hanami::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(Hanami::Model::Error) | ||
exception.message.must_equal('unique constraint error') | ||
end | ||
end | ||
it "raises #{hanami_error} instead of #{sequel_error}" do | ||
query = SqlQueryFake.new | ||
command = Hanami::Model::Adapters::Sql::Command.new(query) | ||
|
||
describe 'when a Sequel::DatabaseError is raised' do | ||
it 'raises Hanami::Model::Error exception' do | ||
collection.define_singleton_method(:insert) do |_| | ||
raise ::Sequel::DatabaseError.new('db error') | ||
query.scoped.stub(:update, ->(entity) { raise Object.const_get(sequel_error).new }) do | ||
assert_raises(Object.const_get(hanami_error)) { command.update(Object.new) } | ||
end | ||
exception = -> { @command.create(Object.new) }.must_raise(Hanami::Model::Error) | ||
exception.message.must_equal('db error') | ||
end | ||
end | ||
|
||
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('constraint error') | ||
end | ||
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 Hanami::Model::Error exception' do | ||
collection.define_singleton_method(:update) do |_| | ||
raise Sequel::DatabaseError.new('db error') | ||
describe 'when #delete raises Sequel Database Violation Error' do | ||
SEQUEL_TO_HANAMI_MAPPING.each do |sequel_error, hanami_error| | ||
|
||
it "raises #{hanami_error} instead of #{sequel_error}" do | ||
query = SqlQueryFake.new | ||
command = Hanami::Model::Adapters::Sql::Command.new(query) | ||
|
||
query.scoped.stub(:delete, -> { raise Object.const_get(sequel_error).new }) do | ||
assert_raises(Object.const_get(hanami_error)) { command.delete } | ||
end | ||
exception = -> { @command.update(Object.new) }.must_raise(Hanami::Model::Error) | ||
exception.message.must_equal('db error') | ||
end | ||
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('constraint error') | ||
end | ||
exception = -> { @command.update(Object.new) }.must_raise(Sequel::Error) | ||
exception.message.must_equal('constraint error') | ||
end | ||
class SqlQueryFake | ||
def scoped | ||
@collection ||= CollectionFake.new | ||
end | ||
end | ||
|
||
describe '#delete' do | ||
describe 'when a Sequel::DatabaseError is raised' do | ||
it 'raises Hanami::Model::Error exception' do | ||
collection.define_singleton_method(:delete) do | ||
raise Sequel::DatabaseError.new('db error') | ||
end | ||
exception = -> { @command.delete }.must_raise(Hanami::Model::Error) | ||
exception.message.must_equal('db error') | ||
end | ||
class CollectionFake | ||
def insert(entity) | ||
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('constraint error') | ||
end | ||
exception = -> { @command.delete }.must_raise(Sequel::Error) | ||
exception.message.must_equal('constraint error') | ||
end | ||
def update(entity) | ||
end | ||
|
||
def delete | ||
end | ||
end | ||
end |