From e8a88eb954244c80b5a7ce18dfe5b6e84187409f Mon Sep 17 00:00:00 2001 From: David Laprade Date: Wed, 14 Mar 2018 09:23:53 -0400 Subject: [PATCH] Test dynamic parameter persistence --- .../cangaroo/persist_parameters.rb | 16 ++- spec/fixtures/json_payload_parameters.json | 27 ++++ .../cangaroo/persist_parameters_spec.rb | 131 ++++++++++++++++++ 3 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/json_payload_parameters.json create mode 100644 spec/interactors/cangaroo/persist_parameters_spec.rb diff --git a/app/interactors/cangaroo/persist_parameters.rb b/app/interactors/cangaroo/persist_parameters.rb index a829b26..275fbb9 100644 --- a/app/interactors/cangaroo/persist_parameters.rb +++ b/app/interactors/cangaroo/persist_parameters.rb @@ -5,10 +5,7 @@ class PersistParameters def call return if request_params.empty? unless connection.update(parameters: new_params) - context.fail!( - message: "could not update #{context.flow.class.name} parameters: #{connection.errors.full_messages.to_sentence}", - error_code: 500 - ) + fail_context! end end @@ -16,6 +13,7 @@ def call def new_params persisted_params + .with_indifferent_access .merge(request_params) .slice(*persisted_params.keys) end @@ -25,12 +23,18 @@ def connection end def persisted_params - connection.parameters.stringify_keys + connection.parameters end def request_params - context.parameters.to_h.select{|k,v| v.present?}.stringify_keys + 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 diff --git a/spec/fixtures/json_payload_parameters.json b/spec/fixtures/json_payload_parameters.json new file mode 100644 index 0000000..33471d2 --- /dev/null +++ b/spec/fixtures/json_payload_parameters.json @@ -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" + } +} diff --git a/spec/interactors/cangaroo/persist_parameters_spec.rb b/spec/interactors/cangaroo/persist_parameters_spec.rb new file mode 100644 index 0000000..f10eeb2 --- /dev/null +++ b/spec/interactors/cangaroo/persist_parameters_spec.rb @@ -0,0 +1,131 @@ +require 'rails_helper' + +class JobC < Cangaroo::BaseJob + connection :job_c_connection +end +class JobD < Cangaroo::BaseJob; 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 + 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 + + 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 + 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 } + 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" => ""} } + it "removes them" do + expect(subject.send(:request_params)).to eq({"b" => 3}) + 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| + 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