-
-
Notifications
You must be signed in to change notification settings - Fork 908
/
validate_uniqueness_of_matcher_spec.rb
564 lines (480 loc) · 18 KB
/
validate_uniqueness_of_matcher_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher do
context 'a model without a a uniqueness validation' do
it 'rejects' do
model = define_model(:example, attr: :string) { attr_accessible :attr } .new
Example.create!(attr: 'value')
expect(model).not_to matcher
end
end
context 'a model with a uniqueness validation' do
context 'where the subject has a character limit' do
it 'tests with values within the character limit' do
model = define_model(:example, attr: { type: :string, options: { limit: 1 } }) do
attr_accessible :attr
validates_uniqueness_of :attr
end.new
expect(model).to matcher
end
end
context 'with an existing record' do
it 'requires a unique value for that attribute' do
create_existing
expect(validating_uniqueness_with_other).to matcher
end
it 'accepts when the subject is an existing record' do
expect(create_existing).to matcher
end
it 'rejects when a scope is specified' do
create_existing
expect(validating_uniqueness_with_other).not_to matcher.scoped_to(:other)
end
def create_existing
define_model_with_other
Example.create!(attr: 'value', other: 1)
end
end
context 'without an existing record' do
it 'does not require a created instance' do
define_model_with_other
expect(Example.count).to eq 0
expect(validating_uniqueness_with_other).to matcher
end
context "and the table uses non-nullable columns, set beforehand" do
it "does not require the record to be persisted" do
model = define_model_with_non_nullable_column
record = model.new(required_attribute_name => "some value")
expect(record).to validate_uniqueness_of(unique_attribute_name)
end
def define_model_with_non_nullable_column
model = define_model(:example,
unique_attribute_name => :string,
required_attribute_name => {
type: :string,
options: { null: false }
}
)
model.tap do
model.attr_accessible(
required_attribute_name,
unique_attribute_name
)
model.validates_presence_of(required_attribute_name)
model.validates_uniqueness_of(unique_attribute_name)
end
end
def required_attribute_name
:required_attribute_name
end
def unique_attribute_name
:unique_attribute_name
end
end
end
def define_model_with_other(options = {})
@model ||= define_model(:example, attr: :string, other: :integer) do
attr_accessible :attr, :other
validates_uniqueness_of :attr, options
end
end
def validating_uniqueness_with_other(options = {})
define_model_with_other.new
end
end
context 'a model with a uniqueness validation, a custom error, and an existing record' do
it 'rejects when the actual message does not match the default message' do
expect(validating_uniqueness_with_existing_record(message: 'Bad value')).
not_to matcher
end
it 'rejects when the messages do not match' do
expect(validating_uniqueness_with_existing_record(message: 'Bad value')).
not_to matcher.with_message(/abc/)
end
it 'accepts when the messages match' do
expect(validating_uniqueness_with_existing_record(message: 'Bad value')).
to matcher.with_message(/Bad/)
end
def validating_uniqueness_with_existing_record(options = {})
model = define_model(:example, attr: :string) do
attr_accessible :attr
validates_uniqueness_of :attr, options
end.new
Example.create!(attr: 'value')
model
end
end
context 'a model with a scoped uniqueness validation with an existing value' do
it 'accepts when the correct scope is specified' do
expect(validating_scoped_uniqueness([:scope1, :scope2])).
to matcher.scoped_to(:scope1, :scope2)
end
it 'accepts when the subject is an existing record' do
define_scoped_model([:scope1, :scope2])
expect(create_existing_record).to matcher.scoped_to(:scope1, :scope2)
end
it 'rejects when too narrow of a scope is specified' do
expect(validating_scoped_uniqueness([:scope1, :scope2])).
not_to matcher.scoped_to(:scope1, :scope2, :other)
end
it 'rejects when too broad of a scope is specified' do
expect(validating_scoped_uniqueness([:scope1, :scope2])).
not_to matcher.scoped_to(:scope1)
end
it 'rejects when a different scope is specified' do
expect(validating_scoped_uniqueness([:scope1])).
not_to matcher.scoped_to(:other)
end
it 'rejects when no scope is specified' do
expect(validating_scoped_uniqueness([:scope1])).not_to matcher
end
it 'rejects when a non-existent attribute is specified as a scope' do
expect(validating_scoped_uniqueness([:scope1])).
not_to matcher.scoped_to(:fake)
end
if rails_gte_4_1?
context 'when the scoped attribute is an enum' do
it 'accepts' do
expect(validating_scoped_uniqueness_with_enum([:scope1], scope1: 0)).
to matcher.scoped_to(:scope1)
end
context 'with a nil value' do
it 'accepts' do
expect(validating_scoped_uniqueness_with_enum([:scope1], scope1: nil)).
to matcher.scoped_to(:scope1)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness_with_enum_with_two_scopes).
not_to matcher.scoped_to(:scope1, :scope2, :other)
end
end
context 'when too broad of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness_with_enum_with_two_scopes).
not_to matcher.scoped_to(:scope1)
end
end
def validating_scoped_uniqueness_with_enum_with_two_scopes
validating_scoped_uniqueness_with_enum([:scope1, :scope2], scope1: 0, scope2: 0)
end
end
end
context 'when the scoped attribute is a date' do
it "accepts" do
expect(validating_scoped_uniqueness([:scope1], :date, scope1: Date.today)).
to matcher.scoped_to(:scope1)
end
context 'with an existing record that conflicts with scope.next' do
it 'accepts' do
expect(validating_scoped_uniqueness_with_conflicting_next(:scope1, :date, scope1: Date.today)).
to matcher.scoped_to(:scope1)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness([:scope1, :scope2], :date, scope1: Date.today, scope2: Date.today)).
not_to matcher.scoped_to(:scope1, :scope2, :other)
end
end
context 'when too broad of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness([:scope1, :scope2], :date, scope1: Date.today, scope2: Date.today)).
not_to matcher.scoped_to(:scope1)
end
end
end
context 'when the scoped attribute is a datetime' do
it 'accepts' do
expect(validating_scoped_uniqueness([:scope1], :datetime, scope1: DateTime.now)).
to matcher.scoped_to(:scope1)
end
context 'with an existing record that conflicts with scope.next' do
it 'accepts' do
expect(validating_scoped_uniqueness_with_conflicting_next(:scope1, :datetime, scope1: DateTime.now)).
to matcher.scoped_to(:scope1)
end
end
context 'with a nil value' do
it 'accepts' do
expect(validating_scoped_uniqueness([:scope1], :datetime, scope1: nil)).
to matcher.scoped_to(:scope1)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness([:scope1, :scope2], :datetime, scope1: DateTime.now, scope2: DateTime.now)).
not_to matcher.scoped_to(:scope1, :scope2, :other)
end
end
context 'when too broad of a scope is specified' do
it 'rejects' do
expect(validating_scoped_uniqueness([:scope1, :scope2], :datetime, scope1: DateTime.now, scope2: DateTime.now)).
not_to matcher.scoped_to(:scope1)
end
end
end
context 'when the scoped attribute is a uuid' do
it 'accepts' do
expect(validating_scoped_uniqueness([:scope1], :uuid, scope1: SecureRandom.uuid)).
to matcher.scoped_to(:scope1)
end
context 'with an existing record that conflicts with scope.next' do
it 'accepts' do
expect(validating_scoped_uniqueness_with_conflicting_next(:scope1, :uuid, scope1: SecureRandom.uuid)).
to matcher.scoped_to(:scope1)
end
end
context 'with a nil value' do
it 'accepts' do
expect(validating_scoped_uniqueness([:scope1], :uuid, scope1: nil)).
to matcher.scoped_to(:scope1)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects' do
record = validating_scoped_uniqueness([:scope1, :scope2], :uuid,
scope1: SecureRandom.uuid,
scope2: SecureRandom.uuid
)
expect(record).not_to matcher.scoped_to(:scope1, :scope2, :other)
end
end
context 'when too broad of a scope is specified' do
it 'rejects' do
record = validating_scoped_uniqueness([:scope1, :scope2], :uuid,
scope1: SecureRandom.uuid,
scope2: SecureRandom.uuid
)
expect(record).not_to matcher.scoped_to(:scope1)
end
end
end
def create_existing_record(attributes = {})
@existing ||= create_record(attributes)
end
def create_record(attributes = {})
default_attributes = {attr: 'value', scope1: 1, scope2: 2, other: 3}
Example.create!(default_attributes.merge(attributes))
end
def define_scoped_model(scope, scope_attr_type = :integer)
define_model(:example, attr: :string, scope1: scope_attr_type,
scope2: scope_attr_type, other: :integer) do
attr_accessible :attr, :scope1, :scope2, :other
validates_uniqueness_of :attr, scope: scope
end
end
def validating_scoped_uniqueness(*args)
attributes = args.extract_options!
model = define_scoped_model(*args).new
create_existing_record(attributes)
model
end
def validating_scoped_uniqueness_with_enum(*args)
attributes = args.extract_options!
model = define_scoped_model(*args)
model.enum scope1: [:foo, :bar]
create_existing_record(attributes)
model.new
end
def validating_scoped_uniqueness_with_conflicting_next(*args)
attributes = args.extract_options!
model = define_scoped_model(*args).new
2.times do
attributes[:scope1] = attributes[:scope1].next
create_record(attributes)
end
model
end
end
context 'a model with a case-sensitive uniqueness validation on a string attribute and an existing record' do
it 'accepts a case-sensitive value for that attribute' do
expect(case_sensitive_validation_with_existing_value(:string)).
to matcher
end
it 'rejects a case-insensitive value for that attribute' do
expect(case_sensitive_validation_with_existing_value(:string)).
not_to matcher.case_insensitive
end
end
context 'a model with a case-sensitive uniqueness validation on an integer attribute with an existing value' do
it 'accepts a case-insensitive value for that attribute' do
expect(case_sensitive_validation_with_existing_value(:integer)).
to matcher.case_insensitive
end
it 'accepts a case-sensitive value for that attribute' do
expect(case_sensitive_validation_with_existing_value(:integer)).to matcher
end
end
context "when the validation allows nil" do
context "when there is an existing entry with a nil" do
it "should allow_nil" do
model = define_model_with_allow_nil
Example.create!(attr: nil)
expect(model).to matcher.allow_nil
end
end
if active_model_3_1?
context 'when the subject has a secure password' do
it 'allows nil on the attribute' do
model = define_model(:example, attr: :string, password_digest: :string) do |m|
validates_uniqueness_of :attr, allow_nil: true
has_secure_password
end.new
expect(model).to matcher.allow_nil
end
end
end
it "should create a nil and verify that it is allowed" do
model = define_model_with_allow_nil
expect(model).to matcher.allow_nil
Example.all.any?{ |instance| instance.attr.nil? }
end
def define_model_with_allow_nil
define_model(:example, attr: :integer) do
attr_accessible :attr
validates_uniqueness_of :attr, allow_nil: true
end.new
end
end
context "when the validation does not allow a nil" do
context "when there is an existing entry with a nil" do
it "should not allow_nil" do
model = define_model_without_allow_nil
Example.create!(attr: nil)
expect(model).not_to matcher.allow_nil
end
end
it "should not allow_nil" do
model = define_model_without_allow_nil
expect(model).not_to matcher.allow_nil
end
def define_model_without_allow_nil
define_model(:example, attr: :integer) do
attr_accessible :attr
validates_uniqueness_of :attr
end.new
end
end
context 'when the validation allows blank' do
context 'when there is an existing record with a blank value' do
it 'accepts' do
model = model_allowing_blank
model.create!(attribute_name => '')
expect(model.new).to matcher.allow_blank
end
end
context 'when there is not an an existing record with a blank value' do
it 'still accepts' do
expect(record_allowing_blank).to matcher.allow_blank
end
it 'automatically creates a record' do
model = model_allowing_blank
matcher.allow_blank.matches?(model.new)
record_created = model.all.any? do |instance|
instance.__send__(attribute_name).blank?
end
expect(record_created).to be true
end
end
def attribute_name
:attr
end
def model_allowing_blank
_attribute_name = attribute_name
define_model(:example, attribute_name => :string) do
attr_accessible _attribute_name
validates_uniqueness_of _attribute_name, allow_blank: true
end
end
def record_allowing_blank
model_allowing_blank.new
end
end
context 'when the validation does not allow blank' do
context 'when there is an existing entry with a blank value' do
it 'rejects' do
model = model_disallowing_blank
model.create!(attribute_name => '')
expect(model.new).not_to matcher.allow_blank
end
end
it 'should not allow_blank' do
expect(record_disallowing_blank).not_to matcher.allow_blank
end
def attribute_name
:attr
end
def model_disallowing_blank
_attribute_name = attribute_name
define_model(:example, attribute_name => :string) do
attr_accessible _attribute_name
validates_uniqueness_of _attribute_name, allow_blank: false
end
end
def record_disallowing_blank
model_disallowing_blank.new
end
end
context "when testing that a polymorphic *_type column is one of the validation scopes" do
it "sets that column to a meaningful value that works with other validations on the same column" do
user_model = define_model :user
favorite_columns = {
favoriteable_id: { type: :integer, options: { null: false } },
favoriteable_type: { type: :string, options: { null: false } }
}
favorite_model = define_model :favorite, favorite_columns do
attr_accessible :favoriteable
belongs_to :favoriteable, polymorphic: true
validates :favoriteable, presence: true
validates :favoriteable_id, uniqueness: { scope: :favoriteable_type }
end
user = user_model.create!
favorite_model.create!(favoriteable: user)
new_favorite = favorite_model.new
expect(new_favorite).
to validate_uniqueness_of(:favoriteable_id).
scoped_to(:favoriteable_type)
end
context "if the model the *_type column refers to is namespaced, and shares the last part of its name with an existing model" do
it "still works" do
define_class 'User'
define_module 'Models'
user_model = define_model 'Models::User'
favorite_columns = {
favoriteable_id: { type: :integer, options: { null: false } },
favoriteable_type: { type: :string, options: { null: false } }
}
favorite_model = define_model 'Models::Favorite', favorite_columns do
attr_accessible :favoriteable
belongs_to :favoriteable, polymorphic: true
validates :favoriteable, presence: true
validates :favoriteable_id, uniqueness: { scope: :favoriteable_type }
end
user = user_model.create!
favorite_model.create!(favoriteable: user)
new_favorite = favorite_model.new
expect(new_favorite).
to validate_uniqueness_of(:favoriteable_id).
scoped_to(:favoriteable_type)
end
end
end
def case_sensitive_validation_with_existing_value(attr_type)
model = define_model(:example, attr: attr_type) do
attr_accessible :attr
validates_uniqueness_of :attr, case_sensitive: true
end.new
if attr_type == :string
Example.create!(attr: 'value')
elsif attr_type == :integer
Example.create!(attr: 1)
else
raise 'Must be :string or :integer'
end
model
end
def matcher
validate_uniqueness_of(:attr)
end
end