Skip to content

Commit

Permalink
feat(pacts for verification): update inclusion messages to specify co…
Browse files Browse the repository at this point in the history
…nsumer name when selecting all pacts for a given consumer
  • Loading branch information
bethesque committed Mar 24, 2020
1 parent 749e708 commit fdff0c5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
13 changes: 6 additions & 7 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,17 @@ def find_latest_pact_versions_for_provider provider_name, tag = nil
end
end

def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, consumer_version_tag_name, consumer_name = nil
def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, selector
provider = pacticipant_repository.find_by_name(provider_name)
consumer = consumer_name ? pacticipant_repository.find_by_name(consumer_name) : nil
consumer = selector.consumer ? pacticipant_repository.find_by_name(selector.consumer) : nil

PactPublication
.select_all_qualified
.select_append(Sequel[:cv][:order].as(:consumer_version_order))
.select_append(Sequel[:ct][:name].as(:consumer_version_tag_name))
.remove_overridden_revisions
.join_consumer_versions(:cv)
.join_consumer_version_tags_with_names(consumer_version_tag_name)
.join_consumer_version_tags_with_names(selector.tag)
.where(provider: provider)
.where_consumer_if_set(consumer)
.eager(:consumer)
Expand All @@ -150,10 +151,8 @@ def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name
.group_by(&:pact_version_id)
.values
.collect do | pact_publications |
selector_tag_names = pact_publications.collect{ | p| p.values.fetch(:consumer_version_tag_name) }
latest_pact_publication = pact_publications.sort_by{ |p| p.values.fetch(:consumer_version_order) }.last
selectors = Selectors.create_for_all_of_each_tag(selector_tag_names)
SelectedPact.new(latest_pact_publication.to_domain, selectors)
SelectedPact.new(latest_pact_publication.to_domain, [selector])
end
end

Expand Down Expand Up @@ -461,7 +460,7 @@ def find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, co
selectors = consumer_version_selectors.select(&:all_for_tag?)

selectors.flat_map do | selector |
find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector.tag, selector.consumer)
find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector)
end
end

Expand Down
10 changes: 10 additions & 0 deletions lib/pact_broker/pacts/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def latest_for_tag? potential_tag = nil
end
end

def all_for_tag_and_consumer?
!!(tag && !latest? && consumer)
end

def all_for_tag?
!!(tag && !latest?)
end
Expand All @@ -95,6 +99,12 @@ def <=> other
else
latest_for_tag? ? -1 : 1
end
elsif consumer || other.consumer
if consumer == other.consumer
tag <=> other.tag
else
consumer ? -1 : 1
end
else
tag <=> other.tag
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/pacts/verifiable_pact_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def selector_description selector
else
"latest pact for a consumer version tagged '#{selector.tag}'"
end
elsif selector.tag
elsif selector.all_for_tag_and_consumer?
"pacts for all #{selector.consumer} versions tagged '#{selector.tag}'"
elsif selector.all_for_tag?
"pacts for all consumer versions tagged '#{selector.tag}'"
else
selector.to_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def find_by_consumer_name_and_consumer_version_number(consumer_name, consumer_ve
expect(find_by_consumer_version_number("prod-version-2")).to_not be nil
expect(find_by_consumer_version_number("prod-version-3")).to be nil
end

it "sets the selectors" do
expect(find_by_consumer_version_number("prod-version-1").selectors.first.tag).to eq 'prod'
expect(find_by_consumer_version_number("prod-version-1").selectors.first.consumer).to eq 'Foo2'
end
end

context "when a pact version has been selected by two different selectors" do
Expand Down
7 changes: 5 additions & 2 deletions spec/lib/pact_broker/pacts/selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ module Pacts
let(:overall_latest_2) { Selector.overall_latest }
let(:latest_for_tag_prod) { Selector.latest_for_tag('prod') }
let(:latest_for_tag_dev) { Selector.latest_for_tag('dev') }
let(:all_prod_for_consumer_1) { Selector.all_for_tag_and_consumer('prod', 'Foo') }
let(:all_prod_for_consumer_2) { Selector.all_for_tag_and_consumer('prod', 'Bar') }
let(:all_dev_for_consumer_1) { Selector.all_for_tag_and_consumer('dev', 'Bar') }
let(:all_prod) { Selector.all_for_tag('prod') }
let(:all_dev) { Selector.all_for_tag('dev') }

let(:unsorted_selectors) do
[all_prod, all_dev, latest_for_tag_prod, overall_latest_1, overall_latest_1, latest_for_tag_dev]
[all_prod, all_dev, all_dev_for_consumer_1, latest_for_tag_prod, overall_latest_1, overall_latest_1, latest_for_tag_dev, all_prod_for_consumer_2, all_prod_for_consumer_1]
end

let(:expected_sorted_selectors) do
[overall_latest_1, overall_latest_1, latest_for_tag_dev, latest_for_tag_prod, all_dev, all_prod]
[overall_latest_1, overall_latest_1, latest_for_tag_dev, latest_for_tag_prod, all_dev_for_consumer_1, all_prod_for_consumer_2, all_prod_for_consumer_1, all_dev, all_prod]
end

it "sorts the selectors" do
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ module Pacts

its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it matches the following configured selection criterion: pacts for all consumer versions tagged 'prod'"}
end

context "when the pact is one of all versions for a tag and consumer" do
let(:selectors) { Selectors.new(Selector.all_for_tag_and_consumer('prod', 'Foo')) }

its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it matches the following configured selection criterion: pacts for all Foo versions tagged 'prod'"}
end
end

describe "#pending_reason" do
Expand Down

0 comments on commit fdff0c5

Please sign in to comment.