-
Notifications
You must be signed in to change notification settings - Fork 18
Dynamic parameter persistence #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
aadf8ce
7c046a8
5d0566e
0e6d3fe
5e38aff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| module Cangaroo | ||
| class PersistParameters | ||
| include Interactor | ||
|
|
||
| def call | ||
| return if request_params.empty? || new_params.empty? | ||
| unless connection.update(parameters: new_params) | ||
| fail_context! | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def new_params | ||
| persisted_params | ||
| .with_indifferent_access | ||
| .merge(request_params) | ||
| .slice(*persisted_params.keys) | ||
| end | ||
|
|
||
| def connection | ||
| context.flow.send(:destination_connection) | ||
| end | ||
|
|
||
| def persisted_params | ||
| connection.parameters | ||
| end | ||
|
|
||
| def request_params | ||
| context.parameters.to_h.select{|k,v| v.present?} | ||
| end | ||
|
|
||
| def fail_context! | ||
| context.fail!( | ||
| message: "could not update #{context.flow.class.name} parameters: #{connection.errors.full_messages.to_sentence}", | ||
| error_code: 500 | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "orders":[ | ||
| { | ||
| "id":"O154085346172", | ||
| "state":"cart" | ||
| }, | ||
| { | ||
| "id":"O154085343224", | ||
| "state":"payed" | ||
| } | ||
| ], | ||
| "shipments":[ | ||
| { | ||
| "id":"S53454325", | ||
| "state":"shipped" | ||
| }, | ||
| { | ||
| "id":"S53565543", | ||
| "state":"waiting" | ||
| } | ||
| ], | ||
| "line_items": [], | ||
| "parameters": { | ||
| "first": 1521034044, | ||
| "second": "oh hai" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| require 'rails_helper' | ||
|
|
||
| class JobC < Cangaroo::BaseJob | ||
| connection :job_c_connection | ||
| end | ||
| class JobD < Cangaroo::BaseJob; end | ||
| class JobE < Cangaroo::BaseJob | ||
| connection :job_e_connection | ||
| end | ||
|
|
||
| describe Cangaroo::PersistParameters do | ||
| let!(:connection) { create(:cangaroo_connection, name: JobC.connection.to_s) } | ||
| let(:flow) { JobC.new(source_connection: nil, type: 'orders', payload: {}) } | ||
| let(:parameters) { Hash.new } | ||
| subject do | ||
| described_class.new( | ||
| flow: flow, | ||
| parameters: parameters | ||
| ) | ||
| end | ||
|
|
||
| describe 'integration with other interactors' do | ||
| let(:json_body) { JSON.parse(load_fixture('json_payload_parameters.json')) } | ||
| before(:each) do | ||
| job_d = double(:job_d, perform?: true, enqueue: true) | ||
| allow(JobD).to receive(:new).and_return(job_d) | ||
| end | ||
| context 'connection with parameters' do | ||
| before(:each) { expect(connection.parameters).to_not be_empty } | ||
| it "persists the new parameters" do | ||
| expect{ | ||
| Cangaroo::PerformFlow.call( | ||
| flow: flow, | ||
| json_body: json_body, | ||
| jobs: [JobD], | ||
| source_connection: connection | ||
| ) | ||
| }.to change{ | ||
| connection.reload.parameters | ||
| }.to(json_body["parameters"]) | ||
| end | ||
| end | ||
| context 'connection without parameters' do | ||
| let!(:connection) { create(:store, name: JobC.connection.to_s) } | ||
| before(:each) { expect(connection.parameters).to_not be_present } | ||
| it "doesn't persist params for a connection without them" do | ||
| expect{ | ||
| Cangaroo::PerformFlow.call( | ||
| flow: flow, | ||
| json_body: json_body, | ||
| jobs: [JobD], | ||
| source_connection: connection | ||
| ) | ||
| }.to_not change{ | ||
| connection.reload.parameters | ||
| }.from({}) | ||
| end | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at block body end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at block body end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at block body end. |
||
| end | ||
| end | ||
|
|
||
| describe '#call' do | ||
| context "new parameters" do | ||
| let(:parameters) { { connection.parameters.keys.first => "new value" } } | ||
| it 'persists the new parameters' do | ||
| old_params = connection.parameters | ||
| new_params = connection.parameters.with_indifferent_access.merge(parameters) | ||
| expect{ | ||
| subject.call | ||
| }.to change{ | ||
| connection.reload.parameters | ||
| }.from(old_params).to(new_params) | ||
| end | ||
| it 'fails if updates cannot be persisted' do | ||
| allow_any_instance_of(Cangaroo::Connection).to receive(:update) { false } | ||
| context = described_class.call(flow: flow, parameters: parameters) | ||
| expect(context).to_not be_success | ||
| expect(context).to be_failure | ||
| end | ||
| end | ||
| context 'different request parameters' do | ||
| let(:parameters) { { "different" => "param" } } | ||
| it 'does not update the DB' do | ||
| expect(connection.parameters.keys).to_not include(parameters.keys.first) | ||
| expect{ subject.call }.to_not change{ connection.reload.parameters.with_indifferent_access } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parenthesize the param change{ connection.reload.parameters.with_indifferent_access } to make sure that the block will be associated with the change method call. |
||
| end | ||
| end | ||
| it 'does not update the DB if there are no request params' do | ||
| expect(parameters).to be_empty | ||
| expect{ subject.call }.to_not change{ connection.reload.parameters } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parenthesize the param change{ connection.reload.parameters } to make sure that the block will be associated with the change method call. |
||
| end | ||
| end | ||
|
|
||
| describe '#connection' do | ||
| it "is the flow's connection" do | ||
| expect(subject.send(:connection)).to eq connection | ||
| end | ||
| end | ||
|
|
||
| describe '#request_params' do | ||
| context 'nil params' do | ||
| let(:parameters) { nil } | ||
| it "does not throw an error" do | ||
| expect(subject.send(:request_params)).to eq({}) | ||
| end | ||
| end | ||
| it "handles empty params" do | ||
| expect(parameters).to be_empty | ||
| expect(subject.send(:request_params)).to eq({}) | ||
| end | ||
| context "empty param values" do | ||
| let(:parameters) { {"a" => nil, "b" => 3, "c" => ""} } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space inside { missing. |
||
| it "removes them" do | ||
| expect(subject.send(:request_params)).to eq({"b" => 3}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space inside { missing. |
||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#new_params" do | ||
| let(:new_params) { subject.send(:new_params) } | ||
| context "empty request params" do | ||
| before(:each) { expect(parameters).to be_empty } | ||
| it "returns the persisted params" do | ||
| persisted_params = connection.parameters | ||
| persisted_params.each do |key, value| | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use each_key instead of each. |
||
| expect(new_params.fetch(key)).to eq(persisted_params.fetch(key)) | ||
| end | ||
| end | ||
| end | ||
| context "extra request params" do | ||
| let(:parameters) { connection.parameters.merge("a" => 1) } | ||
| it "removes them" do | ||
| expect(new_params).to_not have_key("a") | ||
| end | ||
| end | ||
| context "missing request params" do | ||
| let(:param_key) { connection.parameters.keys.first } | ||
| let(:parameters) { { param_key => "new value" } } | ||
| it "ignores them" do | ||
| old_params = connection.parameters | ||
| missing_keys = old_params.keys - [param_key] | ||
| missing_keys.each do |missing_key| | ||
| expect(new_params[missing_key]).to eq(old_params[missing_key]) | ||
| end | ||
| end | ||
| it "sets the present key" do | ||
| expect(new_params[param_key]).to eq(parameters[param_key]) | ||
| end | ||
| end | ||
| context "symbol request params" do | ||
| let(:param_key) { connection.parameters.keys.first } | ||
| let(:parameters) { { param_key.to_sym => "new value" } } | ||
| before(:each) do | ||
| connection.update(parameters: connection.parameters.stringify_keys) | ||
| end | ||
| it "still matches them with their string counterparts" do | ||
| expect(new_params[param_key]).to eq(parameters[param_key.to_sym]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| require 'factory_girl_rails' | ||
| require 'factory_bot_rails' | ||
|
|
||
| RSpec.configure do |config| | ||
| config.include FactoryGirl::Syntax::Methods | ||
| config.include FactoryBot::Syntax::Methods | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space between { and | missing.
Unused block argument - k. If it's necessary, use _ or _k as an argument name to indicate that it won't be used.
Space missing after comma.
Space missing inside }.