Skip to content

Commit

Permalink
feat(matrix): change query params for matrix to use q[][pacticipant]=…
Browse files Browse the repository at this point in the history
…? and q[][version]=?
  • Loading branch information
bethesque committed Oct 27, 2017
1 parent 41c20c5 commit e3913f7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'cgi'
require 'rack/utils'

module PactBroker
module Matrix
class ParseQuery
def self.call query
params = CGI.parse(CGI.unescape(query))
params['pacticipant[]'].zip(params['version[]']).each_with_object({}) do | (pacticipant, version), hash |
hash[pacticipant] = version
params = Rack::Utils.parse_nested_query(query)
(params['q'] || []).each_with_object({}) do | selector, hash |
hash[selector['pacticipant']] = selector['version']
end
end
end
Expand Down
16 changes: 15 additions & 1 deletion lib/pact_broker/matrix/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ def find_compatible_pacticipant_versions criteria
def validate_selectors selectors
error_messages = []

selectors.keys.each do | pacticipant_name |
selectors.each do | pacticipant_name, version |
if pacticipant_name.nil? && version.nil?
error_messages << "Please specify the pacticipant name and version"
elsif pacticipant_name.nil?
error_messages << "Please specify the pacticipant name"
elsif version.nil?
error_messages << "Please specify the version for #{pacticipant_name}"
end
end

if selectors.values.any?(&:nil?)
error_messages << "Please specify the pacticipant version"
end

selectors.keys.compact.each do | pacticipant_name |
unless pacticipant_service.find_pacticipant_by_name(pacticipant_name)
error_messages << "Pacticipant '#{pacticipant_name}' not found"
end
Expand Down
6 changes: 4 additions & 2 deletions spec/features/get_matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
let(:path) { "/matrix" }
let(:params) do
{
pacticipant: ['Consumer', 'Provider'],
version: ['1.0.0', '4.5.6']
q: [
{ pacticipant: 'Consumer', version: '1.0.0' },
{ pacticipant: 'Provider', version: '4.5.6' }
]
}
end
let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Resources
let(:td) { TestDataBuilder.new }
let(:path) { "/matrix" }
let(:json_response_body) { JSON.parse(subject.body, symbolize_names: true) }
let(:params) { { pacticipant: ['Foo', 'Bar'], version: ['1', '2'] } }
let(:params) { {q: [{pacticipant: 'Foo', version: '1'}, {pacticipant: 'Bar', version: '2'}]} }
let(:error_messages) { [] }

subject { get path, params, {'Content-Type' => 'application/hal+json'}; last_response }
Expand Down
20 changes: 18 additions & 2 deletions spec/lib/pact_broker/matrix/parse_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PactBroker
module Matrix
describe ParseQuery do
describe ".call" do
let(:query) { "pacticipant[]=Foo&pacticipant[]=Bar&version[]=1.2.3&version[]=9.9.9" }
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" }

subject { ParseQuery.call(query) }

Expand All @@ -13,12 +13,28 @@ module Matrix
end

context "with spaces" do
let(:query) { "pacticipant%5B%5D=Name%20With%20Spaces&version%5B%5D=1%202" }
let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" }

it "works" do
expect(subject).to eq "Name With Spaces" => "1 2"
end
end

context "with no q" do
let(:query) { "foo" }

it "returns an empty hash" do
expect(subject).to eq({})
end
end

context "with an incorrect param names" do
let(:query) { "q[][wrong]=Foo&q[][blah]=1.2.3" }

it "returns nil keys or values" do
expect(subject).to eq nil => nil
end
end
end
end
end
Expand Down
25 changes: 24 additions & 1 deletion spec/lib/pact_broker/matrix/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,31 @@ module Matrix
expect(subject.first).to eq "Pacticipant 'Foo' not found"
end
end

context "when the pacticipant name is not specified" do
let(:selectors) { {nil => "1"} }

it "returns error messages" do
expect(subject.first).to eq "Please specify the pacticipant name"
end
end

context "when the pacticipant version is not specified" do
let(:selectors) { {'Foo' => nil} }

it "returns error messages" do
expect(subject.first).to eq "Please specify the version for Foo"
end
end

context "when the pacticipant name and version are not specified" do
let(:selectors) { {nil => nil} }

it "returns error messages" do
expect(subject.first).to eq "Please specify the pacticipant name and version"
end
end
end
end
end
end

0 comments on commit e3913f7

Please sign in to comment.