-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathPrimitiveCollectionsQueryTestBase.cs
More file actions
1180 lines (1026 loc) · 48.5 KB
/
PrimitiveCollectionsQueryTestBase.cs
File metadata and controls
1180 lines (1026 loc) · 48.5 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
namespace Microsoft.EntityFrameworkCore.Query;
public abstract class PrimitiveCollectionsQueryTestBase<TFixture> : QueryTestBase<TFixture>
where TFixture : PrimitiveCollectionsQueryTestBase<TFixture>.PrimitiveCollectionsQueryFixtureBase, new()
{
protected PrimitiveCollectionsQueryTestBase(TFixture fixture)
: base(fixture)
{
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_of_ints_Contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 10, 999 }.Contains(c.Int)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_of_nullable_ints_Contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new int?[] { 10, 999 }.Contains(c.NullableInt)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_of_nullable_ints_Contains_null(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new int?[] { null, 999 }.Contains(c.NullableInt)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Count_with_zero_values(bool async)
=> AssertQuery(
async,
// ReSharper disable once UseArrayEmptyMethod
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new int[0].Count(i => i > c.Id) == 1));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Count_with_one_value(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2 }.Count(i => i > c.Id) == 1));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Count_with_two_values(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999 }.Count(i => i > c.Id) == 1));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Count_with_three_values(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999, 1000 }.Count(i => i > c.Id) == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_zero_values(bool async)
=> AssertQuery(
async,
// ReSharper disable once UseArrayEmptyMethod
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new int[0].Contains(c.Id)),
assertEmpty: true);
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_one_value(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2 }.Contains(c.Id)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_two_values(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999 }.Contains(c.Id)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_three_values(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999, 1000 }.Contains(c.Id)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_EF_Constant(bool async)
{
var ids = new[] { 2, 999, 1000 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => EF.Constant(ids).Contains(c.Id)),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 99, 1000 }.Contains(c.Id)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_all_parameters(bool async)
{
var (i, j) = (2, 999);
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { i, j }.Contains(c.Id)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_with_constant_and_parameter(bool async)
{
var j = 999;
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, j }.Contains(c.Id)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Inline_collection_Contains_with_mixed_value_types(bool async)
{
// Note: see many nullability-related variations on this in NullSemanticsQueryTestBase
var i = 11;
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 999, i, c.Id, c.Id + c.Int }.Contains(c.Int)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Contains_as_Any_with_predicate(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999 }.Any(i => i == c.Id)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_negated_Contains_as_All(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 2, 999 }.All(i => i != c.Id)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_Count(bool async)
{
var ids = new[] { 2, 999 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ids.Count(i => i > c.Id) == 1));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_ints_Contains_int(bool async)
{
var ints = new[] { 10, 999 };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints.Contains(c.Int)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.Int)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_ints_Contains_nullable_int(bool async)
{
var ints = new int?[] { 10, 999 };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints.Contains(c.NullableInt)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.NullableInt)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_nullable_ints_Contains_int(bool async)
{
var nullableInts = new int?[] { 10, 999 };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => nullableInts.Contains(c.Int)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !nullableInts.Contains(c.Int)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_nullable_ints_Contains_nullable_int(bool async)
{
var nullableInts = new int?[] { null, 999 };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => nullableInts.Contains(c.NullableInt)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !nullableInts.Contains(c.NullableInt)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_strings_Contains_string(bool async)
{
var strings = new[] { "10", "999" };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => strings.Contains(c.String)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !strings.Contains(c.String)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_strings_Contains_nullable_string(bool async)
{
var strings = new[] { "10", "999" };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => strings.Contains(c.NullableString)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !strings.Contains(c.NullableString)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_nullable_strings_Contains_string(bool async)
{
var strings = new[] { "10", null };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => strings.Contains(c.String)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !strings.Contains(c.String)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_of_nullable_strings_Contains_nullable_string(bool async)
{
var strings = new[] { "999", null };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => strings.Contains(c.NullableString)));
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !strings.Contains(c.NullableString)));
}
// See more nullability-related tests in NullSemanticsQueryTestBase
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_of_DateTimes_Contains(bool async)
{
var dateTimes = new[]
{
new DateTime(2020, 1, 10, 12, 30, 0, DateTimeKind.Utc), new DateTime(9999, 1, 1, 0, 0, 0, DateTimeKind.Utc)
};
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => dateTimes.Contains(c.DateTime)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_of_bools_Contains(bool async)
{
var bools = new[] { true };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => bools.Contains(c.Bool)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_of_enums_Contains(bool async)
{
var enums = new[] { MyEnum.Value1, MyEnum.Value4 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => enums.Contains(c.Enum)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_null_Contains(bool async)
{
int[]? ints = null;
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints!.Contains(c.Int)),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => false),
assertEmpty: true);
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_ints_Contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Contains(10)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_nullable_ints_Contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.NullableInts.Contains(10)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_nullable_ints_Contains_null(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.NullableInts.Contains(null)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_strings_contains_null(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Strings.Contains(null)),
assertEmpty: true);
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_nullable_strings_contains_null(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.NullableStrings.Contains(null)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_of_bools_Contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Bools.Contains(true)));
// C# 14 first-class spans caused MemoryExtensions.Contains to get resolved instead of Enumerable.Contains.
// The following tests that the various overloads are all supported.
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Contains_on_Enumerable(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => Enumerable.Contains(new[] { 10, 999 }, c.Int)));
// C# 14 first-class spans caused MemoryExtensions.Contains to get resolved instead of Enumerable.Contains.
// The following tests that the various overloads are all supported.
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Contains_on_MemoryExtensions(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => MemoryExtensions.Contains(new[] { 10, 999 }, c.Int)));
// Note that we don't test EF 8/9 with .NET 10; this test is here for completeness/documentation purposes.
#if NET10_0_OR_GREATER
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Contains_with_MemoryExtensions_with_null_comparer(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => MemoryExtensions.Contains(new[] { 10, 999 }, c.Int, comparer: null)));
#endif
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Count_method(bool async)
=> AssertQuery(
async,
// ReSharper disable once UseCollectionCountProperty
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Count() == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Length(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Length == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Count_with_predicate(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Count(i => i > 1) == 2));
[ConditionalTheory] // #33932
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Where_Count(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Where(i => i > 1).Count() == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_index_int(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints[1] == 10),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Ints.Length >= 2 ? c.Ints[1] : -1) == 10));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_index_string(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Strings[1] == "10"),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Strings.Length >= 2 ? c.Strings[1] : "-1") == "10"));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_index_datetime(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(
c => c.DateTimes[1] == new DateTime(2020, 1, 10, 12, 30, 0, DateTimeKind.Utc)),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(
c => (c.DateTimes.Length >= 2 ? c.DateTimes[1] : default) == new DateTime(2020, 1, 10, 12, 30, 0, DateTimeKind.Utc)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_index_beyond_end(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints[999] == 10),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => false),
assertEmpty: true);
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Nullable_reference_column_collection_index_equals_nullable_column(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.NullableStrings[2] == c.NullableString),
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => (c.NullableStrings.Length > 2 ? c.NullableStrings[2] : default) == c.NullableString));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Non_nullable_reference_column_collection_index_equals_nullable_column(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.Strings.Any() && c.Strings[1] == c.NullableString),
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.Strings.Any() && (c.Strings.Length > 1 ? c.Strings[1] : default) == c.NullableString));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_index_Column(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { 1, 2, 3 }[c.Int] == 1),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Int <= 2 ? new[] { 1, 2, 3 }[c.Int] : -1) == 1));
// The JsonScalarExpression (ints[c.Int]) should get inferred from the column on the other side (c.Int), and that should propagate to
// ints
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_index_Column_equal_Column(bool async)
{
var ints = new[] { 0, 2, 3 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints[c.Int] == c.Int),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Int <= 2 ? ints[c.Int] : -1) == c.Int));
}
// Since the JsonScalarExpression (ints[c.Int]) is being compared to a constant, there's nothing to infer the type mapping from.
// ints should get the default type mapping for based on its CLR type.
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_index_Column_equal_constant(bool async)
{
var ints = new[] { 1, 2, 3 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints[c.Int] == 1),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Int <= 2 ? ints[c.Int] : -1) == 1));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_ElementAt(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.ElementAt(1) == 10),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => (c.Ints.Length >= 2 ? c.Ints.ElementAt(1) : -1) == 10));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Skip(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Skip(1).Count() == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Take(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Take(2).Contains(11)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Skip_Take(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Skip(1).Take(2).Contains(11)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_OrderByDescending_ElementAt(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.Ints.OrderByDescending(i => i).ElementAt(0) == 111),
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.Ints.Length > 0 && c.Ints.OrderByDescending(i => i).ElementAt(0) == 111));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Any(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Any()));
// If this test is failing because of DistinctAfterOrderByWithoutRowLimitingOperatorWarning, this is because EF warns/errors by
// default for Distinct after OrderBy (without Skip/Take); but you likely have a naturally-ordered JSON collection, where the
// ordering has been added by the provider as part of the collection translation.
// Consider overriding RelationalQueryableMethodTranslatingExpressionVisitor.IsNaturallyOrdered() to identify such naturally-ordered
// collections, exempting them from the warning.
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Distinct(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Distinct().Count() == 3));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_projection_from_top_level(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(c => c.Id).Select(c => c.Ints),
elementAsserter: (a, b) => Assert.Equivalent(a, b),
assertOrder: true);
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Join_parameter_collection(bool async)
{
var ints = new[] { 11, 111 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => c.Ints.Join(ints, i => i, j => j, (i, j) => new { I = i, J = j }).Count() == 2));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Join_ordered_column_collection(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>()
.Where(c => new[] { 11, 111 }.Join(c.Ints, i => i, j => j, (i, j) => new { I = i, J = j }).Count() == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_Concat_column_collection(bool async)
{
var ints = new[] { 11, 111 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ((IEnumerable<int>)ints).Concat(c.Ints).Count() == 2));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Union_parameter_collection(bool async)
{
var ints = new[] { 11, 111 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Union(ints).Count() == 2));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Intersect_inline_collection(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Intersect(new[] { 11, 111 }).Count() == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Inline_collection_Except_column_collection(bool async)
// Note that since the VALUES is on the left side of the set operation, it must assign column names, otherwise the column coming
// out of the set operation has undetermined naming.
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(
c => new[] { 11, 111 }.Except(c.Ints).Count(i => i % 2 == 1) == 2));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_equality_parameter_collection(bool async)
{
var ints = new[] { 1, 10 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints == ints),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.SequenceEqual(ints)));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Column_collection_Concat_parameter_collection_equality_inline_collection(bool async)
{
var ints = new[] { 1, 10 };
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Concat(ints) == new[] { 1, 11, 111, 1, 10 }),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Concat(ints).SequenceEqual(new[] { 1, 11, 111, 1, 10 })));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_equality_inline_collection(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints == new[] { 1, 10 }),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.SequenceEqual(new[] { 1, 10 })));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Column_collection_equality_inline_collection_with_parameters(bool async)
{
var (i, j) = (1, 10);
await AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints == new[] { i, j }),
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.SequenceEqual(new[] { i, j })));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_in_subquery_Count_as_compiled_query(bool async)
{
// The Skip causes a pushdown into a subquery before the Union, and so the projection on the left side of the union points to the
// subquery as its table, and not directly to the parameter's table.
// This creates an initially untyped ColumnExpression referencing the pushed-down subquery; it must also be inferred.
// Note that this must be a compiled query, since with normal queries the Skip(1) gets client-evaluated.
var compiledQuery = EF.CompileQuery(
(PrimitiveCollectionsContext context, int[] ints)
=> context.Set<PrimitiveCollectionsEntity>().Where(p => ints.Skip(1).Count(i => i > p.Id) == 1).Count());
await using var context = Fixture.CreateContext();
var ints = new[] { 10, 111 };
// TODO: Complete
var results = compiledQuery(context, ints);
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_in_subquery_Union_column_collection_as_compiled_query(bool async)
{
// The Skip causes a pushdown into a subquery before the Union, and so the projection on the left side of the union points to the
// subquery as its table, and not directly to the parameter's table.
// This creates an initially untyped ColumnExpression referencing the pushed-down subquery; it must also be inferred.
// Note that this must be a compiled query, since with normal queries the Skip(1) gets client-evaluated.
var compiledQuery = EF.CompileQuery(
(PrimitiveCollectionsContext context, int[] ints)
=> context.Set<PrimitiveCollectionsEntity>().Where(p => ints.Skip(1).Union(p.Ints).Count() == 3));
await using var context = Fixture.CreateContext();
var ints = new[] { 10, 111 };
// TODO: Complete
var results = compiledQuery(context, ints).ToList();
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_in_subquery_Union_column_collection(bool async)
{
var ints = new[] { 10, 111 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(p => ints.Skip(1).Union(p.Ints).Count() == 3));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Parameter_collection_in_subquery_Union_column_collection_nested(bool async)
{
var ints = new[] { 10, 111 };
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(
p => ints.Skip(1).Union(p.Ints.OrderBy(x => x).Skip(1).Distinct().OrderByDescending(x => x).Take(20)).Count() == 3));
}
[ConditionalFact]
public virtual void Parameter_collection_in_subquery_and_Convert_as_compiled_query()
{
var query = EF.CompileQuery(
(PrimitiveCollectionsContext context, object[] parameters)
=> context.Set<PrimitiveCollectionsEntity>().Where(p => p.String == (string)parameters[0]));
using var context = Fixture.CreateContext();
_ = query(context, new[] { "foo" }).ToList();
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Parameter_collection_in_subquery_Union_another_parameter_collection_as_compiled_query(bool async)
{
var compiledQuery = EF.CompileQuery(
(PrimitiveCollectionsContext context, int[] ints1, int[] ints2)
=> context.Set<PrimitiveCollectionsEntity>().Where(p => ints1.Skip(1).Union(ints2).Count() == 3));
await using var context = Fixture.CreateContext();
var ints1 = new[] { 10, 111 };
var ints2 = new[] { 7, 42 };
_ = compiledQuery(context, ints1, ints2).ToList();
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_in_subquery_Union_parameter_collection(bool async)
{
var ints = new[] { 10, 111 };
// The Skip causes a pushdown into a subquery before the Union. This creates an initially untyped ColumnExpression referencing the
// pushed-down subquery; it must also be inferred
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Skip(1).Union(ints).Count() == 3));
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_ints_simple(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.Ints),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, ordered: true));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_ints_ordered(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.Ints.OrderByDescending(xx => xx).ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, ordered: true));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_datetimes_filtered(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.DateTimes.Where(xx => xx.Day != 1).ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_nullable_ints_with_paging(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.NullableInts.Take(20).ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_nullable_ints_with_paging2(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.NullableInts.OrderBy(x => x).Skip(1).ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_nullable_ints_with_paging3(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.NullableInts.Skip(2).ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_ints_with_distinct(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.Ints.Distinct().ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory(Skip = "issue #31277")]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_collection_of_nullable_ints_with_distinct(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(x => x.NullableInts.Distinct().ToList()),
assertOrder: true,
elementAsserter: (e, a) => AssertCollection(e, a, elementSorter: ee => ee));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_empty_collection_of_nullables_and_collection_only_containing_nulls(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(
x => new
{
Empty = x.NullableInts.Where(x => false).ToList(), OnlyNull = x.NullableInts.Where(x => x == null).ToList(),
}),
assertOrder: true,
elementAsserter: (e, a) =>
{
AssertCollection(e.Empty, a.Empty, ordered: true);
AssertCollection(e.OnlyNull, a.OnlyNull, ordered: true);
});
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_multiple_collections(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().OrderBy(x => x.Id).Select(
x => new
{
Ints = x.Ints.ToList(),
OrderedInts = x.Ints.OrderByDescending(xx => xx).ToList(),
FilteredDateTimes = x.DateTimes.Where(xx => xx.Day != 1).ToList(),
FilteredDateTimes2 = x.DateTimes.Where(xx => xx > new DateTime(2000, 1, 1)).ToList()
}),
elementAsserter: (e, a) =>
{
AssertCollection(e.Ints, a.Ints, ordered: true);
AssertCollection(e.OrderedInts, a.OrderedInts, ordered: true);
AssertCollection(e.FilteredDateTimes, a.FilteredDateTimes, elementSorter: ee => ee);
AssertCollection(e.FilteredDateTimes2, a.FilteredDateTimes2, elementSorter: ee => ee);
},
assertOrder: true);
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Project_primitive_collections_element(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(x => x.Id < 4).OrderBy(x => x.Id).Select(
x => new
{
Indexer = x.Ints[0],
EnumerableElementAt = x.DateTimes.ElementAt(0),
QueryableElementAt = x.Strings.AsQueryable().ElementAt(1)
}),
elementAsserter: (e, a) =>
{
AssertEqual(e.Indexer, a.Indexer);
AssertEqual(e.EnumerableElementAt, a.EnumerableElementAt);
AssertEqual(e.QueryableElementAt, a.QueryableElementAt);
},
assertOrder: true);
[ConditionalTheory] // #32208, #32215
[MemberData(nameof(IsAsyncData))]
public virtual Task Nested_contains_with_Lists_and_no_inferred_type_mapping(bool async)
{
var ints = new List<int> { 1, 2, 3 };
var strings = new List<string> { "one", "two", "three" };
// Note that in this query, the outer Contains really has no type mapping, neither for its source (collection parameter), nor
// for its item (the conditional expression returns constants). The default type mapping must be applied.
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(e => strings.Contains(ints.Contains(e.Int) ? "one" : "two")));
}
[ConditionalTheory] // #32208, #32215
[MemberData(nameof(IsAsyncData))]
public virtual Task Nested_contains_with_arrays_and_no_inferred_type_mapping(bool async)
{
var ints = new[] { 1, 2, 3 };
var strings = new[] { "one", "two", "three" };
// Note that in this query, the outer Contains really has no type mapping, neither for its source (collection parameter), nor
// for its item (the conditional expression returns constants). The default type mapping must be applied.
return AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(e => strings.Contains(ints.Contains(e.Int) ? "one" : "two")));
}
public abstract class PrimitiveCollectionsQueryFixtureBase : SharedStoreFixtureBase<PrimitiveCollectionsContext>, IQueryFixtureBase
{
private PrimitiveArrayData? _expectedData;
protected override string StoreName
=> "PrimitiveCollectionsTest";
public Func<DbContext> GetContextCreator()
=> () => CreateContext();
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
=> modelBuilder.Entity<PrimitiveCollectionsEntity>().Property(p => p.Id).ValueGeneratedNever();
protected override void Seed(PrimitiveCollectionsContext context)
=> new PrimitiveArrayData(context);
public virtual ISetSource GetExpectedData()
=> _expectedData ??= new PrimitiveArrayData();
public IReadOnlyDictionary<Type, object?> EntitySorters { get; } = new Dictionary<Type, Func<object?, object?>>
{
{ typeof(PrimitiveCollectionsEntity), e => ((PrimitiveCollectionsEntity?)e)?.Id }
}.ToDictionary(e => e.Key, e => (object?)e.Value);
public IReadOnlyDictionary<Type, object> EntityAsserters { get; } = new Dictionary<Type, Action<object?, object?>>
{
{
typeof(PrimitiveCollectionsEntity), (e, a) =>
{
Assert.Equal(e == null, a == null);
if (a != null)
{
var ee = (PrimitiveCollectionsEntity)e!;
var aa = (PrimitiveCollectionsEntity)a;
Assert.Equal(ee.Id, aa.Id);
Assert.Equivalent(ee.Ints, aa.Ints, strict: true);
Assert.Equivalent(ee.Strings, aa.Strings, strict: true);
Assert.Equivalent(ee.DateTimes, aa.DateTimes, strict: true);
Assert.Equivalent(ee.Bools, aa.Bools, strict: true);
// TODO: Complete
}
}
}
}.ToDictionary(e => e.Key, e => (object)e.Value);
}
public class PrimitiveCollectionsContext : PoolableDbContext