Skip to content

Commit 2db5979

Browse files
committed
fix: correct logic for finding pacticipants by name when the name contains an underscore
1 parent 108a01f commit 2db5979

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/pact_broker/pacticipants/repository.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'sequel'
22
require 'pact_broker/domain/pacticipant'
33
require 'pact_broker/repositories/helpers'
4+
require 'pact_broker/error'
45

56
module PactBroker
67
module Pacticipants
@@ -9,7 +10,13 @@ class Repository
910
include PactBroker::Repositories::Helpers
1011

1112
def find_by_name name
12-
PactBroker::Domain::Pacticipant.where(name_like(:name, name)).single_record
13+
if PactBroker.configuration.use_case_sensitive_resource_names
14+
PactBroker::Domain::Pacticipant.where(name: name).single_record
15+
else
16+
pacticipants = PactBroker::Domain::Pacticipant.where(name_like(:name, name)).all
17+
handle_multiple_pacticipants_found(name, pacticipants) if pacticipants.size > 1
18+
pacticipants.first
19+
end
1320
end
1421

1522
def find_by_id id
@@ -66,6 +73,11 @@ def delete_if_orphan(pacticipant)
6673
pacticipant.destroy
6774
end
6875
end
76+
77+
def handle_multiple_pacticipants_found(name, pacticipants)
78+
names = pacticipants.collect(&:name).join(", ")
79+
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")
80+
end
6981
end
7082
end
7183
end

spec/lib/pact_broker/pacticipants/repository_spec.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module Pacticipants
6060
end
6161
describe "#find_by_name" do
6262
before do
63-
TestDataBuilder.new.create_pacticipant("Foo Bar")
63+
td.create_pacticipant("Foo Bar")
6464
end
6565

6666
subject { Repository.new.find_by_name('foo bar') }
@@ -86,6 +86,23 @@ module Pacticipants
8686
expect(subject.name).to eq "Foo Bar"
8787
end
8888
end
89+
90+
context "with case sensitivity turned off and multiple records found" do
91+
before do
92+
td.create_pacticipant("Foo bar")
93+
allow(PactBroker.configuration).to receive(:use_case_sensitive_resource_names).and_return(false)
94+
end
95+
96+
it "raises an error" do
97+
expect { subject }.to raise_error PactBroker::Error, /Found multiple pacticipants.*foo bar/
98+
end
99+
end
100+
101+
context "with case sensitivity turned off no record found" do
102+
subject { Repository.new.find_by_name('blah') }
103+
104+
it { is_expected.to be nil }
105+
end
89106
end
90107
end
91108

0 commit comments

Comments
 (0)