Skip to content

Commit

Permalink
feat: support marking an interaction as writable/not writable (#75)
Browse files Browse the repository at this point in the history
* feat(skip writing to pact): Add metadata attribute to Interaction and add #writable_interactions

* Stop using the safe navigation operator
  • Loading branch information
thatguysimon authored and bethesque committed Sep 29, 2019
1 parent eb5837b commit e1fc347
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ reports
Gemfile.lock
gemfiles/*.lock
spec/examples.txt
.byebug_history
11 changes: 11 additions & 0 deletions lib/pact/consumer_contract/consumer_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,16 @@ def each
end
end

def writable_interactions
interactions.reject do |interaction|
# For the sake of backwards compatibility, only reject interactions where
# write_to_pact is explicitly set to false
interaction.respond_to?(:metadata) &&
!interaction.metadata.nil? &&
interaction.metadata.key?(:write_to_pact) &&
interaction.metadata[:write_to_pact] == false
end
end

end
end
4 changes: 3 additions & 1 deletion lib/pact/consumer_contract/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ module Pact
class Interaction
include ActiveSupportSupport

attr_accessor :description, :request, :response, :provider_state, :provider_states
attr_accessor :description, :request, :response, :provider_state, :provider_states, :metadata

def initialize attributes = {}
@description = attributes[:description]
@request = attributes[:request]
@response = attributes[:response]
@provider_state = attributes[:provider_state] || attributes[:providerState]
@provider_states = attributes[:provider_states]
@metadata = attributes[:metadata]
end

def self.from_hash hash, options = {}
Expand All @@ -30,6 +31,7 @@ def to_hash

h[:request] = request.to_hash
h[:response] = response.to_hash
h[:metadata] = metadata
h
end

Expand Down
10 changes: 9 additions & 1 deletion lib/pact/consumer_contract/interaction_v2_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def self.call hash, options
request = parse_request(hash['request'], options)
response = parse_response(hash['response'], options)
provider_states = parse_provider_states(hash['providerState'] || hash['provider_state'])
Interaction.new(symbolize_keys(hash).merge(request: request, response: response, provider_states: provider_states))
metadata = parse_metadata(hash['metadata'])
Interaction.new(symbolize_keys(hash).merge(request: request,
response: response,
provider_states: provider_states,
metadata: metadata))
end

def self.parse_request request_hash, options
Expand All @@ -30,5 +34,9 @@ def self.parse_response response_hash, options
def self.parse_provider_states provider_state_name
provider_state_name ? [Pact::ProviderState.new(provider_state_name)] : []
end

def self.parse_metadata metadata_hash
symbolize_keys(metadata_hash)
end
end
end
11 changes: 10 additions & 1 deletion lib/pact/consumer_contract/interaction_v3_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def self.call hash, options
if provider_states && provider_states.size > 1
Pact.configuration.error_stream.puts("WARN: Currently only 1 provider state is supported. Ignoring ")
end
Interaction.new(symbolize_keys(hash).merge(request: request, response: response, provider_states: provider_states, provider_state: provider_state))
metadata = parse_metadata(hash['metadata'])
Interaction.new(symbolize_keys(hash).merge(request: request,
response: response,
provider_states: provider_states,
provider_state: provider_state,
metadata: metadata))
end

def self.parse_request request_hash, options
Expand Down Expand Up @@ -69,5 +74,9 @@ def self.parse_provider_states provider_states
Pact::ProviderState.new(provider_state_hash['name'], provider_state_hash['params'])
end
end

def self.parse_metadata metadata_hash
symbolize_keys(metadata_hash)
end
end
end
22 changes: 22 additions & 0 deletions spec/lib/pact/consumer_contract/consumer_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,27 @@ module Pact
end
end

describe "#writable_interactions" do
let(:consumer) { double('Pact::ServiceConsumer', :name => 'Consumer')}
let(:provider) { double('Pact::ServiceProvider', :name => 'Provider')}
let(:interaction1) { double('Pact::Interaction') }
let(:interaction2) { double('Pact::Interaction') }
let(:interaction3) { double('Pact::Interaction') }
let(:interaction4) { double('Pact::Interaction') }

before do
allow(interaction1).to receive(:metadata).and_return(write_to_pact: false)
allow(interaction2).to receive(:metadata).and_return(write_to_pact: true)
allow(interaction3).to receive(:metadata).and_return(some_other_key: :some_value)
allow(interaction4).to receive(:metadata).and_return(nil)
end

subject { ConsumerContract.new(:interactions => [interaction1, interaction2, interaction3, interaction4], :consumer => consumer, :provider => provider) }
context "when one interaction is not writable" do
it "returns only the writable interactions" do
expect(subject.writable_interactions.size).to eql 3
end
end
end
end
end

0 comments on commit e1fc347

Please sign in to comment.