Skip to content

Commit

Permalink
Test dynamic parameter persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlaprade committed Mar 14, 2018
1 parent 457b297 commit e8a88eb
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/interactors/cangaroo/persist_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ 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

private

def new_params
persisted_params
.with_indifferent_access
.merge(request_params)
.slice(*persisted_params.keys)
end
Expand All @@ -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
27 changes: 27 additions & 0 deletions spec/fixtures/json_payload_parameters.json
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"
}
}
131 changes: 131 additions & 0 deletions spec/interactors/cangaroo/persist_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e8a88eb

Please sign in to comment.