Skip to content

Commit

Permalink
feat(matrix): allow multiple verification statuses to be queried for …
Browse files Browse the repository at this point in the history
…using status[]=
  • Loading branch information
bethesque committed Oct 28, 2017
1 parent 2fb5817 commit fba2771
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/matrix/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 31 additions & 5 deletions spec/lib/pact_broker/matrix/parse_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ 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) }

it "extracts the pacticipant names and respective versions" do
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" }

Expand Down Expand Up @@ -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
Expand Down
63 changes: 62 additions & 1 deletion spec/lib/pact_broker/matrix/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fba2771

Please sign in to comment.