Skip to content

Commit

Permalink
fix: correct logic for finding pacticipants by name when the name con…
Browse files Browse the repository at this point in the history
…tains an underscore
  • Loading branch information
bethesque committed Jan 15, 2020
1 parent 108a01f commit 2db5979
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'sequel'
require 'pact_broker/domain/pacticipant'
require 'pact_broker/repositories/helpers'
require 'pact_broker/error'

module PactBroker
module Pacticipants
Expand All @@ -9,7 +10,13 @@ class Repository
include PactBroker::Repositories::Helpers

def find_by_name name
PactBroker::Domain::Pacticipant.where(name_like(:name, name)).single_record
if PactBroker.configuration.use_case_sensitive_resource_names
PactBroker::Domain::Pacticipant.where(name: name).single_record
else
pacticipants = PactBroker::Domain::Pacticipant.where(name_like(:name, name)).all
handle_multiple_pacticipants_found(name, pacticipants) if pacticipants.size > 1
pacticipants.first
end
end

def find_by_id id
Expand Down Expand Up @@ -66,6 +73,11 @@ def delete_if_orphan(pacticipant)
pacticipant.destroy
end
end

def handle_multiple_pacticipants_found(name, pacticipants)
names = pacticipants.collect(&:name).join(", ")
raise PactBroker::Error.new("Found multiple pacticipants with a case insensitive name match for '#{name}': #{names}. Please delete one of them, or set PactBroker.configuration.use_case_sensitive_resource_names = true")
end
end
end
end
19 changes: 18 additions & 1 deletion spec/lib/pact_broker/pacticipants/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module Pacticipants
end
describe "#find_by_name" do
before do
TestDataBuilder.new.create_pacticipant("Foo Bar")
td.create_pacticipant("Foo Bar")
end

subject { Repository.new.find_by_name('foo bar') }
Expand All @@ -86,6 +86,23 @@ module Pacticipants
expect(subject.name).to eq "Foo Bar"
end
end

context "with case sensitivity turned off and multiple records found" do
before do
td.create_pacticipant("Foo bar")
allow(PactBroker.configuration).to receive(:use_case_sensitive_resource_names).and_return(false)
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Error, /Found multiple pacticipants.*foo bar/
end
end

context "with case sensitivity turned off no record found" do
subject { Repository.new.find_by_name('blah') }

it { is_expected.to be nil }
end
end
end

Expand Down

0 comments on commit 2db5979

Please sign in to comment.