-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathquerying_examples.rb
881 lines (751 loc) · 37.2 KB
/
querying_examples.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
shared_examples_for "AR Model with translated scope" do |model_class_name, a1=:title, a2=:content|
let(:backend_name) { model_class.mobility_modules.first.backend_name }
let(:model_class) { model_class_name.constantize }
let(:query_scope) { model_class.i18n }
describe ".where" do
context "querying on one translated attribute" do
before do
@instance1 = model_class.create(a1 => "foo")
@instance2 = model_class.create(a1 => "bar")
@instance3 = model_class.create(a1 => "baz", published: true)
@instance4 = model_class.create(a1 => "baz", published: false)
@instance5 = model_class.create(a1 => "foo", published: true)
end
it "returns correct result searching on unique attribute value" do
expect(query_scope.where(a1 => "bar")).to eq([@instance2])
end
it "returns correct results when query matches multiple records" do
expect(query_scope.where(a1 => "foo")).to match_array([@instance1, @instance5])
end
it "returns correct result when querying on translated and untranslated attributes" do
expect(query_scope.where(a1 => "baz", published: true)).to eq([@instance3])
end
it "returns correct result when querying on nil values" do
instance = model_class.create(a1 => nil)
expect(query_scope.where(a1 => nil)).to eq([instance])
end
context "with content in different locales" do
before do
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo ja")
@ja_instance2 = model_class.create(a1 => "foo")
end
end
it "returns correct result when querying on same attribute value in different locale" do
expect(query_scope.where(a1 => "foo")).to match_array([@instance1, @instance5])
Mobility.with_locale(:ja) do
expect(query_scope.where(a1 => "foo ja")).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo")).to eq([@ja_instance2])
end
end
it "returns correct result when querying with locale option" do
expect(query_scope.where(a1 => "foo", locale: :en)).to match_array([@instance1, @instance5])
expect(query_scope.where(a1 => "foo ja", locale: :ja)).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo", locale: :ja)).to eq([@ja_instance2])
end
it "returns correct result when querying with locale option twice in separate clauses" do
@ja_instance1.update(a1 => "foo en")
expect(query_scope.where(a1 => "foo ja", locale: :ja).where(a1 => "foo en", locale: :en)).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo", locale: :ja).where(a1 => nil, locale: :en)).to eq([@ja_instance2])
end
end
context "with exists?" do
it "returns correct result searching on unique attribute value" do
aggregate_failures do
expect(query_scope.where(a1 => "bar").exists?).to eq(true)
expect(query_scope.where(a1 => "aaa").exists?).to eq(false)
end
end
end
end
context "with two translated attributes" do
before do
@instance1 = model_class.create(a1 => "foo" )
@instance2 = model_class.create(a1 => "foo", a2 => "foo content" )
@instance3 = model_class.create(a1 => "foo", a2 => "foo content", published: false)
@instance4 = model_class.create( a2 => "foo content" )
@instance5 = model_class.create(a1 => "bar", a2 => "bar content" )
@instance6 = model_class.create(a1 => "bar", published: true )
end
# @note Regression spec
it "does not modify scope in-place" do
query_scope.where(a1 => "foo")
expect(query_scope.to_sql).to eq(model_class.all.to_sql)
end
it "returns correct results querying on one attribute" do
expect(query_scope.where(a1 => "foo")).to match_array([@instance1, @instance2, @instance3])
expect(query_scope.where(a2 => "foo content")).to match_array([@instance2, @instance3, @instance4])
end
it "returns correct results querying on two attributes in single where call" do
expect(query_scope.where(a1 => "foo", a2 => "foo content")).to match_array([@instance2, @instance3])
end
it "returns correct results querying on two attributes in separate where calls" do
expect(query_scope.where(a1 => "foo").where(a2 => "foo content")).to match_array([@instance2, @instance3])
end
it "returns correct result querying on two translated attributes and untranslated attribute" do
expect(query_scope.where(a1 => "foo", a2 => "foo content", published: false)).to eq([@instance3])
end
it "works with nil values" do
expect(query_scope.where(a1 => "foo", a2 => nil)).to eq([@instance1])
expect(query_scope.where(a1 => "foo").where(a2 => nil)).to eq([@instance1])
instance = model_class.create
expect(query_scope.where(a1 => nil, a2 => nil)).to eq([instance])
end
context "with content in different locales" do
before do
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo ja", a2 => "foo content ja")
@ja_instance2 = model_class.create(a1 => "foo", a2 => "foo content" )
@ja_instance3 = model_class.create(a1 => "foo" )
@ja_instance4 = model_class.create( a2 => "foo" )
end
end
it "returns correct result when querying on same attribute values in different locale" do
expect(query_scope.where(a1 => "foo", a2 => "foo content")).to match_array([@instance2, @instance3])
expect(query_scope.where(a1 => "foo", a2 => nil)).to eq([@instance1])
Mobility.with_locale(:ja) do
expect(query_scope.where(a1 => "foo")).to match_array([@ja_instance2, @ja_instance3])
expect(query_scope.where(a1 => "foo", a2 => "foo content")).to eq([@ja_instance2])
expect(query_scope.where(a1 => "foo ja", a2 => "foo content ja")).to eq([@ja_instance1])
end
end
end
end
context "with array of values" do
before do
@instance1 = model_class.create(a1 => "foo")
@instance2 = model_class.create(a1 => "bar")
@instance3 = model_class.create(a1 => "baz")
@instance4 = model_class.create(a1 => nil)
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo")
end
end
it "returns records with matching translated attribute values" do
expect(query_scope.where(a1 => ["foo", "baz"])).to match_array([@instance1, @instance3])
expect(query_scope.where(a1 => ["foo", nil])).to match_array([@instance1, @instance4, @ja_instance1])
end
it "collapses clauses in array of values" do
instance = model_class.create(a1 => "baz")
expect(query_scope.where(a1 => ["foo", nil, nil])).to match_array([@instance1, @instance4, @ja_instance1])
expect(query_scope.where(a1 => ["foo", "foo", nil])).to match_array([@instance1, @instance4, @ja_instance1])
aggregate_failures do
expect(query_scope.where(a1 => ["foo", nil]).to_sql).to eq(query_scope.where(a1 => ["foo", nil, nil]).to_sql)
expect(query_scope.where(a1 => ["foo", nil]).to_sql).to eq(query_scope.where(a1 => ["foo", "foo", nil]).to_sql)
end
end
end
context "with single table inheritance" do
let(:sti_model) { Class.new(model_class) }
it "works with sti model" do
instance = sti_model.create(a1 => "foo")
sti_model.i18n.where(a1 => "foo")
expect(sti_model.i18n.where(a1 => "foo")).to match_array([instance])
end
end
end
describe ".not" do
before do
@instance1 = model_class.create(a1 => "foo" )
@instance2 = model_class.create(a1 => "foo", a2 => "foo content" )
@instance3 = model_class.create(a1 => "foo", a2 => "foo content", published: false)
@instance4 = model_class.create( a2 => "foo content" )
@instance5 = model_class.create(a1 => "bar", a2 => "bar content", published: true )
@instance6 = model_class.create(a1 => "bar", a2 => "baz content", published: false)
@instance7 = model_class.create( published: true )
end
# @note Regression spec
it "does not modify scope in-place" do
query_scope.where.not(a1 => nil)
expect(query_scope.to_sql).to eq(model_class.all.to_sql)
end
it "works with nil values" do
expect(query_scope.where.not(a1 => nil)).to match_array([@instance1, @instance2, @instance3, @instance5, @instance6])
expect(query_scope.where.not(a1 => nil).where.not(a2 => nil)).to match_array([@instance2, @instance3, @instance5, @instance6])
expect(query_scope.where(a1 => nil).where.not(a2 => nil)).to eq([@instance4])
end
it "returns record without translated attribute value" do
expect(query_scope.where.not(a1 => "foo")).to match_array([@instance5, @instance6])
end
it "returns record without set of translated attribute values" do
expect(query_scope.where.not(a1 => "foo", a2 => "baz content")).to match_array([@instance5])
end
it "works in combination with untranslated attributes" do
expect(query_scope.where.not(a1 => "foo", published: true)).to eq([@instance6])
end
it "works with array of values" do
instance = model_class.create(a1 => "baz")
aggregate_failures do
expect(query_scope.where.not(a1 => ["foo", "bar"])).to match_array([instance])
expect(query_scope.where.not(a1 => ["foo", nil])).to match_array([instance, @instance5, @instance6])
end
end
it "collapses clauses in array of values" do
instance = model_class.create(a1 => "baz")
expect(query_scope.where.not(a1 => ["foo", nil, nil])).to match_array([instance, @instance5, @instance6])
expect(query_scope.where.not(a1 => ["foo", "foo", nil])).to match_array([instance, @instance5, @instance6])
aggregate_failures do
expect(query_scope.where.not(a1 => ["foo", nil]).to_sql).to eq(query_scope.where.not(a1 => ["foo", nil, nil]).to_sql)
expect(query_scope.where.not(a1 => ["foo", nil]).to_sql).to eq(query_scope.where.not(a1 => ["foo", "foo", nil]).to_sql)
end
end
it "uses IN when matching array of two or more non-nil values" do
aggregate_failures "where" do
expect(query_scope.where(a1 => ["foo", "bar"]).to_sql).to match /\sIN\s/
expect(query_scope.where(a1 => ["foo", "bar", nil]).to_sql).to match /\sIN\s/
expect(query_scope.where(a1 => ["foo", nil]).to_sql).not_to match /\sIN\s/
expect(query_scope.where(a1 => "foo").to_sql).not_to match /\sIN\s/
expect(query_scope.where(a1 => nil).to_sql).not_to match /\sIN\s/
end
aggregate_failures "where not" do
expect(query_scope.where.not(a1 => ["foo", "bar"]).to_sql).to match /\sIN\s/
expect(query_scope.where.not(a1 => ["foo", "bar", nil]).to_sql).to match /\sIN\s/
expect(query_scope.where.not(a1 => ["foo", nil]).to_sql).not_to match /\sIN\s/
expect(query_scope.where.not(a1 => "foo").to_sql).not_to match /\sIN\s/
expect(query_scope.where.not(a1 => nil).to_sql).not_to match /\sIN\s/
end
end
end
describe ".order" do
let!(:i) do
[
model_class.create(a1 => "foo0"),
model_class.create(a1 => "foo2", a2 => "foo1"),
model_class.create(a1 => "foo1", a2 => "bar2"),
model_class.create(a1 => "foo3", a2 => "bar1")
]
end
it "orders records correctly with string argument" do
expect(query_scope.order(a1.to_s)).to eq([i[0], i[2], i[1], i[3]])
end
it "orders records correctly with symbol argument" do
expect(query_scope.order(a1.to_sym)).to eq([i[0], i[2], i[1], i[3]])
end
it "orders records correctly with 1-key hash argument" do
aggregate_failures "one attribute" do
expect(query_scope.order(a1 => :asc)).to eq([i[0], i[2], i[1], i[3]])
expect(query_scope.order(a1 => :desc)).to eq([i[3], i[1], i[2], i[0]])
end
end
it "orders records correctly with 2-key hash argument" do
skip "Not supported by #{backend_name}" if [:table, :key_value].include?(backend_name)
added = model_class.create(a1 => "foo2", a2 => "foo2")
expect(query_scope.order(a1 => :desc, a2 => :asc)).to eq([i[3], i[1], added, i[2], i[0]])
end
it "handles untranslated attributes" do
expect { query_scope.order(published: :desc) }.not_to raise_error
end
it "does not modify original hash" do
hash = { a1 => :asc }
expect { query_scope.order(hash) }.not_to change { hash }
end
end
describe "Arel queries" do
# Shortcut for passing block to e.g. Post.i18n
def query(*args, &block); model_class.i18n(*args, &block); end
context "single-block querying" do
let!(:i) { [
model_class.create(a1 => "foo" ),
model_class.create( ),
model_class.create( a2 => "bar"),
model_class.create( a2 => "foo"),
model_class.create(a1 => "bar" ),
model_class.create(a1 => "foo", a2 => "bar"),
model_class.create(a1 => "foo", a2 => "baz")
] }
describe "equality" do
it "handles (a EQ 'foo')" do
expect(query { __send__(a1).eq("foo") }).to match_array([i[0], *i[5..6]])
end
it "handles (a EQ NULL)" do
expect(query { __send__(a1).eq(nil) }).to match_array(i[1..3])
end
it "handles (a EQ b)" do
matching = [
model_class.create(a1 => "foo", a2 => "foo"),
model_class.create(a1 => "bar", a2 => "bar")
]
expect(query { __send__(a1).eq(__send__(a2)) }).to match_array(matching)
end
context "with locale option" do
it "handles (a EQ 'foo')" do
post1 = model_class.new(a1 => "foo en", a2 => "bar en")
Mobility.with_locale(:ja) do
post1.send("#{a1}=", "foo ja")
post1.send("#{a2}=", "bar ja")
end
post1.save
post2 = model_class.new(a1 => "baz en")
Mobility.with_locale(:'pt-BR') { post2.send("#{a1}=", "baz pt-br") }
post2.save
expect(query(locale: :en) { __send__(a1).eq("foo en") }).to match_array([post1])
expect(query(locale: :en) { __send__(a2).eq("bar en") }).to match_array([post1])
expect(query(locale: :ja) { __send__(a1).eq("foo ja") }).to match_array([post1])
expect(query(locale: :ja) { __send__(a2).eq("bar ja") }).to match_array([post1])
expect(query(locale: :en) { __send__(a1).eq("baz en") }).to match_array([post2])
expect(query(locale: :'pt-BR') { __send__(a1).eq("baz pt-br") }).to match_array([post2])
end
end
end
describe "not equal" do
it "handles (a NOT EQ 'foo')" do
expect(query { __send__(a1).not_eq("foo") }).to match_array([i[4]])
end
it "handles (a NOT EQ NULL)" do
expect(query { __send__(a1).not_eq(nil) }).to match_array([i[0], *i[4..6]])
end
context "with AND" do
it "handles ((a NOT EQ NULL) AND (b NOT EQ NULL))" do
expect(query {
__send__(a1).not_eq(nil).and(__send__(a2).not_eq(nil))
}).to match_array(i[5..6])
end
end
context "with OR" do
it "handles ((a NOT EQ NULL) OR (b NOT EQ NULL))" do
expect(query {
__send__(a1).not_eq(nil).or(__send__(a2).not_eq(nil))
}).to match_array([i[0], *i[2..6]])
end
end
end
describe "AND" do
it "handles (a AND b)" do
expect(query {
__send__(a1).eq("foo").and(__send__(a2).eq("bar"))
}).to match_array([i[5]])
end
it "handles (a AND b), where a is NULL-valued" do
expect(query {
__send__(a1).eq(nil).and(__send__(a2).eq("bar"))
}).to match_array([i[2]])
end
it "handles (a AND b), where both a and b are NULL-valued" do
expect(query {
__send__(a1).eq(nil).and(__send__(a2).eq(nil))
}).to match_array([i[1]])
end
end
describe "OR" do
it "handles (a OR b) on same attribute" do
expect(query {
__send__(a1).eq("foo").or(__send__(a1).eq("bar"))
}).to match_array([i[0], *i[4..6]])
end
it "handles (a OR b) on same attribute, where a is NULL-valued" do
expect(query {
__send__(a1).eq(nil).or(__send__(a1).eq("foo"))
}).to match_array([*i[0..3], *i[5..6]])
end
it "handles (a OR b) on two attributes" do
expect(query {
__send__(a1).eq("foo").or(__send__(a2).eq("bar"))
}).to match_array([i[0], i[2], *i[5..6]])
end
it "handles (a OR b) on two attributes, where a is NULL-valued" do
expect(query {
__send__(a1).eq(nil).or(__send__(a2).eq("bar"))
}).to match_array([*i[1..2], i[3], i[5]])
end
it "handles (a OR b) on two attributes, where both a and b are NULL-valued" do
expect(query {
__send__(a1).eq(nil).or(__send__(a2).eq(nil))
}).to match_array(i[0..4])
end
end
describe "combination of AND and OR" do
it "handles a AND (b OR c)" do
expect(query {
__send__(a1).eq("foo").and(
__send__(a2).eq("bar").or(__send__(a2).eq("baz")))
}).to match_array(i[5..6])
end
it "handles a AND (b OR c), where c is NULL-valued" do
expect(query {
__send__(a1).eq("foo").and(
__send__(a2).eq("bar").or(__send__(a2).eq(nil)))
}).to match_array([i[0], i[5]])
end
it "handles (a AND b) OR (c AND d), where b and d are NULL-valued" do
expect(query {
__send__(a1).eq("foo").or(__send__(a1).eq(nil)).and(
__send__(a2).eq("baz").or(__send__(a2).eq(nil)))
}).to match_array([*i[0..1], i[6]])
end
end
describe "LIKE/ILIKE (matches)" do
if ENV['DB'] != 'mysql'
it "includes partial string matches" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { __send__(a1).matches("foo%") }).to match_array([i[0], *i[5..6], foobar])
expect(query { __send__(a1).matches("%foo") }).to match_array([i[0], *i[5..6], barfoo])
end
end
if (ENV['DB'] == 'mysql' || ENV['DB'] == 'postgres') && ::ActiveRecord::VERSION::STRING >= '5.0'
it "works with case_sensitive option" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { __send__(a1).matches("foo%", nil, true) }).not_to include(foobar)
expect(query { __send__(a1).matches("foO%", nil, true) }).to include(foobar)
expect(query { __send__(a1).matches("%foo", nil, true) }).not_to include(barfoo)
expect(query { __send__(a1).matches("%fOo", nil, true) }).to include(barfoo)
end
end
end
describe "LOWER" do
it "matches lowercase string values" do
foobar = model_class.create(a1 => "foObar")
expect(query { __send__(a1).lower.eq("foobar") }).to match_array([foobar])
end
end
end
context "multi-block querying" do
it "combines multiple locales with non-nil values" do
post1 = model_class.new(a1 => "foo en", a2 => "bar en")
Mobility.with_locale(:ja) do
post1.send("#{a1}=", "foo ja")
post1.send("#{a2}=", "bar ja")
end
post1.save
post2 = model_class.new(a1 => "baz en")
Mobility.with_locale(:'pt-BR') { post2.send("#{a1}=", "baz pt-br") }
post2.save
aggregate_failures do
expect(
query(locale: :en) { |en|
query(locale: :ja) { |ja|
en.__send__(a1).eq("foo en").and(ja.__send__(a2).eq("bar ja"))
}
}
).to match_array([post1])
expect(
query(locale: :en) { |en|
query(locale: :'pt-BR') { |pt|
en.__send__(a1).eq("baz en").and(pt.__send__(a1).eq("baz pt-br"))
}
}
).to match_array([post2])
end
end
it "combines multiple locales with nil and non-nil values" do
post1 = model_class.new(a1 => "foo en")
Mobility.with_locale(:ja) { post1.send("#{a1}=", "foo ja") }
post1.save
post2 = model_class.create(a1 => "foo en")
expect(
query(locale: :en) { |en|
query(locale: :ja) { |ja|
en.__send__(a1).eq("foo en").and(ja.__send__(a1).eq(nil))
}
}
).to match_array([post2])
end
end
end
end
shared_examples_for "Sequel Model with translated dataset" do |model_class_name, a1=:title, a2=:content|
let(:model_class) { constantize(model_class_name) }
let(:table_name) { model_class.table_name }
let(:query_scope) { model_class.i18n }
let(:backend_name) { model_class.mobility_modules.first.backend_name }
describe ".where" do
context "querying on one translated attribute" do
before do
@instance1 = model_class.create(a1 => "foo")
@instance2 = model_class.create(a1 => "bar")
@instance3 = model_class.create(a1 => "baz", :published => true)
@instance4 = model_class.create(a1 => "baz", :published => false)
@instance5 = model_class.create(a1 => "foo", :published => true)
end
it "returns correct result searching on unique attribute value" do
expect(query_scope.where(a1 => "bar").select_all(table_name).all).to eq([@instance2])
end
it "returns correct results when query matches multiple records" do
expect(query_scope.where(a1 => "foo").select_all(table_name).all).to match_array([@instance1, @instance5])
end
it "returns correct result when querying on translated and untranslated attributes" do
expect(query_scope.where(a1 => "baz", :published => true).select_all(table_name).all).to eq([@instance3])
end
it "returns correct result when querying on nil values" do
instance = model_class.create(a1 => nil)
expect(query_scope.where(a1 => nil).select_all(table_name).all).to eq([instance])
end
context "with content in different locales" do
before do
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo ja")
@ja_instance2 = model_class.create(a1 => "foo")
end
end
it "returns correct result when querying on same attribute value in different locale" do
expect(query_scope.where(a1 => "foo").select_all(table_name).all).to match_array([@instance1, @instance5])
Mobility.with_locale(:ja) do
expect(query_scope.where(a1 => "foo ja").select_all(table_name).all).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo").select_all(table_name).all).to eq([@ja_instance2])
end
end
it "returns correct result when querying with locale option" do
expect(query_scope.where(a1 => "foo", locale: :en).select_all(table_name).all).to match_array([@instance1, @instance5])
expect(query_scope.where(a1 => "foo ja", locale: :ja).select_all(table_name).all).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo", locale: :ja).select_all(table_name).all).to eq([@ja_instance2])
end
it "returns correct result when querying with locale option twice in separate clauses" do
@ja_instance1.update(a1 => "foo en")
@ja_instance1.reload
expect(query_scope.where(a1 => "foo ja", locale: :ja).where(a1 => "foo en", locale: :en).select_all(table_name).all).to eq([@ja_instance1])
expect(query_scope.where(a1 => "foo", locale: :ja).where(a1 => nil, locale: :en).select_all(table_name).all).to eq([@ja_instance2])
end
end
end
context "with two translated attributes" do
before do
@instance1 = model_class.create(a1 => "foo" )
@instance2 = model_class.create(a1 => "foo", a2 => "foo content" )
@instance3 = model_class.create(a1 => "foo", a2 => "foo content", published: false)
@instance4 = model_class.create( a2 => "foo content" )
@instance5 = model_class.create(a1 => "bar", a2 => "bar content" )
@instance6 = model_class.create(a1 => "bar", published: true )
end
it "returns correct results querying on one attribute" do
expect(query_scope.where(a1 => "foo").select_all(table_name).all).to match_array([@instance1, @instance2, @instance3])
expect(query_scope.where(a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3, @instance4])
end
it "returns correct results querying on two attributes in single where call" do
expect(query_scope.where(a1 => "foo", a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3])
end
it "returns correct results querying on two attributes in separate where calls" do
expect(query_scope.where(a1 => "foo").where(a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3])
end
it "returns correct result querying on two translated attributes and untranslated attribute" do
expect(query_scope.where(a1 => "foo", a2 => "foo content", published: false).select_all(table_name).all).to eq([@instance3])
end
it "works with nil values" do
expect(query_scope.where(a1 => "foo", a2 => nil).select_all(table_name).all).to eq([@instance1])
expect(query_scope.where(a1 => "foo").where(a2 => nil).select_all(table_name).all).to eq([@instance1])
instance = model_class.create
expect(query_scope.where(a1 => nil, a2 => nil).select_all(table_name).all).to eq([instance])
end
context "with content in different locales" do
before do
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo ja", a2 => "foo content ja")
@ja_instance2 = model_class.create(a1 => "foo", a2 => "foo content" )
@ja_instance3 = model_class.create(a1 => "foo" )
@ja_instance4 = model_class.create( a2 => "foo" )
end
end
it "returns correct result when querying on same attribute values in different locale" do
expect(query_scope.where(a1 => "foo", a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3])
expect(query_scope.where(a1 => "foo", a2 => nil).select_all(table_name).all).to eq([@instance1])
Mobility.with_locale(:ja) do
expect(query_scope.where(a1 => "foo").select_all(table_name).all).to match_array([@ja_instance2, @ja_instance3])
expect(query_scope.where(a1 => "foo", a2 => "foo content").select_all(table_name).all).to eq([@ja_instance2])
expect(query_scope.where(a1 => "foo ja", a2 => "foo content ja").select_all(table_name).all).to eq([@ja_instance1])
end
end
end
end
context "with array of values" do
before do
@instance1 = model_class.create(a1 => "foo")
@instance2 = model_class.create(a1 => "bar")
@instance3 = model_class.create(a1 => "baz")
Mobility.with_locale(:ja) do
@ja_instance1 = model_class.create(a1 => "foo")
end
end
it "returns records with matching translated attribute values" do
expect(query_scope.where(a1 => ["foo", "baz"]).select_all(table_name).all).to match_array([@instance1, @instance3])
end
it "collapses clauses in array of values" do
expect(query_scope.where(a1 => ["foo", "foo"]).select_all(table_name).all).to match_array([@instance1])
aggregate_failures do
expect(query_scope.where(a1 => ["foo", "foo", nil]).sql).to eq(query_scope.where(a1 => ["foo", nil]).sql)
expect(query_scope.where(a1 => ["foo", nil, nil]).sql).to eq(query_scope.where(a1 => ["foo", nil]).sql)
end
end
it "uses IN when matching array of two or more non-nil values" do
aggregate_failures do
expect(query_scope.where(a1 => ["foo", "bar"]).sql).to match /\sIN\s/
expect(query_scope.where(a1 => "foo").sql).not_to match /\sIN\s/
expect(query_scope.where(a1 => nil).sql).not_to match /\sIN\s/
end
end
end
end
describe ".exclude" do
before do
@instance1 = model_class.create(a1 => "foo" )
@instance2 = model_class.create(a1 => "foo", a2 => "baz content" )
@instance3 = model_class.create(a1 => "bar", a2 => "foo content", published: false)
end
it "returns record without excluded attribute condition" do
expect(query_scope.exclude(a1 => "foo").select_all(table_name).all).to match_array([@instance3])
end
it "returns record without excluded set of attribute conditions" do
expect(query_scope.exclude(a1 => "foo", a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3])
end
it "works with nil values" do
expect(query_scope.exclude(a1 => "bar", a2 => nil).select_all(table_name).all).to match_array([@instance1, @instance2, @instance3])
expect(query_scope.exclude(a1 => "bar").exclude(a2 => nil).select_all(table_name).all).to eq([@instance2])
expect(query_scope.exclude(a1 => nil).exclude(a2 => nil).select_all(table_name).all).to match_array([@instance2, @instance3])
end
end
describe ".or" do
before do
@instance1 = model_class.create(a1 => "baz", a2 => "foo content", published: true )
@instance2 = model_class.create(a1 => "foo", a2 => "baz content", published: false)
@instance3 = model_class.create(a1 => "bar", a2 => "foo content", published: false)
end
it "returns union of queries" do
expect(query_scope.where(published: true).or(a1 => "foo").select_all(table_name).all).to match_array([@instance1, @instance2])
end
it "works with set of translated and untranslated attributes" do
# For backends that join translation tables (Table and KeyValue backends)
# this fails because the table will be inner join'ed, excluding the
# result which satisfies the second (or) condition. This is impossible to
# avoid without modification of an earlier dataset, which is probably not
# a good idea.
skip "Not supported by #{backend_name}" if [:table, :key_value].include?(backend_name)
expect(query_scope.where(a1 => "foo").or(:published => false, a2 => "foo content").select_all(table_name).all).to match_array([@instance2, @instance3])
end
end
describe "dataset queries" do
# Shortcut for passing block to e.g. Post.i18n
def query(*args, &block); model_class.i18n(*args, &block); end
context "single-block querying" do
let!(:i) { [
model_class.create(a1 => "foo" ),
model_class.create( ),
model_class.create( a2 => "bar"),
model_class.create( a2 => "foo"),
model_class.create(a1 => "bar" ),
model_class.create(a1 => "foo", a2 => "bar"),
model_class.create(a1 => "foo", a2 => "baz")
] }
describe "equality" do
it "handles (a EQ 'foo')" do
expect(query { __send__(a1) =~ "foo" }.select_all(table_name).all).to match_array([i[0], *i[5..6]])
end
it "handles (a EQ NULL)" do
expect(query { __send__(a1) =~ nil }.select_all(table_name).all).to match_array(i[1..3])
end
it "handles (a EQ b)" do
matching = [
model_class.create(a1 => "foo", a2 => "foo"),
model_class.create(a1 => "bar", a2 => "bar")
]
expect(query { __send__(a1) =~ __send__(a2) }.select_all(table_name).all).to match_array(matching)
end
context "with locale option" do
it "handles (a EQ 'foo')" do
post1 = model_class.new(a1 => "foo en", a2 => "bar en")
Mobility.with_locale(:ja) do
post1.send("#{a1}=", "foo ja")
post1.send("#{a2}=", "bar ja")
end
post1.save
post2 = model_class.new(a1 => "baz en")
Mobility.with_locale(:'pt-BR') { post2.send("#{a1}=", "baz pt-br") }
post2.save
expect(query(locale: :en) { __send__(a1) =~ "foo en" }.select_all(table_name).all).to match_array([post1])
expect(query(locale: :en) { __send__(a2) =~ "bar en" }.select_all(table_name).all).to match_array([post1])
expect(query(locale: :ja) { __send__(a1) =~ "foo ja" }.select_all(table_name).all).to match_array([post1])
expect(query(locale: :ja) { __send__(a2) =~ "bar ja" }.select_all(table_name).all).to match_array([post1])
expect(query(locale: :en) { __send__(a1) =~ "baz en" }.select_all(table_name).all).to match_array([post2])
expect(query(locale: :'pt-BR') { __send__(a1) =~ "baz pt-br" }.select_all(table_name).all).to match_array([post2])
end
end
end
describe "not equal" do
it "handles (a != 'foo')" do
expect(query { __send__(a1) !~ "foo" }.select_all(table_name).all).to match_array([i[4]])
end
# @note For sequel, we need to use +invert+ to get NOT EQ NULL
it "handles (a NOT EQ NULL)" do
expect(query { __send__(a1) =~ nil }.invert.select_all(table_name).all).to match_array([i[0], *i[4..6]])
end
end
describe "AND" do
it "handles (a AND b)" do
expect(query {
(__send__(a1) =~ "foo") & (__send__(a2) =~ "bar")
}.select_all(table_name).all).to match_array([i[5]])
end
end
describe "OR" do
it "handles (a OR b) on same attribute" do
expect(query {
(__send__(a1) =~ "foo") | (__send__(a1) =~ "bar")
}.select_all(table_name).all).to match_array([i[0], *i[4..6]])
end
it "handles (a OR b) on two attributes" do
expect(query {
(__send__(a1) =~ "foo") | (__send__(a2) =~ "bar")
}.select_all(table_name).all).to match_array([i[0], i[2], *i[5..6]])
end
end
describe "combination of AND and OR" do
it "handles a AND (b OR c)" do
expect(query {
((__send__(a1) =~ "foo") & (__send__(a2) =~ "bar")) | (__send__(a2) =~ "baz")
}.select_all(table_name).all).to match_array(i[5..6])
end
end
describe "ILIKE" do
it "matches case-insensitively, including partial string matches" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { Sequel.ilike(__send__(a1), "foo%") }.select_all(table_name).all).
to match_array([i[0], *i[5..6], foobar])
expect(query { Sequel.ilike(__send__(a1), "%foo") }.select_all(table_name).all).
to match_array([i[0], *i[5..6], barfoo])
end
end
describe "LIKE" do
it "matches case-sensitively, including partial string matches" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { Sequel.like(__send__(a1), "foo%") }.select_all(table_name).all).
not_to include(foobar)
expect(query { Sequel.like(__send__(a1), "foO%") }.select_all(table_name).all).
to include(foobar)
expect(query { Sequel.like(__send__(a1), "%foo") }.select_all(table_name).all).
not_to include(barfoo)
expect(query { Sequel.like(__send__(a1), "%fOo") }.select_all(table_name).all).
to include(barfoo)
end
end
end
context "multi-block querying" do
it "combines multiple locales" do
post1 = model_class.new(a1 => "foo en", a2 => "bar en")
Mobility.with_locale(:ja) do
post1.send("#{a1}=", "foo ja")
post1.send("#{a2}=", "bar ja")
end
post1.save
post2 = model_class.new(a1 => "baz en")
Mobility.with_locale(:'pt-BR') { post2.send("#{a1}=", "baz pt-br") }
post2.save
aggregate_failures do
expect(
query(locale: :en) { |en|
query(locale: :ja) { |ja|
(en.__send__(a1) =~ "foo en") & (ja.__send__(a2) =~ "bar ja")
}
}.select_all(table_name).all
).to match_array([post1])
expect(
query(locale: :en) { |en|
query(locale: :'pt-BR') { |pt|
(en.__send__(a1) =~ "baz en") & (pt.__send__(a1) =~ "baz pt-br")
}
}.select_all(table_name).all
).to match_array([post2])
end
end
end
end
end