File tree 2 files changed +31
-2
lines changed
lib/pact_broker/pacticipants
spec/lib/pact_broker/pacticipants
2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 1
1
require 'sequel'
2
2
require 'pact_broker/domain/pacticipant'
3
3
require 'pact_broker/repositories/helpers'
4
+ require 'pact_broker/error'
4
5
5
6
module PactBroker
6
7
module Pacticipants
@@ -9,7 +10,13 @@ class Repository
9
10
include PactBroker ::Repositories ::Helpers
10
11
11
12
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
13
20
end
14
21
15
22
def find_by_id id
@@ -66,6 +73,11 @@ def delete_if_orphan(pacticipant)
66
73
pacticipant . destroy
67
74
end
68
75
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
69
81
end
70
82
end
71
83
end
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ module Pacticipants
60
60
end
61
61
describe "#find_by_name" do
62
62
before do
63
- TestDataBuilder . new . create_pacticipant ( "Foo Bar" )
63
+ td . create_pacticipant ( "Foo Bar" )
64
64
end
65
65
66
66
subject { Repository . new . find_by_name ( 'foo bar' ) }
@@ -86,6 +86,23 @@ module Pacticipants
86
86
expect ( subject . name ) . to eq "Foo Bar"
87
87
end
88
88
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
89
106
end
90
107
end
91
108
You can’t perform that action at this time.
0 commit comments