From 52755fd1382ccae68674b71be685637d1400ebe6 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 16 Oct 2017 16:50:56 +1100 Subject: [PATCH] feat(matrix): add method to find all matrix lines for a list of versions, regardless of verification status --- lib/pact_broker/matrix/repository.rb | 30 ++++++++++------- .../lib/pact_broker/matrix/repository_spec.rb | 33 +++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 12ebab076..65d8de6cd 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -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]} @@ -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 = [] diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index eb8a40fea..052a82d8f 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -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