Skip to content

Commit

Permalink
feat(matrix): add method to find all matrix lines for a list of versi…
Browse files Browse the repository at this point in the history
…ons, regardless of verification status
  • Loading branch information
bethesque committed Oct 16, 2017
1 parent f68ca21 commit 52755fd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ class Repository
include PactBroker::Repositories::Helpers
include PactBroker::Repositories

def find criteria
version_ids = criteria.reject{ |key, value| !value }.collect do | key, value |
version_repository.find_by_pacticipant_name_and_number(key, value).id
end

pacticipant_names = criteria.reject{|key, value| value }.keys

# If there is a nil provider_version_number it is because there is no verification
# but the row has been included because it is a left outer join.
# All the unverified pacts will be grouped together in the group_by because of this,
# so we include all of that group.
find_for_version_ids(version_ids, pacticipant_names)
.group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]}
.values
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
.flatten
end

def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
find_for_version_ids([], [pacticipant_1_name, pacticipant_2_name])
.sort{|l1, l2| l2[:consumer_version_order] <=> l1[:consumer_version_order]}
Expand All @@ -17,17 +35,7 @@ def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
# Returns a list of matrix lines indicating the compatible versions
#
def find_compatible_pacticipant_versions criteria
version_ids = criteria.reject{ |key, value| !value }.collect do | key, value |
version_repository.find_by_pacticipant_name_and_number(key, value).id
end

pacticipant_names = criteria.reject{|key, value| value }.keys

find_for_version_ids(version_ids, pacticipant_names)
.group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]}
.values
.collect(&:last)
.select{ |line | line[:success] }
find(criteria).select{ |line | line[:success] }
end

def find_for_version_ids version_ids, pacticipant_names = []
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/pact_broker/matrix/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@
module PactBroker
module Matrix
describe Repository do
let(:td) { TestDataBuilder.new}

describe "find" do
context "when the provider version resource exists but there is no verification for that version" do
before do
td.create_pact_with_hierarchy("A", "1.2.3", "B")
.use_provider("B")
.create_version("2.0.0")
.create_provider("C")
.create_version("3.0.0")
.create_pact
end

subject { Repository.new.find "A" => "1.2.3", "B" => "2.0.0", "C" => "3.0.0" }

it "returns a row for each pact" do
expect(subject.size).to eq 2
end

it "returns an row with a blank provider_version_number" do
expect(subject.first).to include consumer_name: "A",
provider_name: "B",
consumer_version_number: "1.2.3",
provider_version_number: nil

expect(subject.last).to include consumer_name: "A",
provider_name: "C",
consumer_version_number: "1.2.3",
provider_version_number: nil
end
end
end

describe "#find_for_consumer_and_provider" do
before do
TestDataBuilder.new
Expand Down

0 comments on commit 52755fd

Please sign in to comment.