|
| 1 | +namespace SqlServerSimulator; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// End-to-end tests for the LINQ predicate shapes EF Core emits via its |
| 5 | +/// SqlServer provider, exercising the simulator's full boolean-expression |
| 6 | +/// grammar (AND, OR, NOT, parens) and three-valued evaluator. These pin the |
| 7 | +/// "real-world LINQ Where queries round-trip" guarantee — the headline |
| 8 | +/// unlock from adding tri-state Run plus OR/NOT/parens to BooleanExpression. |
| 9 | +/// </summary> |
| 10 | +[TestClass] |
| 11 | +public class EFCorePredicates |
| 12 | +{ |
| 13 | + public TestContext TestContext { get; set; } = null!; |
| 14 | + |
| 15 | + private static TestDbContext SeededContext() |
| 16 | + { |
| 17 | + var context = new TestDbContext(TestDbContext.CreateFiltersSimulation()); |
| 18 | + context.Filters.AddRange( |
| 19 | + new Filter { A = 1, B = 1, NullableC = 10, IsActive = true, Status = "active" }, |
| 20 | + new Filter { A = 1, B = 2, NullableC = null, IsActive = false, Status = "pending" }, |
| 21 | + new Filter { A = 2, B = 2, NullableC = 20, IsActive = true, Status = "active" }, |
| 22 | + new Filter { A = 2, B = 3, NullableC = null, IsActive = false, Status = null }, |
| 23 | + new Filter { A = 3, B = 1, NullableC = 30, IsActive = true, Status = "archived" }); |
| 24 | + _ = context.SaveChanges(); |
| 25 | + return context; |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void Where_OrAcrossTwoColumns_FlatPredicate() |
| 30 | + { |
| 31 | + // EF Core emits `WHERE [A] = 1 OR [B] = 3`. Pre-fix the simulator |
| 32 | + // dropped the OR clause; now it returns rows matching either side. |
| 33 | + using var context = SeededContext(); |
| 34 | + var ids = context.Filters.Where(f => f.A == 1 || f.B == 3).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 35 | + CollectionAssert.AreEqual(new[] { 1, 2, 4 }, ids); |
| 36 | + } |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void Where_OrChain_StatusValueSet() |
| 40 | + { |
| 41 | + // EF Core consolidates a same-column OR-chain into `[Status] IN |
| 42 | + // (N'a', N'b')`. The simulator now parses that path. |
| 43 | + using var context = SeededContext(); |
| 44 | + var ids = context.Filters.Where(f => f.Status == "active" || f.Status == "archived").OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 45 | + CollectionAssert.AreEqual(new[] { 1, 3, 5 }, ids); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Where_ContainsArray_EmitsInList() |
| 50 | + { |
| 51 | + // EF Core's idiomatic shape for "value in a set": LINQ Contains |
| 52 | + // against an in-memory array. SqlServer provider emits `IN (...)`. |
| 53 | + using var context = SeededContext(); |
| 54 | + var wanted = new[] { "active", "archived" }; |
| 55 | + var ids = context.Filters.Where(f => wanted.Contains(f.Status)).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 56 | + CollectionAssert.AreEqual(new[] { 1, 3, 5 }, ids); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void Where_AndOrPrecedence_AndBindsTighter() |
| 61 | + { |
| 62 | + // `a == 1 || (b == 2 && active)` is what EF Core's expression |
| 63 | + // visitor will emit unparenthesized: `WHERE [A] = 1 OR [B] = 2 AND |
| 64 | + // [IsActive] = CAST(1 AS bit)`. Standard SQL precedence. |
| 65 | + using var context = SeededContext(); |
| 66 | + var ids = context.Filters.Where(f => f.A == 1 || (f.B == 2 && f.IsActive)).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 67 | + CollectionAssert.AreEqual(new[] { 1, 2, 3 }, ids); |
| 68 | + } |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void Where_ParenthesizedCompound_ExplicitGrouping() |
| 72 | + { |
| 73 | + // (A=1 OR B=3) AND IsActive — explicit parens force AND across the |
| 74 | + // whole OR group rather than only the right side. |
| 75 | + using var context = SeededContext(); |
| 76 | + var ids = context.Filters.Where(f => (f.A == 1 || f.B == 3) && f.IsActive).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 77 | + CollectionAssert.AreEqual(new[] { 1 }, ids); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void Where_NotEqualOverNullableColumn_ExcludesNull() |
| 82 | + { |
| 83 | + // EF Core emits `WHERE [NullableC] <> 20` plus a null-compensation |
| 84 | + // branch (`OR [NullableC] IS NULL` or similar). Now both halves |
| 85 | + // parse; tri-state Run gives the right answer (rows 1 and 5). |
| 86 | + using var context = SeededContext(); |
| 87 | + var ids = context.Filters.Where(f => f.NullableC != 20).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 88 | + CollectionAssert.AreEqual(new[] { 1, 2, 4, 5 }, ids); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void Where_NullableColumnIsNull() |
| 93 | + { |
| 94 | + // `f.NullableC == null` translates to `WHERE [NullableC] IS NULL`. |
| 95 | + using var context = SeededContext(); |
| 96 | + var ids = context.Filters.Where(f => f.NullableC == null).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 97 | + CollectionAssert.AreEqual(new[] { 2, 4 }, ids); |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + public void Where_NullableColumnIsNotNull() |
| 102 | + { |
| 103 | + using var context = SeededContext(); |
| 104 | + var ids = context.Filters.Where(f => f.NullableC != null).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 105 | + CollectionAssert.AreEqual(new[] { 1, 3, 5 }, ids); |
| 106 | + } |
| 107 | + |
| 108 | + [TestMethod] |
| 109 | + public void Where_NotInCompoundPredicate() |
| 110 | + { |
| 111 | + // !IsActive combined via AND with another comparison; EF Core emits |
| 112 | + // `WHERE [IsActive] = CAST(0 AS bit) AND [A] > 0` (or NOT-flavor |
| 113 | + // depending on version). Either lands on the new boolean grammar. |
| 114 | + using var context = SeededContext(); |
| 115 | + var ids = context.Filters.Where(f => !f.IsActive && f.A > 1).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 116 | + CollectionAssert.AreEqual(new[] { 4 }, ids); |
| 117 | + } |
| 118 | + |
| 119 | + [TestMethod] |
| 120 | + public void Where_KeysetPagination_OrShape() |
| 121 | + { |
| 122 | + // The keyset-pagination shape EF Core users frequently write: |
| 123 | + // `(A > val) OR (A == val AND B > val2)` — gets the next page after |
| 124 | + // a known `(A, B)` cursor. Multi-clause OR with an inner AND group. |
| 125 | + using var context = SeededContext(); |
| 126 | + var ids = context.Filters |
| 127 | + .Where(f => f.A > 1 || (f.A == 1 && f.B > 1)) |
| 128 | + .OrderBy(f => f.A).ThenBy(f => f.B) |
| 129 | + .Select(f => f.Id) |
| 130 | + .ToArray(); |
| 131 | + CollectionAssert.AreEqual(new[] { 2, 3, 4, 5 }, ids); |
| 132 | + } |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void Where_BoolColumnDirect_IncludesActiveOnly() |
| 136 | + { |
| 137 | + using var context = SeededContext(); |
| 138 | + var ids = context.Filters.Where(f => f.IsActive).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 139 | + CollectionAssert.AreEqual(new[] { 1, 3, 5 }, ids); |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void Where_NotBoolColumn_OnlyInactive() |
| 144 | + { |
| 145 | + using var context = SeededContext(); |
| 146 | + var ids = context.Filters.Where(f => !f.IsActive).OrderBy(f => f.Id).Select(f => f.Id).ToArray(); |
| 147 | + CollectionAssert.AreEqual(new[] { 2, 4 }, ids); |
| 148 | + } |
| 149 | + |
| 150 | + [TestMethod] |
| 151 | + public void Where_NullableEqualsValue_AutoExcludesNulls() |
| 152 | + { |
| 153 | + // `f.NullableC == 30` — SQL `=` on NULL returns UNKNOWN → excluded. |
| 154 | + using var context = SeededContext(); |
| 155 | + var ids = context.Filters.Where(f => f.NullableC == 30).Select(f => f.Id).ToArray(); |
| 156 | + CollectionAssert.AreEqual(new[] { 5 }, ids); |
| 157 | + } |
| 158 | +} |
0 commit comments