diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 387872ca2..dee0f8a28 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -20,7 +20,7 @@ class Repository GROUP_BY_PROVIDER_VERSION_NUMBER = [:consumer_name, :consumer_version_number, :provider_name, :provider_version_number] GROUP_BY_PROVIDER = [:consumer_name, :consumer_version_number, :provider_name] - GROUP_BY_PACT = [:consumer_name, :provider_name] + GROUP_BY_CONSUMER_AND_PROVIDER = [:consumer_name, :provider_name] # THE METHOD for querying the Matrix # @param [Array] unresolved_specified_selectors @@ -91,14 +91,18 @@ def apply_latestby(options, lines) group_by_columns = case options[:latestby] when "cvpv" then GROUP_BY_PROVIDER_VERSION_NUMBER when "cvp" then GROUP_BY_PROVIDER - when "cp" then GROUP_BY_PACT + when "cp" then GROUP_BY_CONSUMER_AND_PROVIDER end # The group with the nil provider_version_numbers will be the results of the left outer join # that don't have verifications, so we need to include them all. - lines.group_by{|line| group_by_columns.collect{|key| line.send(key) }} + + lines + .group_by{ |line| group_by_columns.collect{ |key| line.send(key) } } .values - .collect{ | line | line.first.provider_version_number.nil? ? line : line.sort_by(&:provider_version_order).last } + .collect { | lines | + lines.first.provider_version_number.nil? ? lines.first : lines.sort_by(&:provider_version_order).last + } .flatten end # rubocop: enable Metrics/CyclomaticComplexity diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index 60f69e626..ae574f9d9 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -20,24 +20,24 @@ def shorten_rows rows describe "find" do before do - # A1 - B1 - # A1 - B1 r2 - # A1 - B2 r3 - # A1 - C1 + # A1 - B1 n1 + # A1 - B1 n2 + # A1 - B2 n3 + # A1 - C1 n1 + # A2 - C3 n1 # A2 - B? - # A2 - C2 td.create_pact_with_hierarchy("A", "1", "B") - .create_verification(provider_version: "1", success: false) - .create_verification(provider_version: "1", number: 2, success: true) - .create_verification(provider_version: "2", number: 3, success: true) + .create_verification(provider_version: "1", success: false).comment("A1 - B1 n1") + .create_verification(provider_version: "1", number: 2, success: true).comment("A1 - B1 n2") + .create_verification(provider_version: "2", number: 3, success: true).comment("A1 - B2 n3") .create_provider("C") .create_pact - .create_verification(provider_version: "1") + .create_verification(provider_version: "1").comment("A1 - C1 n1") .create_consumer_version("2") - .create_pact - .create_verification(provider_version: "3") + .create_pact.comment("A2") + .create_verification(provider_version: "3").comment("A2 - C3 n1") .use_provider("B") - .create_pact + .create_pact.comment("A2 - B?") end subject { shorten_rows(rows) } @@ -89,14 +89,12 @@ def shorten_rows rows end end - context "when latestby=cp", pending: true do + context "when latestby=cp" do let(:latestby) { "cp" } it "returns the latest rows per consumer/provider" do expect(subject).to include "A2 C3 n1" expect(subject).to include "A2 B? n?" - expect(subject).to include a1_c1_n1 - expect(subject).to_not include a1_b2_n3 expect(subject.size).to eq 2 end end