Skip to content

Commit e1fc347

Browse files
thatguysimonbethesque
thatguysimon
authored andcommitted
feat: support marking an interaction as writable/not writable (pact-foundation#75)
* feat(skip writing to pact): Add metadata attribute to Interaction and add #writable_interactions * Stop using the safe navigation operator
1 parent eb5837b commit e1fc347

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ reports
3030
Gemfile.lock
3131
gemfiles/*.lock
3232
spec/examples.txt
33+
.byebug_history

lib/pact/consumer_contract/consumer_contract.rb

+11
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,16 @@ def each
8282
end
8383
end
8484

85+
def writable_interactions
86+
interactions.reject do |interaction|
87+
# For the sake of backwards compatibility, only reject interactions where
88+
# write_to_pact is explicitly set to false
89+
interaction.respond_to?(:metadata) &&
90+
!interaction.metadata.nil? &&
91+
interaction.metadata.key?(:write_to_pact) &&
92+
interaction.metadata[:write_to_pact] == false
93+
end
94+
end
95+
8596
end
8697
end

lib/pact/consumer_contract/interaction.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ module Pact
55
class Interaction
66
include ActiveSupportSupport
77

8-
attr_accessor :description, :request, :response, :provider_state, :provider_states
8+
attr_accessor :description, :request, :response, :provider_state, :provider_states, :metadata
99

1010
def initialize attributes = {}
1111
@description = attributes[:description]
1212
@request = attributes[:request]
1313
@response = attributes[:response]
1414
@provider_state = attributes[:provider_state] || attributes[:providerState]
1515
@provider_states = attributes[:provider_states]
16+
@metadata = attributes[:metadata]
1617
end
1718

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

3132
h[:request] = request.to_hash
3233
h[:response] = response.to_hash
34+
h[:metadata] = metadata
3335
h
3436
end
3537

lib/pact/consumer_contract/interaction_v2_parser.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ def self.call hash, options
1414
request = parse_request(hash['request'], options)
1515
response = parse_response(hash['response'], options)
1616
provider_states = parse_provider_states(hash['providerState'] || hash['provider_state'])
17-
Interaction.new(symbolize_keys(hash).merge(request: request, response: response, provider_states: provider_states))
17+
metadata = parse_metadata(hash['metadata'])
18+
Interaction.new(symbolize_keys(hash).merge(request: request,
19+
response: response,
20+
provider_states: provider_states,
21+
metadata: metadata))
1822
end
1923

2024
def self.parse_request request_hash, options
@@ -30,5 +34,9 @@ def self.parse_response response_hash, options
3034
def self.parse_provider_states provider_state_name
3135
provider_state_name ? [Pact::ProviderState.new(provider_state_name)] : []
3236
end
37+
38+
def self.parse_metadata metadata_hash
39+
symbolize_keys(metadata_hash)
40+
end
3341
end
3442
end

lib/pact/consumer_contract/interaction_v3_parser.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def self.call hash, options
1919
if provider_states && provider_states.size > 1
2020
Pact.configuration.error_stream.puts("WARN: Currently only 1 provider state is supported. Ignoring ")
2121
end
22-
Interaction.new(symbolize_keys(hash).merge(request: request, response: response, provider_states: provider_states, provider_state: provider_state))
22+
metadata = parse_metadata(hash['metadata'])
23+
Interaction.new(symbolize_keys(hash).merge(request: request,
24+
response: response,
25+
provider_states: provider_states,
26+
provider_state: provider_state,
27+
metadata: metadata))
2328
end
2429

2530
def self.parse_request request_hash, options
@@ -69,5 +74,9 @@ def self.parse_provider_states provider_states
6974
Pact::ProviderState.new(provider_state_hash['name'], provider_state_hash['params'])
7075
end
7176
end
77+
78+
def self.parse_metadata metadata_hash
79+
symbolize_keys(metadata_hash)
80+
end
7281
end
7382
end

spec/lib/pact/consumer_contract/consumer_contract_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,27 @@ module Pact
138138
end
139139
end
140140

141+
describe "#writable_interactions" do
142+
let(:consumer) { double('Pact::ServiceConsumer', :name => 'Consumer')}
143+
let(:provider) { double('Pact::ServiceProvider', :name => 'Provider')}
144+
let(:interaction1) { double('Pact::Interaction') }
145+
let(:interaction2) { double('Pact::Interaction') }
146+
let(:interaction3) { double('Pact::Interaction') }
147+
let(:interaction4) { double('Pact::Interaction') }
148+
149+
before do
150+
allow(interaction1).to receive(:metadata).and_return(write_to_pact: false)
151+
allow(interaction2).to receive(:metadata).and_return(write_to_pact: true)
152+
allow(interaction3).to receive(:metadata).and_return(some_other_key: :some_value)
153+
allow(interaction4).to receive(:metadata).and_return(nil)
154+
end
155+
156+
subject { ConsumerContract.new(:interactions => [interaction1, interaction2, interaction3, interaction4], :consumer => consumer, :provider => provider) }
157+
context "when one interaction is not writable" do
158+
it "returns only the writable interactions" do
159+
expect(subject.writable_interactions.size).to eql 3
160+
end
161+
end
162+
end
141163
end
142164
end

0 commit comments

Comments
 (0)