|
| 1 | +namespace SqlServerSimulator; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// End-to-end regression tests for EF Core's subquery emission patterns: |
| 5 | +/// LINQ <c>Any</c> against another DbSet inside a WHERE predicate |
| 6 | +/// translates to <c>EXISTS (SELECT 1 ...)</c>; LINQ <c>Contains</c> over a |
| 7 | +/// subquery projection translates to <c>IN (SELECT ...)</c>. Validates the |
| 8 | +/// simulator's correlated-subquery support against the SqlServer provider's |
| 9 | +/// actual emit shapes. |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public class EFCoreSubquery |
| 13 | +{ |
| 14 | + public TestContext TestContext { get; set; } = null!; |
| 15 | + |
| 16 | + private static TestDbContext SeededContext() |
| 17 | + { |
| 18 | + var context = new TestDbContext(TestDbContext.CreateCustomersSimulation()); |
| 19 | + context.Customers.AddRange( |
| 20 | + new Customer { Name = "alpha" }, |
| 21 | + new Customer { Name = "beta" }, |
| 22 | + new Customer { Name = "gamma" }); |
| 23 | + _ = context.SaveChanges(); |
| 24 | + // Customer ids are 1, 2, 3 after the IDENTITY assignment. |
| 25 | + context.CustomerOrders.AddRange( |
| 26 | + new CustomerOrder { CustomerId = 1, Amount = 10m }, |
| 27 | + new CustomerOrder { CustomerId = 1, Amount = 20m }, |
| 28 | + new CustomerOrder { CustomerId = 2, Amount = 30m }); |
| 29 | + _ = context.SaveChanges(); |
| 30 | + return context; |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Where_AnyCorrelated_EmitsExistsSubquery() |
| 35 | + { |
| 36 | + // EF Core translates `Any` against another DbSet to `WHERE EXISTS |
| 37 | + // (SELECT 1 FROM CustomerOrders o WHERE o.CustomerId = c.Id)`. |
| 38 | + // Customers 1 and 2 have orders; customer 3 doesn't. |
| 39 | + using var context = SeededContext(); |
| 40 | + var ids = context.Customers |
| 41 | + .Where(c => context.CustomerOrders.Any(o => o.CustomerId == c.Id)) |
| 42 | + .OrderBy(c => c.Id) |
| 43 | + .Select(c => c.Id) |
| 44 | + .ToArray(); |
| 45 | + CollectionAssert.AreEqual(new[] { 1, 2 }, ids); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Where_NotAnyCorrelated_EmitsNotExistsSubquery() |
| 50 | + { |
| 51 | + // The complement: customers without any order → only id 3. |
| 52 | + using var context = SeededContext(); |
| 53 | + var ids = context.Customers |
| 54 | + .Where(c => !context.CustomerOrders.Any(o => o.CustomerId == c.Id)) |
| 55 | + .OrderBy(c => c.Id) |
| 56 | + .Select(c => c.Id) |
| 57 | + .ToArray(); |
| 58 | + CollectionAssert.AreEqual(new[] { 3 }, ids); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void Where_ContainsSubquery_EmitsInSelect() |
| 63 | + { |
| 64 | + // EF Core translates `Contains` against a subquery to `IN (SELECT |
| 65 | + // ...)`. The subquery projects CustomerId from CustomerOrders; |
| 66 | + // customers with at least one order match. |
| 67 | + using var context = SeededContext(); |
| 68 | + var ids = context.Customers |
| 69 | + .Where(c => context.CustomerOrders.Select(o => o.CustomerId).Contains(c.Id)) |
| 70 | + .OrderBy(c => c.Id) |
| 71 | + .Select(c => c.Id) |
| 72 | + .ToArray(); |
| 73 | + CollectionAssert.AreEqual(new[] { 1, 2 }, ids); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void Where_AnyWithAdditionalPredicate_FiltersInsideSubquery() |
| 78 | + { |
| 79 | + // Inner WHERE is also correlated: only customers with an order >= 25. |
| 80 | + using var context = SeededContext(); |
| 81 | + var ids = context.Customers |
| 82 | + .Where(c => context.CustomerOrders.Any(o => o.CustomerId == c.Id && o.Amount >= 25m)) |
| 83 | + .OrderBy(c => c.Id) |
| 84 | + .Select(c => c.Id) |
| 85 | + .ToArray(); |
| 86 | + CollectionAssert.AreEqual(new[] { 2 }, ids); |
| 87 | + } |
| 88 | +} |
0 commit comments