Skip to content

Commit

Permalink
fix: correct logic for determining if all interactions for a pact hav…
Browse files Browse the repository at this point in the history
…e been verified

Closes: #221
  • Loading branch information
bethesque committed Sep 28, 2020
1 parent 0813498 commit c4f968e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/pact/provider/pact_source.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact/consumer_contract/pact_file'
require 'pact/hal/http_client'
require 'pact/hal/entity'
require 'pact/consumer_contract'

module Pact
module Provider
Expand All @@ -24,6 +25,10 @@ def pending?
uri.metadata[:pending]
end

def consumer_contract
@consumer_contract ||= Pact::ConsumerContract.from_json(pact_json)
end

def hal_entity
http_client_keys = [:username, :password, :token]
http_client_options = uri.options.reject{ |k, _| !http_client_keys.include?(k) }
Expand Down
30 changes: 17 additions & 13 deletions lib/pact/provider/verification_results/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ def any_failures?
end

def publishable?
executed_interactions_count == all_interactions_count && all_interactions_count > 0
if defined?(@publishable)
@publishable
else
@publishable = pact_source.consumer_contract.interactions.all? do | interaction |
examples_for_pact_uri.any?{ |e| example_is_for_interaction?(e, interaction) }
end && examples_for_pact_uri.count > 0
end
end

def example_is_for_interaction?(example, interaction)
# Use the Pact Broker id if supported
if interaction._id
example[:pact_interaction]._id == interaction._id
else
# fall back to object equality (based on the field values of the interaction)
example[:pact_interaction] == interaction
end
end

def examples_for_pact_uri
Expand All @@ -39,18 +55,6 @@ def count_failures_for_pact_uri
examples_for_pact_uri.count{ |e| e[:status] != 'passed' }
end

def executed_interactions_count
examples_for_pact_uri
.collect { |e| e[:pact_interaction].object_id }
.uniq
.count
end

def all_interactions_count
interactions = (pact_source.pact_hash['interactions'] || pact_source.pact_hash['messages'])
interactions ? interactions.count : 0
end

def test_results_hash_for_pact_uri
{
tests: examples_for_pact_uri.collect{ |e| clean_example(e) },
Expand Down
2 changes: 1 addition & 1 deletion pact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'webrick', '~> 1.3'
gem.add_runtime_dependency 'term-ansicolor', '~> 1.0'

gem.add_runtime_dependency 'pact-support', '~> 1.9'
gem.add_runtime_dependency 'pact-support', '~> 1.15'
gem.add_runtime_dependency 'pact-mock_service', '~> 3.0', '>= 3.3.1'

gem.add_development_dependency 'rake', '~> 13.0'
Expand Down
9 changes: 6 additions & 3 deletions spec/integration/publish_verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'pact/provider/pact_uri'

describe "publishing verifications" do

before do
allow(Pact.configuration).to receive(:provider).and_return(provider_configuration)
allow($stdout).to receive(:puts)
Expand All @@ -16,13 +15,16 @@
end

let(:pact_sources) do
[instance_double('Pact::Provider::PactSource', pact_hash: pact_hash, uri: pact_uri)]
[instance_double('Pact::Provider::PactSource', consumer_contract: consumer_contract, pact_hash: pact_hash, uri: pact_uri)]
end

let(:pact_uri) do
instance_double('Pact::Provider::PactURI', uri: 'pact.json', options: {}, metadata: metadata)
end

let(:consumer_contract) { instance_double('Pact::ConsumerContract', interactions: [pact_interaction])}
let(:pact_interaction) { instance_double('Pact::Interaction', _id: "1") }

let(:metadata) { { notices: notices} }
let(:notices) { instance_double('Pact::PactBroker::Notices', after_verification_notices_text: ['hello'] ) }

Expand Down Expand Up @@ -53,7 +55,8 @@
{
testDescription: '1',
status: 'passed',
pact_uri: pact_uri
pact_uri: pact_uri,
pact_interaction: pact_interaction
}
]
}
Expand Down
23 changes: 10 additions & 13 deletions spec/lib/pact/provider/verification_results/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ module VerificationResults
double('provider_configuration', application_version: '1.2.3')
end
let(:pact_source_1) do
instance_double('Pact::Provider::PactSource', uri: pact_uri_1, pact_hash: pact_hash_1)
instance_double('Pact::Provider::PactSource', uri: pact_uri_1, consumer_contract: consumer_contract)
end
let(:consumer_contract) { instance_double('Pact::ConsumerContract', interactions: interactions)}
let(:interactions) { [interaction_1] }
let(:interaction_1) { instance_double('Pact::Interaction', _id: "1") }
let(:interaction_2) { instance_double('Pact::Interaction', _id: "2") }
let(:pact_uri_1) { instance_double('Pact::Provider::PactURI', uri: URI('foo')) }
let(:pact_uri_2) { instance_double('Pact::Provider::PactURI', uri: URI('bar')) }
let(:example_1) do
{
pact_uri: pact_uri_1,
pact_interaction: double('interaction'),
pact_interaction: interaction_1,
status: 'passed'
}
end
let(:example_2) do
{
pact_uri: pact_uri_2,
pact_interaction: double('interaction'),
pact_interaction: interaction_2,
status: 'passed'
}
end
Expand All @@ -37,11 +41,6 @@ module VerificationResults
tests: [example_1, example_2]
}
end
let(:pact_hash_1) do
{
'interactions' => [{}]
}
end

subject { Create.call(pact_source_1, test_results_hash) }

Expand Down Expand Up @@ -78,11 +77,9 @@ module VerificationResults
end

context "when not every interaction has been executed" do
let(:pact_hash_1) do
{
'interactions' => [{}, {}]
}
end
let(:interaction_3) { instance_double('Pact::Interaction', _id: "3") }
let(:interactions) { [interaction_1, interaction_2]}

it "sets publishable to false" do
expect(VerificationResult).to receive(:new).with(false, anything, anything, anything)
subject
Expand Down

0 comments on commit c4f968e

Please sign in to comment.