From fba27714554cbee13b1d97789018593286480319 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 28 Oct 2017 21:07:39 +1100 Subject: [PATCH] feat(matrix): allow multiple verification statuses to be queried for using status[]= --- lib/pact_broker/api/resources/matrix.rb | 2 +- lib/pact_broker/matrix/parse_query.rb | 9 ++- lib/pact_broker/matrix/repository.rb | 10 ++- lib/pact_broker/matrix/service.rb | 4 +- .../pact_broker/matrix/parse_query_spec.rb | 36 +++++++++-- .../lib/pact_broker/matrix/repository_spec.rb | 63 ++++++++++++++++++- 6 files changed, 111 insertions(+), 13 deletions(-) diff --git a/lib/pact_broker/api/resources/matrix.rb b/lib/pact_broker/api/resources/matrix.rb index 3730f3a12..16ee5f721 100644 --- a/lib/pact_broker/api/resources/matrix.rb +++ b/lib/pact_broker/api/resources/matrix.rb @@ -30,7 +30,7 @@ def malformed_request? end def to_json - lines = matrix_service.find(selectors) + lines = matrix_service.find(selectors, options) PactBroker::Api::Decorators::MatrixPactDecorator.new(lines).to_json(user_options: { base_url: base_url }) end diff --git a/lib/pact_broker/matrix/parse_query.rb b/lib/pact_broker/matrix/parse_query.rb index 4d122f869..3185c968b 100644 --- a/lib/pact_broker/matrix/parse_query.rb +++ b/lib/pact_broker/matrix/parse_query.rb @@ -7,8 +7,13 @@ def self.call query params = Rack::Utils.parse_nested_query(query) selectors = (params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } } options = {} - if params['success'] - options[:success] = params['success'] == 'true' + if params.key?('success') && params['success'].is_a?(Array) + options[:success] = params['success'].collect do | value | + value == '' ? nil : value == 'true' + end + end + if params.key?('success') && params['success'].is_a?(String) + options[:success] = [params['success'] == '' ? nil : params['success'] == 'true'] end return selectors, options end diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index cda83a686..bb8689e25 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -6,12 +6,18 @@ class Repository include PactBroker::Repositories::Helpers include PactBroker::Repositories - def find selectors - find_all(selectors) + def find selectors, options = {} + lines = find_all(selectors) .group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]} .values .collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last } .flatten + + if options.key?(:success) + lines = lines.select{ |l| options[:success].include?(l[:success]) } + end + + lines end def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name diff --git a/lib/pact_broker/matrix/service.rb b/lib/pact_broker/matrix/service.rb index 64c252fe9..2fc95e579 100644 --- a/lib/pact_broker/matrix/service.rb +++ b/lib/pact_broker/matrix/service.rb @@ -8,8 +8,8 @@ module Service extend PactBroker::Repositories extend PactBroker::Services - def find criteria - matrix_repository.find criteria + def find criteria, options = {} + matrix_repository.find criteria, options end def find_for_consumer_and_provider params diff --git a/spec/lib/pact_broker/matrix/parse_query_spec.rb b/spec/lib/pact_broker/matrix/parse_query_spec.rb index dd6fb2bf6..ff131bbb2 100644 --- a/spec/lib/pact_broker/matrix/parse_query_spec.rb +++ b/spec/lib/pact_broker/matrix/parse_query_spec.rb @@ -4,7 +4,7 @@ module PactBroker module Matrix describe ParseQuery do describe ".call" do - let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9&success=true" } + let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" } subject { ParseQuery.call(query) } @@ -12,10 +12,6 @@ module Matrix expect(subject.first).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }]) end - it "extracts the options" do - expect(subject.last).to eq success: true - end - context "with spaces" do let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" } @@ -47,6 +43,36 @@ module Matrix expect(subject.last).to eq({}) end end + + context "with just one status specified" do + let(:query) { "success=true" } + it "extracts the one status" do + expect(subject.last).to eq success: [true] + end + end + + context "with an array of statuses" do + let(:query) { "success[]=true&success[]=false&success[]=" } + it "extracts the statuses" do + expect(subject.last).to eq success: [true, false, nil] + end + end + + context "with success[]=" do + let(:query) { "success[]=&foo=bar" } + + it "sets an array with a nil success" do + expect(subject.last).to eq(success: [nil]) + end + end + + context "with success=" do + let(:query) { "success=&foo=bar" } + + it "sets an array with a nil success" do + expect(subject.last).to eq(success: [nil]) + end + end end end end diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index 7c496d378..07dceeb34 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -44,13 +44,74 @@ def build_selectors(hash) end context "when only 2 version selectors are specified" do - subject { Repository.new.find build_selectors("A" => "1.2.3", "B" => "2.0.0") } + let(:selectors) { build_selectors("A" => "1.2.3", "B" => "2.0.0") } + + subject { Repository.new.find(selectors) } it "only returns 1 row" do expect(subject.size).to eq 1 end end end + + context "using the success option" do + before do + td.create_pact_with_hierarchy("A", "1.2.3", "B") + .create_verification(provider_version: "1.0.0") + .create_consumer_version("1.2.4") + .create_pact + .create_verification(provider_version: "2.0.0", success: false) + .create_consumer_version("1.2.5") + .create_pact + end + + let(:selectors) { build_selectors("A" => nil, "B" => nil) } + + subject { Repository.new.find(selectors, options) } + + context "when the success option is not set" do + let(:options) { { } } + + it "returns all rows specified by the selectors" do + expect(subject.size).to eq 3 + end + end + + context "when the success option is true" do + let(:options) { { success: [true] } } + + it "only includes successes" do + expect(subject.first[:provider_version_number]).to eq "1.0.0" + expect(subject.size).to eq 1 + end + end + + context "when the success option is false" do + let(:options) { { success: [false] } } + + it "only includes failures" do + expect(subject.first[:provider_version_number]).to eq "2.0.0" + expect(subject.size).to eq 1 + end + end + + context "when the success option is nil" do + let(:options) { { success: [nil] } } + + it "only includes unverified rows" do + expect(subject.first[:provider_version_number]).to eq nil + expect(subject.size).to eq 1 + end + end + + context "when multiple success options are specified" do + let(:options) { { success: [false, nil] } } + + it "returns all matching rows" do + expect(subject.collect{ |r| r[:provider_version_number]}).to eq [nil, "2.0.0"] + end + end + end end describe "#find_for_consumer_and_provider" do