-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
validate_uniqueness_of_matcher_spec.rb
1675 lines (1417 loc) · 57 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher, type: :model do
shared_context 'it supports scoped attributes of a certain type' do |options = {}|
column_type = options.fetch(:column_type)
value_type = options.fetch(:value_type, column_type)
array = options.fetch(:array, false)
context 'when the correct scope is specified' do
context 'when the subject is a new record' do
it 'accepts' do
record = build_record_validating_uniqueness(
scopes: [
build_attribute(name: :scope1),
{ name: :scope2 },
],
)
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2)
end
it 'still accepts if the scope is unset beforehand' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :scope, value: nil)],
)
expect(record).to validate_uniqueness.scoped_to(:scope)
end
end
context 'when the subject is an existing record' do
it 'accepts' do
record = create_record_validating_uniqueness(
scopes: [
build_attribute(name: :scope1),
{ name: :scope2 },
],
)
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2)
end
it 'still accepts if the scope is unset beforehand' do
record = create_record_validating_uniqueness(
scopes: [build_attribute(name: :scope, value: nil)],
)
expect(record).to validate_uniqueness.scoped_to(:scope)
end
end
end
context "when more than one record exists that has the next version of the attribute's value" do
it 'accepts' do
value1 = dummy_value_for(value_type, array: array)
value2 = next_version_of(value1, value_type)
value3 = next_version_of(value2, value_type)
model = define_model_validating_uniqueness(
scopes: [build_attribute(name: :scope)],
)
create_record_from(model, scope: value2)
create_record_from(model, scope: value3)
record = build_record_from(model, scope: value1)
expect(record).to validate_uniqueness.scoped_to(:scope)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
scopes: [
build_attribute(name: :scope1),
build_attribute(name: :scope2),
],
additional_attributes: [:other],
)
assertion = lambda do
expect(record).
to validate_uniqueness.
scoped_to(:scope1, :scope2, :other)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :scope1, :scope2, and :other, but this could not be
proved.
Expected the validation to be scoped to :scope1, :scope2, and :other,
but it was scoped to :scope1 and :scope2 instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when too broad of a scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
scopes: [
build_attribute(name: :scope1),
build_attribute(name: :scope2),
],
)
assertion = lambda do
expect(record).
to validate_uniqueness.
scoped_to(:scope1)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :scope1, but this could not be proved.
Expected the validation to be scoped to :scope1, but it was scoped to
:scope1 and :scope2 instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when a different scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :other)],
additional_attributes: [:scope],
)
assertion = lambda do
expect(record).
to validate_uniqueness.
scoped_to(:scope)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :scope, but this could not be proved.
Expected the validation to be scoped to :scope, but it was scoped to
:other instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when no scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :scope)],
)
assertion = lambda do
expect(record).to validate_uniqueness
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
Expected the validation not to be scoped to anything, but it was
scoped to :scope instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
context 'if the scope attribute is unset in the record given to the matcher' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :scope, value: nil)],
)
assertion = lambda do
expect(record).to validate_uniqueness
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
Expected the validation not to be scoped to anything, but it was
scoped to :scope instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
context 'when a non-existent attribute is specified as a scope' do
context 'when there is more than one scope' do
it 'rejects with an appropriate failure message (and does not raise an error)' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :scope)],
)
assertion = lambda do
expect(record).to validate_uniqueness.scoped_to(:non_existent)
end
message = <<-MESSAGE.strip
Expected Example to validate that :attr is case-sensitively unique
within the scope of :non_existent, but this could not be proved.
:non_existent does not seem to be an attribute on Example.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when there is more than one scope' do
it 'rejects with an appropriate failure message (and does not raise an error)' do
record = build_record_validating_uniqueness(
scopes: [build_attribute(name: :scope)],
)
assertion = lambda do
expect(record).to validate_uniqueness.scoped_to(
:non_existent1,
:non_existent2,
)
end
message = <<-MESSAGE.strip
Expected Example to validate that :attr is case-sensitively unique
within the scope of :non_existent1 and :non_existent2, but this could
not be proved.
:non_existent1 and :non_existent2 do not seem to be attributes on
Example.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
context 'when there is more than one validation on the same attribute with different scopes' do
context 'when a record exists beforehand, where all scopes are set' do
if column_type != :boolean
context 'when each validation has the same (default) message' do
# this needs another qualifier to properly fix
# it 'accepts' do
# model = define_model(
# 'Example',
# attribute_name => :string,
# scope1: column_type,
# scope2: column_type
# ) do |m|
# m.validates_uniqueness_of(attribute_name, scope: [:scope1])
# m.validates_uniqueness_of(attribute_name, scope: [:scope2])
# end
# model.create!(
# attribute_name => dummy_value_for(:string),
# scope1: dummy_value_for(column_type),
# scope2: dummy_value_for(column_type)
# )
# expect(model.new).to validate_uniqueness.scoped_to(:scope1)
# expect(model.new).to validate_uniqueness.scoped_to(:scope2)
# end
end
end
context 'when each validation has a different message' do
it 'accepts' do
model = define_model(
'Example',
attribute_name => :string,
scope1: column_type,
scope2: column_type,
) do |m|
m.validates_uniqueness_of(
attribute_name,
scope: [:scope1],
message: 'first message',
)
m.validates_uniqueness_of(
attribute_name,
scope: [:scope2],
message: 'second message',
)
end
model.create!(
attribute_name => dummy_value_for(:string),
scope1: dummy_value_for(column_type),
scope2: dummy_value_for(column_type),
)
expect(model.new).
to validate_uniqueness.
scoped_to(:scope1).
with_message('first message')
expect(model.new).
to validate_uniqueness.
scoped_to(:scope2).
with_message('second message')
end
end
end
context 'when no record exists beforehand' do
# this needs another qualifier to properly fix
# it 'accepts' do
# model = define_model(
# 'Example',
# attribute_name => :string,
# scope1: column_type,
# scope2: column_type
# ) do |m|
# m.validates_uniqueness_of(attribute_name, scope: [:scope1])
# m.validates_uniqueness_of(attribute_name, scope: [:scope2])
# end
# expect(model.new).to validate_uniqueness.scoped_to(:scope1)
# expect(model.new).to validate_uniqueness.scoped_to(:scope2)
# end
end
end
define_method(:build_attribute) do |attribute_options|
attribute_options.deep_merge(
column_type: column_type,
value_type: value_type,
options: { array: array },
)
end
end
context 'when the model does not have a uniqueness validation' do
it 'rejects with an appropriate failure message' do
model = define_model_without_validation
model.create!(attribute_name => 'value')
assertion = lambda do
expect(model.new).to validate_uniqueness_of(attribute_name)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
Given an existing Example whose :attr is ‹"value"›, after making a new
Example and setting its :attr to ‹"value"› as well, the matcher
expected the new Example to be invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when the model has a uniqueness validation' do
context 'when the attribute has a character limit' do
it 'accepts' do
record = build_record_validating_uniqueness(
attribute_type: :string,
attribute_options: { limit: 1 },
)
expect(record).to validate_uniqueness
end
end
context 'when the attribute is of integer type' do
it 'accepts' do
record = build_record_validating_uniqueness(
attribute_type: :integer,
attribute_options: { limit: 4 },
)
expect(record).to validate_uniqueness
end
end
context 'when the existing record was created beforehand' do
context 'when the subject is a new record' do
it 'accepts' do
create_record_validating_uniqueness
expect(new_record_validating_uniqueness).
to validate_uniqueness
end
end
context 'when the subject is itself the existing record' do
it 'accepts' do
expect(existing_record_validating_uniqueness).to validate_uniqueness
end
end
end
context 'when the existing record was not created beforehand' do
context 'and the subject is empty' do
context 'and the attribute being tested is required' do
it 'can save the subject without the attribute being set' do
options = { attribute_name: :attr }
model = define_model_validating_uniqueness(options) do |m|
m.validates_presence_of :attr
end
record = model.new
expect(record).to validate_uniqueness
end
end
context 'and the attribute being tested are required along with other attributes' do
it 'can save the subject without the attributes being set' do
options = {
attribute_name: :attr,
additional_attributes: [:required_attribute],
}
model = define_model_validating_uniqueness(options) do |m|
m.validates_presence_of :attr
m.validates_presence_of :required_attribute
end
expect(model.new).to validate_uniqueness
end
end
context 'and the attribute being tested has other validations on it' do
it 'can save the subject without it being completely valid' do
options = { attribute_name: :attr }
model = define_model_validating_uniqueness(options) do |m|
m.validates_presence_of :attr
m.validates_numericality_of :attr
end
expect(model.new).to validate_uniqueness
end
end
context 'and the table has non-nullable columns other than the attribute being validated' do
context 'which are set beforehand' do
it 'can save the subject' do
options = {
additional_attributes: [
{ name: :required_attribute, options: { null: false } },
],
}
model = define_model_validating_uniqueness(options)
record = model.new
record.required_attribute = 'something'
expect(record).to validate_uniqueness
end
end
context 'which are not set beforehand' do
it 'raises a useful exception' do
options = {
additional_attributes: [
{ name: :required_attribute, options: { null: false } },
],
}
model = define_model_validating_uniqueness(options)
assertion = lambda do
expect(model.new).to validate_uniqueness
end
expect(&assertion).to raise_error(
described_class::ExistingRecordInvalid,
)
end
end
end
context 'and the model has required attributes other than the attribute being validated' do
it 'can save the subject without the attributes being set' do
options = {
additional_attributes: [:required_attribute],
}
model = define_model_validating_uniqueness(options) do |m|
m.validates_presence_of :required_attribute
end
expect(model.new).to validate_uniqueness
end
end
end
context 'and the subject is not empty' do
it 'creates the record automatically from the subject' do
model = define_model_validating_uniqueness
assertion = -> {
record = build_record_from(model)
expect(record).to validate_uniqueness
}
expect(&assertion).to change(model, :count).from(0).to(1)
end
context 'and the table has required attributes other than the attribute being validated, set beforehand' do
it 'can save the subject' do
options = {
additional_attributes: [
{ name: :required_attribute, options: { null: false } },
],
}
model = define_model_validating_uniqueness(options)
record = build_record_from(model, required_attribute: 'something')
expect(record).to validate_uniqueness
end
end
context 'and the model has required attributes other than the attribute being validated, set beforehand' do
it 'can save the subject' do
options = {
additional_attributes: [:required_attribute],
}
model = define_model_validating_uniqueness(options) do |m|
m.validates_presence_of :required_attribute
end
record = build_record_from(model, required_attribute: 'something')
expect(record).to validate_uniqueness
end
end
end
end
context 'when the validation has no scope and a scope is specified' do
it 'rejects with an appropriate failure message' do
model = define_model_validating_uniqueness(
additional_attributes: [:other],
)
create_record_from(model)
record = build_record_from(model)
assertion = lambda do
expect(record).to validate_uniqueness.scoped_to(:other)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :other, but this could not be proved.
Expected the validation to be scoped to :other, but it was not scoped
to anything.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'and the validation has a custom message' do
context 'when no message is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
attribute_value: 'some value',
validation_options: { message: 'bad value' },
)
assertion = lambda do
expect(record).to validate_uniqueness
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
After taking the given Example, whose :attr is ‹"some value"›, and
saving it as the existing record, then making a new Example and
setting its :attr to ‹"some value"› as well, the matcher expected the
new Example to be invalid and to produce the validation error "has
already been taken" on :attr. The record was indeed invalid, but it
produced these validation errors instead:
* attr: ["bad value"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'given a string' do
context 'when the given and actual messages do not match' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
attribute_value: 'some value',
validation_options: { message: 'something else entirely' },
)
assertion = lambda do
expect(record).
to validate_uniqueness.
with_message('some message')
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique,
producing a custom validation error on failure, but this could not be
proved.
After taking the given Example, whose :attr is ‹"some value"›, and
saving it as the existing record, then making a new Example and
setting its :attr to ‹"some value"› as well, the matcher expected the
new Example to be invalid and to produce the validation error "some
message" on :attr. The record was indeed invalid, but it produced
these validation errors instead:
* attr: ["something else entirely"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when the given and actual messages match' do
it 'accepts' do
record = build_record_validating_uniqueness(
validation_options: { message: 'bad value' },
)
expect(record).
to validate_uniqueness.
with_message('bad value')
end
end
end
context 'given a regex' do
context 'when the given and actual messages do not match' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
attribute_value: 'some value',
validation_options: { message: 'something else entirely' },
)
assertion = lambda do
expect(record).
to validate_uniqueness.
with_message(/some message/)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique,
producing a custom validation error on failure, but this could not be
proved.
After taking the given Example, whose :attr is ‹"some value"›, and
saving it as the existing record, then making a new Example and
setting its :attr to ‹"some value"› as well, the matcher expected the
new Example to be invalid and to produce a validation error matching
‹/some message/› on :attr. The record was indeed invalid, but it
produced these validation errors instead:
* attr: ["something else entirely"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when the given and actual messages match' do
it 'accepts' do
record = build_record_validating_uniqueness(
validation_options: { message: 'bad value' },
)
expect(record).
to validate_uniqueness.
with_message(/bad/)
end
end
end
end
it_supports(
'ignoring_interference_by_writer',
tests: {
reject_if_qualified_but_changing_value_interferes: {
model_name: 'Example',
attribute_name: :attr,
default_value: 'some value',
changing_values_with: :next_value,
expected_message: <<-MESSAGE.strip,
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
After taking the given Example, whose :attr is ‹"some valuf"›, and
saving it as the existing record, then making a new Example and
setting its :attr to ‹"some valuf"› (read back as ‹"some valug"›) as
well, the matcher expected the new Example to be invalid, but it was
valid instead.
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you or something else has overridden the
writer method for this attribute to normalize values by changing their
case in any way (for instance, ensuring that the attribute is always
downcased), then try adding `ignoring_case_sensitivity` onto the end
of the uniqueness matcher. Otherwise, you may need to write the test
yourself, or do something different altogether.
MESSAGE
},
},
)
it 'fails when used in the negative' do
assertion = lambda do
record = build_record_validating_uniqueness(
attribute_type: :string,
attribute_options: { limit: 1 },
)
expect(record).not_to validate_uniqueness
end
message = <<-MESSAGE
Expected Example not to validate that :attr is case-sensitively unique,
but this could not be proved.
After taking the given Example, setting its :attr to ‹"x"›, and saving
it as the existing record, then making a new Example and setting its
:attr to a different value, ‹"X"›, the matcher expected the new
Example to be invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when the model has a scoped uniqueness validation' do
context 'when one of the scoped attributes is a string column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :string
end
context 'when one of the scoped attributes is a boolean column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :boolean
end
context 'when there is more than one scoped attribute and all are boolean columns' do
it 'accepts when all of the scoped attributes are true' do
record = build_record_validating_uniqueness(
scopes: [
{ type: :boolean, name: :scope1, value: true },
{ type: :boolean, name: :scope2, value: true },
],
)
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2)
end
it 'accepts when all the scoped attributes are false' do
record = build_record_validating_uniqueness(
scopes: [
{ type: :boolean, name: :scope1, value: false },
{ type: :boolean, name: :scope2, value: false },
],
)
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2)
end
it 'accepts when one of the scoped attributes is true and the other is false' do
record = build_record_validating_uniqueness(
scopes: [
{ type: :boolean, name: :scope1, value: true },
{ type: :boolean, name: :scope2, value: false },
],
)
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2)
end
end
context 'when one of the scoped attributes is an integer column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :integer
context 'when one of the scoped attributes is an enum' do
it 'accepts' do
record = build_record_validating_scoped_uniqueness_with_enum(
enum_scope: :scope,
)
expect(record).to validate_uniqueness.scoped_to(:scope)
end
context 'when too narrow of a scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_scoped_uniqueness_with_enum(
enum_scope: :scope1,
additional_scopes: [:scope2],
additional_attributes: [:other],
)
assertion = lambda do
expect(record).
to validate_uniqueness.
scoped_to(:scope1, :scope2, :other)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :scope1, :scope2, and :other, but this could not be
proved.
Expected the validation to be scoped to :scope1, :scope2, and :other,
but it was scoped to :scope1 and :scope2 instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when too broad of a scope is specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_scoped_uniqueness_with_enum(
enum_scope: :scope1,
additional_scopes: [:scope2],
)
assertion = lambda do
expect(record).to validate_uniqueness.scoped_to(:scope1)
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique
within the scope of :scope1, but this could not be proved.
Expected the validation to be scoped to :scope1, but it was scoped to
:scope1 and :scope2 instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
end
context 'when one of the scoped attributes is a date column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :date
end
context 'when one of the scoped attributes is a datetime column (using DateTime)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :datetime
end
context 'when one of the scoped attributes is a timestamp column (using DateTime)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :timestamp
end
context 'when one of the scoped attributes is a timestamp with time zone column (using DateTime)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :timestamptz
end
context 'when one of the scoped attributes is a time column (using Time)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :time
end
context 'when one of the scoped attributes is a datetime column (using Time)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :datetime,
value_type: :time
end
context 'when one of the scoped attributes is a text column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :text
end
if database_supports_uuid_columns?
context 'when one of the scoped attributes is a UUID column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :uuid
end
end
context 'when an existing record that is not the first has a nil value for the scoped attribute' do
# This fails intermittently
# it 'still works' do
# model = define_model_validating_uniqueness(scopes: [:scope])
# create_record_from(model, scope: 'some value')
# create_record_from(model, scope: nil)
# record = build_record_from(model, scope: 'a different value')
# expect(record).to validate_uniqueness.scoped_to(:scope)
# end
end
end
context 'when the model has a case-sensitive validation' do
context 'when the matcher is not qualified with case_insensitive' do
it 'accepts' do
record = build_record_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: true },
)
expect(record).to validate_uniqueness
end
context 'given an existing record where the value of the attribute under test is not case-swappable' do
it 'raises a NonCaseSwappableValueError' do
model = define_model_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: true },
)
record = create_record_from(model, attribute_name => '123')
running_matcher = -> { validate_uniqueness.matches?(record) }
expect(&running_matcher).
to raise_error(described_class::NonCaseSwappableValueError)
end
end
end
context 'when the matcher is qualified with case_insensitive' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
attribute_type: :string,
attribute_value: 'some value',
validation_options: { case_sensitive: true },
)
assertion = lambda do
expect(record).to validate_uniqueness.case_insensitive
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-insensitively unique,
but this could not be proved.
After taking the given Example, whose :attr is ‹"some value"›, and
saving it as the existing record, then making a new Example and
setting its :attr to a different value, ‹"SOME VALUE"›, the matcher
expected the new Example to be invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
context 'when the model has a case-insensitive validation' do
context 'when case_insensitive is not specified' do
it 'rejects with an appropriate failure message' do
record = build_record_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: false },
)
assertion = lambda do
expect(record).to validate_uniqueness
end
message = <<-MESSAGE
Expected Example to validate that :attr is case-sensitively unique, but
this could not be proved.
After taking the given Example, setting its :attr to ‹"dummy value"›,
and saving it as the existing record, then making a new Example and
setting its :attr to a different value, ‹"DUMMY VALUE"›, the matcher
expected the new Example to be valid, but it was invalid instead,
producing these validation errors:
* attr: ["has already been taken"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'when case_insensitive is specified' do
it 'accepts' do
record = build_record_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: false },
)
expect(record).to validate_uniqueness.case_insensitive
end
it_supports(
'ignoring_interference_by_writer',
tests: {
reject_if_qualified_but_changing_value_interferes: {
model_name: 'Example',
attribute_name: :attr,
default_value: 'some value',
changing_values_with: :next_value,
expected_message: <<-MESSAGE.strip,
Expected Example to validate that :attr is case-insensitively unique,
but this could not be proved.
After taking the given Example, whose :attr is ‹"some valuf"›, and
saving it as the existing record, then making a new Example and
setting its :attr to ‹"some valuf"› (read back as ‹"some valug"›) as
well, the matcher expected the new Example to be invalid, but it was
valid instead.
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you or something else has overridden the
writer method for this attribute to normalize values by changing their
case in any way (for instance, ensuring that the attribute is always
downcased), then try adding `ignoring_case_sensitivity` onto the end
of the uniqueness matcher. Otherwise, you may need to write the test
yourself, or do something different altogether.
MESSAGE
},
},
)
def validation_matcher_scenario_args
super.deep_merge(validation_options: { case_sensitive: false })
end