Skip to content

Commit

Permalink
fix(matrix): return only most recent row missing verification when la…
Browse files Browse the repository at this point in the history
…testby=cp
  • Loading branch information
bethesque committed Aug 30, 2023
1 parent da6b0bb commit b7550e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
12 changes: 8 additions & 4 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<PactBroker::Matrix::UnresolvedSelector>] unresolved_specified_selectors
Expand Down Expand Up @@ -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
Expand Down
28 changes: 13 additions & 15 deletions spec/lib/pact_broker/matrix/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b7550e5

Please sign in to comment.