Skip to content

Commit fba2771

Browse files
committed
feat(matrix): allow multiple verification statuses to be queried for using status[]=
1 parent 2fb5817 commit fba2771

File tree

6 files changed

+111
-13
lines changed

6 files changed

+111
-13
lines changed

lib/pact_broker/api/resources/matrix.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def malformed_request?
3030
end
3131

3232
def to_json
33-
lines = matrix_service.find(selectors)
33+
lines = matrix_service.find(selectors, options)
3434
PactBroker::Api::Decorators::MatrixPactDecorator.new(lines).to_json(user_options: { base_url: base_url })
3535
end
3636

lib/pact_broker/matrix/parse_query.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ def self.call query
77
params = Rack::Utils.parse_nested_query(query)
88
selectors = (params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } }
99
options = {}
10-
if params['success']
11-
options[:success] = params['success'] == 'true'
10+
if params.key?('success') && params['success'].is_a?(Array)
11+
options[:success] = params['success'].collect do | value |
12+
value == '' ? nil : value == 'true'
13+
end
14+
end
15+
if params.key?('success') && params['success'].is_a?(String)
16+
options[:success] = [params['success'] == '' ? nil : params['success'] == 'true']
1217
end
1318
return selectors, options
1419
end

lib/pact_broker/matrix/repository.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ class Repository
66
include PactBroker::Repositories::Helpers
77
include PactBroker::Repositories
88

9-
def find selectors
10-
find_all(selectors)
9+
def find selectors, options = {}
10+
lines = find_all(selectors)
1111
.group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]}
1212
.values
1313
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
1414
.flatten
15+
16+
if options.key?(:success)
17+
lines = lines.select{ |l| options[:success].include?(l[:success]) }
18+
end
19+
20+
lines
1521
end
1622

1723
def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name

lib/pact_broker/matrix/service.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Service
88
extend PactBroker::Repositories
99
extend PactBroker::Services
1010

11-
def find criteria
12-
matrix_repository.find criteria
11+
def find criteria, options = {}
12+
matrix_repository.find criteria, options
1313
end
1414

1515
def find_for_consumer_and_provider params

spec/lib/pact_broker/matrix/parse_query_spec.rb

+31-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ module PactBroker
44
module Matrix
55
describe ParseQuery do
66
describe ".call" do
7-
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9&success=true" }
7+
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" }
88

99
subject { ParseQuery.call(query) }
1010

1111
it "extracts the pacticipant names and respective versions" do
1212
expect(subject.first).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }])
1313
end
1414

15-
it "extracts the options" do
16-
expect(subject.last).to eq success: true
17-
end
18-
1915
context "with spaces" do
2016
let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" }
2117

@@ -47,6 +43,36 @@ module Matrix
4743
expect(subject.last).to eq({})
4844
end
4945
end
46+
47+
context "with just one status specified" do
48+
let(:query) { "success=true" }
49+
it "extracts the one status" do
50+
expect(subject.last).to eq success: [true]
51+
end
52+
end
53+
54+
context "with an array of statuses" do
55+
let(:query) { "success[]=true&success[]=false&success[]=" }
56+
it "extracts the statuses" do
57+
expect(subject.last).to eq success: [true, false, nil]
58+
end
59+
end
60+
61+
context "with success[]=" do
62+
let(:query) { "success[]=&foo=bar" }
63+
64+
it "sets an array with a nil success" do
65+
expect(subject.last).to eq(success: [nil])
66+
end
67+
end
68+
69+
context "with success=" do
70+
let(:query) { "success=&foo=bar" }
71+
72+
it "sets an array with a nil success" do
73+
expect(subject.last).to eq(success: [nil])
74+
end
75+
end
5076
end
5177
end
5278
end

spec/lib/pact_broker/matrix/repository_spec.rb

+62-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,74 @@ def build_selectors(hash)
4444
end
4545

4646
context "when only 2 version selectors are specified" do
47-
subject { Repository.new.find build_selectors("A" => "1.2.3", "B" => "2.0.0") }
47+
let(:selectors) { build_selectors("A" => "1.2.3", "B" => "2.0.0") }
48+
49+
subject { Repository.new.find(selectors) }
4850

4951
it "only returns 1 row" do
5052
expect(subject.size).to eq 1
5153
end
5254
end
5355
end
56+
57+
context "using the success option" do
58+
before do
59+
td.create_pact_with_hierarchy("A", "1.2.3", "B")
60+
.create_verification(provider_version: "1.0.0")
61+
.create_consumer_version("1.2.4")
62+
.create_pact
63+
.create_verification(provider_version: "2.0.0", success: false)
64+
.create_consumer_version("1.2.5")
65+
.create_pact
66+
end
67+
68+
let(:selectors) { build_selectors("A" => nil, "B" => nil) }
69+
70+
subject { Repository.new.find(selectors, options) }
71+
72+
context "when the success option is not set" do
73+
let(:options) { { } }
74+
75+
it "returns all rows specified by the selectors" do
76+
expect(subject.size).to eq 3
77+
end
78+
end
79+
80+
context "when the success option is true" do
81+
let(:options) { { success: [true] } }
82+
83+
it "only includes successes" do
84+
expect(subject.first[:provider_version_number]).to eq "1.0.0"
85+
expect(subject.size).to eq 1
86+
end
87+
end
88+
89+
context "when the success option is false" do
90+
let(:options) { { success: [false] } }
91+
92+
it "only includes failures" do
93+
expect(subject.first[:provider_version_number]).to eq "2.0.0"
94+
expect(subject.size).to eq 1
95+
end
96+
end
97+
98+
context "when the success option is nil" do
99+
let(:options) { { success: [nil] } }
100+
101+
it "only includes unverified rows" do
102+
expect(subject.first[:provider_version_number]).to eq nil
103+
expect(subject.size).to eq 1
104+
end
105+
end
106+
107+
context "when multiple success options are specified" do
108+
let(:options) { { success: [false, nil] } }
109+
110+
it "returns all matching rows" do
111+
expect(subject.collect{ |r| r[:provider_version_number]}).to eq [nil, "2.0.0"]
112+
end
113+
end
114+
end
54115
end
55116

56117
describe "#find_for_consumer_and_provider" do

0 commit comments

Comments
 (0)