Skip to content

Commit

Permalink
Small modification in :fetch_model step behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Dec 18, 2017
1 parent 9c29d5d commit bb20727
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
@@ -1,3 +1,8 @@
## [0.5.1] - 2017-12-18
### Changed
- Change behavior for `:fetch_model` step option `search_by:` to override both the search column and the input key (combine it with `using:` if you need a different value for the input key as well)
- `:fetch_model` step will no longer hit the database if the input key is nil and just return a `:not_found` error instead

## [0.5.0] - 2017-11-6
### Changed
- Change base class for `Pathway::Error` from `StandardError` to `Object`
Expand All @@ -20,7 +25,7 @@

## [0.0.20] - 2017-10-17
### Changed
- Renamed options `key:` and `column:` to `using` and `search_by`, for `:fetch_model` step, at `:sequel_models` plugin
- Renamed options `key:` and `column:` to `using:` and `search_by:`, for `:fetch_model` step, at `:sequel_models` plugin

### Added
- Added new option `to:` for overriding where to store the result, for `:fetch_model` step, at `:sequel_models` plugin
Expand Down
5 changes: 3 additions & 2 deletions lib/pathway/plugins/sequel_models.rb
Expand Up @@ -42,9 +42,10 @@ module InstanceMethods
delegate %i[model_class search_field] => 'self.class'
delegate :db => :model_class

def fetch_model(state, from: model_class, using: search_field, search_by: search_field, to: result_key, overwrite: false)
def fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false)
if state[to].nil? || overwrite
find_model_with(state[:input][using], from, search_by)
wrap_if_present(state[:input][using])
.then { |key| find_model_with(key, from, search_by) }
.then { |model| state.update(to => model) }
else
state
Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
@@ -1,3 +1,3 @@
module Pathway
VERSION = '0.5.0'
VERSION = '0.5.1'
end
54 changes: 45 additions & 9 deletions spec/plugins/sequel_models_spec.rb
Expand Up @@ -117,29 +117,65 @@ def send_emails(my_model:,**)
let(:repository) { double }
let(:object) { double }

it "fetches an instance through 'model_class' and sets result key" do
it "fetches an instance through 'model_class' into result key" do
expect(MyModel).to receive(:first).with(email: key).and_return(object)

expect(operation.fetch_model(input: {email: key}).value[:my_model]).to eq(object)
end

it "fetches an instance through 'model_class' and sets result key using an overrided search column, input key and repository when provdided" do
expect(repository).to receive(:first).with(pk: 'foo').and_return(object)
expect(MyModel).to_not receive(:first)

state = { input: { myid: 'foo' } }
result = operation
.fetch_model({input: {myid: 'foo'}}, from: repository, using: :myid, search_by: :pk)
.fetch_model(state, from: repository, using: :myid, search_by: :pk)
.value[:my_model]

expect(result).to eq(object)
end

it "fetches an instance through 'model_class' into result key using default arguments if none specified" do
expect(MyModel).to receive(:first).with(email: key).and_return(object)

expect(operation.fetch_model(input: {email: key}).value[:my_model]).to eq(object)
it "fetches an instance through 'model_class' and sets result key using an overrided search column and input key with only :search_by is provided" do
expect(MyModel).to receive(:first).with(name: 'foobar').and_return(object)

state = { input: { email: 'other@email.com', name: 'foobar' } }
result = operation
.fetch_model(state, search_by: :name)
.value[:my_model]

expect(result).to eq(object)
end

it "fetches an instance through 'model_class' and sets result key using an overrided input key with but not search column when only :using is provided" do
expect(MyModel).to receive(:first).with(email: 'foobar@mail.com').and_return(object)

state = { input: { email: 'other@email.com', first_email: 'foobar@mail.com' } }
result = operation
.fetch_model(state, using: :first_email)
.value[:my_model]

expect(result).to eq(object)
end

it 'returns an error when no instance is found', :aggregate_failures do
allow(MyModel).to receive(:first).with(email: key).and_return(nil)
expect(MyModel).to receive(:first).with(email: key).and_return(nil)

result = operation.fetch_model(input: {email: key})

expect(result).to be_an(Result::Failure)
expect(result.error).to be_an(Pathway::Error)
expect(result.error.type).to eq(:not_found)
end

it 'returns an error without hitting the database when search key is nil', :aggregate_failures do
expect(MyModel).to_not receive(:first)

result = operation.fetch_model(input: {email: nil})

expect(operation.fetch_model(input: {email: key})).to be_an(Result::Failure)
expect(operation.fetch_model(input: {email: key}).error).to be_an(Pathway::Error)
expect(operation.fetch_model(input: {email: key}).error.type).to eq(:not_found)
expect(result).to be_an(Result::Failure)
expect(result.error).to be_an(Pathway::Error)
expect(result.error.type).to eq(:not_found)
end
end

Expand Down

0 comments on commit bb20727

Please sign in to comment.