|
| 1 | +#if !NET10_0_OR_GREATER |
| 2 | +using ExpressiveSharp.EntityFrameworkCore.RelationalExtensions.Tests.Models; |
| 3 | +using Microsoft.Data.Sqlite; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | + |
| 6 | +namespace ExpressiveSharp.EntityFrameworkCore.RelationalExtensions.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// End-to-end integration tests for ExecuteUpdate via IRewritableQueryable. |
| 10 | +/// These prove that modern C# syntax (null-conditional, switch expressions) |
| 11 | +/// inside SetProperty value expressions works with real SQL execution — functionality |
| 12 | +/// that is impossible with normal C# expression trees. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public class ExecuteUpdateIntegrationTests |
| 16 | +{ |
| 17 | + private SqliteConnection _connection = null!; |
| 18 | + |
| 19 | + [TestInitialize] |
| 20 | + public void Setup() |
| 21 | + { |
| 22 | + _connection = new SqliteConnection("Data Source=:memory:"); |
| 23 | + _connection.Open(); |
| 24 | + } |
| 25 | + |
| 26 | + [TestCleanup] |
| 27 | + public void Cleanup() |
| 28 | + { |
| 29 | + _connection.Dispose(); |
| 30 | + } |
| 31 | + |
| 32 | + private ExecuteUpdateTestDbContext CreateContext() |
| 33 | + { |
| 34 | + var options = new DbContextOptionsBuilder<ExecuteUpdateTestDbContext>() |
| 35 | + .UseSqlite(_connection) |
| 36 | + .UseExpressives(o => o.UseRelationalExtensions()) |
| 37 | + .Options; |
| 38 | + var ctx = new ExecuteUpdateTestDbContext(options); |
| 39 | + ctx.Database.EnsureCreated(); |
| 40 | + return ctx; |
| 41 | + } |
| 42 | + |
| 43 | + private static void SeedProducts(ExecuteUpdateTestDbContext ctx) |
| 44 | + { |
| 45 | + ctx.Products.AddRange( |
| 46 | + new Product { Id = 1, Name = "Widget", Category = "A", Tag = "", Price = 150, Quantity = 10 }, |
| 47 | + new Product { Id = 2, Name = "Gadget", Category = "B", Tag = "", Price = 75, Quantity = 5 }, |
| 48 | + new Product { Id = 3, Name = "Doohickey", Category = null, Tag = "", Price = 30, Quantity = 20 }); |
| 49 | + ctx.SaveChanges(); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Basic test: verify the generator intercepts ExecuteUpdate and forwards to EF Core. |
| 54 | + /// </summary> |
| 55 | + [TestMethod] |
| 56 | + public void ExecuteUpdate_BasicConstant_Works() |
| 57 | + { |
| 58 | + using var ctx = CreateContext(); |
| 59 | + SeedProducts(ctx); |
| 60 | + |
| 61 | + var affected = ctx.ExpressiveProducts |
| 62 | + .ExecuteUpdate(s => s.SetProperty(p => p.Tag, "basic")); |
| 63 | + |
| 64 | + Assert.AreEqual(3, affected); |
| 65 | + // ExecuteUpdate bypasses change tracker — use AsNoTracking to see actual DB state |
| 66 | + var products = ctx.Products.AsNoTracking().OrderBy(p => p.Id).ToList(); |
| 67 | + Assert.AreEqual("basic", products[0].Tag); |
| 68 | + Assert.AreEqual("basic", products[1].Tag); |
| 69 | + Assert.AreEqual("basic", products[2].Tag); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Proves new capability: switch expression inside SetProperty value lambda. |
| 74 | + /// <c>o.Price switch { > 100 => "premium", > 50 => "standard", _ => "budget" }</c> |
| 75 | + /// is impossible in a normal C# expression tree context. |
| 76 | + /// </summary> |
| 77 | + [TestMethod] |
| 78 | + public void ExecuteUpdate_SwitchExpression_TranslatesToSql() |
| 79 | + { |
| 80 | + using var ctx = CreateContext(); |
| 81 | + SeedProducts(ctx); |
| 82 | + |
| 83 | + ctx.ExpressiveProducts |
| 84 | + .ExecuteUpdate(s => s.SetProperty( |
| 85 | + p => p.Tag, |
| 86 | + p => p.Price switch |
| 87 | + { |
| 88 | + > 100 => "premium", |
| 89 | + > 50 => "standard", |
| 90 | + _ => "budget" |
| 91 | + })); |
| 92 | + |
| 93 | + var products = ctx.Products.AsNoTracking().OrderBy(p => p.Id).ToList(); |
| 94 | + Assert.AreEqual("premium", products[0].Tag); // Price=150 |
| 95 | + Assert.AreEqual("standard", products[1].Tag); // Price=75 |
| 96 | + Assert.AreEqual("budget", products[2].Tag); // Price=30 |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Proves new capability: null-coalescing operator inside SetProperty value lambda. |
| 101 | + /// </summary> |
| 102 | + [TestMethod] |
| 103 | + public void ExecuteUpdate_NullCoalescing_TranslatesToSql() |
| 104 | + { |
| 105 | + using var ctx = CreateContext(); |
| 106 | + SeedProducts(ctx); |
| 107 | + |
| 108 | + ctx.ExpressiveProducts |
| 109 | + .ExecuteUpdate(s => s.SetProperty( |
| 110 | + p => p.Tag, |
| 111 | + p => p.Category ?? "UNKNOWN")); |
| 112 | + |
| 113 | + var products = ctx.Products.AsNoTracking().OrderBy(p => p.Id).ToList(); |
| 114 | + Assert.AreEqual("A", products[0].Tag); // Category="A" |
| 115 | + Assert.AreEqual("B", products[1].Tag); // Category="B" |
| 116 | + Assert.AreEqual("UNKNOWN", products[2].Tag); // Category=null |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Proves that multiple SetProperty calls with modern C# syntax work together, |
| 121 | + /// producing multiple SET clauses in a single SQL UPDATE statement. |
| 122 | + /// </summary> |
| 123 | + [TestMethod] |
| 124 | + public void ExecuteUpdate_MultipleProperties_WithModernSyntax() |
| 125 | + { |
| 126 | + using var ctx = CreateContext(); |
| 127 | + SeedProducts(ctx); |
| 128 | + |
| 129 | + ctx.ExpressiveProducts |
| 130 | + .ExecuteUpdate(s => s |
| 131 | + .SetProperty(p => p.Tag, p => p.Price switch |
| 132 | + { |
| 133 | + > 100 => "expensive", |
| 134 | + _ => "moderate" |
| 135 | + }) |
| 136 | + .SetProperty(p => p.Category, "updated")); |
| 137 | + |
| 138 | + var products = ctx.Products.AsNoTracking().OrderBy(p => p.Id).ToList(); |
| 139 | + Assert.AreEqual("expensive", products[0].Tag); // Price=150 |
| 140 | + Assert.AreEqual("updated", products[0].Category); |
| 141 | + Assert.AreEqual("moderate", products[1].Tag); // Price=75 |
| 142 | + Assert.AreEqual("updated", products[1].Category); |
| 143 | + Assert.AreEqual("moderate", products[2].Tag); // Price=30 |
| 144 | + Assert.AreEqual("updated", products[2].Category); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Proves async variant works end-to-end with modern C# syntax. |
| 149 | + /// </summary> |
| 150 | + [TestMethod] |
| 151 | + public async Task ExecuteUpdateAsync_SwitchExpression_TranslatesToSql() |
| 152 | + { |
| 153 | + using var ctx = CreateContext(); |
| 154 | + SeedProducts(ctx); |
| 155 | + |
| 156 | + await ctx.ExpressiveProducts |
| 157 | + .ExecuteUpdateAsync(s => s.SetProperty( |
| 158 | + p => p.Tag, |
| 159 | + p => p.Price switch |
| 160 | + { |
| 161 | + > 100 => "premium", |
| 162 | + > 50 => "standard", |
| 163 | + _ => "budget" |
| 164 | + })); |
| 165 | + |
| 166 | + var products = await ctx.Products.AsNoTracking().OrderBy(p => p.Id).ToListAsync(); |
| 167 | + Assert.AreEqual("premium", products[0].Tag); |
| 168 | + Assert.AreEqual("standard", products[1].Tag); |
| 169 | + Assert.AreEqual("budget", products[2].Tag); |
| 170 | + } |
| 171 | +} |
| 172 | +#endif |
0 commit comments