Skip to content

Commit

Permalink
Merge branch 'fix/t18' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarada87 committed Apr 15, 2021
2 parents 772fe56 + da4686a commit 518e01d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/boxenn/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def find_by_query(query)
end

def save(entity)
attributes = adapt(entity)
save_record(entity.primary_keys_hash, attributes)
attributes = adapt(entity.to_h)
identity = adapt(entity.primary_keys_hash)
save_record(identity, attributes)
end

def destroy(entity)
Expand All @@ -50,8 +51,8 @@ def build(record)
factory.build(record)
end

def adapt(entity)
record_mapper.build(entity)
def adapt(hash)
record_mapper.build(hash)
end
end
end
2 changes: 1 addition & 1 deletion lib/boxenn/use_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(*args)
rescue Dry::Monads::Do::Halt
raise
rescue StandardError => e
Failure.new(e, trace: e.backtrace.first)
Failure.new([e], trace: e.backtrace.first)
end

protected
Expand Down
13 changes: 9 additions & 4 deletions spec/unit/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,24 @@

describe '#save' do
let(:repository) { Boxenn::Repository.new(source_wrapper: source_wrapper, record_mapper: record_mapper) }
let(:record_mapper) { spy('record_mapper', build: { hash: 'attributes' }) }
let(:record_mapper) do
record_mapper = spy('record_mapper')
allow(record_mapper).to receive(:build).with({ id: 1 }).and_return({ id: 1 })
allow(record_mapper).to receive(:build).with({ id: 1, hash: 'attributes' }).and_return({ id: 1, schema: 'attributes' })
record_mapper
end
let(:source_wrapper) { spy('source wrapper', save: 'result') }
let(:entity) { spy('entity', primary_keys_hash: { id: 1 }, id: 'id') }
let(:entity) { spy('entity', primary_keys_hash: { id: 1 }, to_h: { id: 1, hash: 'attributes' }) }

context 'when entity is provided' do
before { repository.save(entity) }

context 'maps the entity into an attribute hash' do
it { expect(record_mapper).to have_received(:build).with(entity) }
it { expect(record_mapper).to have_received(:build).twice }
end

context 'requires the source mapper to store record with updated attributes' do
it { expect(source_wrapper).to have_received(:save).with({ id: 1 }, { hash: 'attributes' }) }
it { expect(source_wrapper).to have_received(:save).with({ id: 1 }, { id: 1, schema: 'attributes' }) }
end

context 'returns the result' do
Expand Down

0 comments on commit 518e01d

Please sign in to comment.