|
| 1 | +using EFCore.BulkExtensions; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace EntityFrameworkCore.Projectables.VendorTests; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Tests that verify Projectables is compatible with EFCore.BulkExtensions batch |
| 9 | +/// delete/update operations. |
| 10 | +/// |
| 11 | +/// Background: EFCore.BulkExtensions' <c>BatchUtil.GetDbContext</c> discovers the |
| 12 | +/// DbContext via reflection by accessing the IQueryCompiler instance stored inside |
| 13 | +/// EntityQueryProvider and then reading its private <c>_queryContextFactory</c> field. |
| 14 | +/// Because C# reflection does not surface private fields from base classes when |
| 15 | +/// GetField is called on a derived type, without an explicit shadow field in |
| 16 | +/// <c>CustomQueryCompiler</c> the lookup returns null and the next GetValue(null) |
| 17 | +/// call throws a <c>TargetException</c> ("Non-static method requires a target"). |
| 18 | +/// The shadow field added to <c>CustomQueryCompiler</c> fixes this. |
| 19 | +/// </summary> |
| 20 | +public class EFCoreBulkExtensionsCompatibilityTests : IDisposable |
| 21 | +{ |
| 22 | + private readonly TestDbContext _context; |
| 23 | + |
| 24 | + public EFCoreBulkExtensionsCompatibilityTests() |
| 25 | + { |
| 26 | + _context = new TestDbContext(); |
| 27 | + _context.Database.EnsureCreated(); |
| 28 | + _context.SeedData(); |
| 29 | + } |
| 30 | + |
| 31 | + public void Dispose() => _context.Dispose(); |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void GetDbContext_WithProjectablesEnabled_DoesNotThrow() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + var query = _context.Set<Order>().Where(o => o.IsCompleted); |
| 38 | + |
| 39 | + // Act – BatchUtil.GetDbContext is the method that was previously throwing |
| 40 | + // "Non-static method requires a target" because _queryContextFactory was not |
| 41 | + // discoverable via reflection on CustomQueryCompiler. |
| 42 | + var exception = Record.Exception(() => BatchUtil.GetDbContext(query)); |
| 43 | + |
| 44 | + // Assert |
| 45 | + Assert.Null(exception); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void GetDbContext_WithProjectablesEnabled_ReturnsCorrectContext() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + var query = _context.Set<Order>().Where(o => o.IsCompleted); |
| 53 | + |
| 54 | + // Act |
| 55 | + var dbContext = BatchUtil.GetDbContext(query); |
| 56 | + |
| 57 | + // Assert – must return the same DbContext, not null |
| 58 | + Assert.NotNull(dbContext); |
| 59 | + Assert.Same(_context, dbContext); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task BatchDeleteAsync_WithProjectablesEnabled_DoesNotThrowTargetException() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + var query = _context.Set<Order>().Where(o => o.IsCompleted); |
| 67 | + |
| 68 | + // Act – previously this would throw TargetException with message |
| 69 | + // "Non-static method requires a target" when Projectables 3.x was used. |
| 70 | +#pragma warning disable CS0618 // BatchDeleteAsync is marked obsolete in favour of EF 7 ExecuteDeleteAsync, but we |
| 71 | + // specifically need to test EFCore.BulkExtensions' own batch path. |
| 72 | + var exception = await Record.ExceptionAsync( |
| 73 | + () => query.BatchDeleteAsync(TestContext.Current.CancellationToken)); |
| 74 | +#pragma warning restore CS0618 |
| 75 | + |
| 76 | + // A TargetException means the reflection-based DbContext discovery inside |
| 77 | + // EFCore.BulkExtensions failed. Other exceptions (e.g. SQL syntax differences |
| 78 | + // on SQLite) are acceptable because they come from actual SQL execution, not |
| 79 | + // from the broken reflection chain. |
| 80 | + AssertNoTargetException(exception, "BatchDeleteAsync"); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task BatchUpdateAsync_WithProjectablesEnabled_DoesNotThrowTargetException() |
| 85 | + { |
| 86 | + // Arrange |
| 87 | + var query = _context.Set<Order>().Where(o => o.IsCompleted); |
| 88 | + |
| 89 | + // Act |
| 90 | +#pragma warning disable CS0618 // BatchUpdateAsync is marked obsolete in favour of EF 7 ExecuteUpdateAsync |
| 91 | + var exception = await Record.ExceptionAsync( |
| 92 | + () => query.BatchUpdateAsync( |
| 93 | + o => new Order { Total = o.Total * 2 }, |
| 94 | + cancellationToken: TestContext.Current.CancellationToken)); |
| 95 | +#pragma warning restore CS0618 |
| 96 | + |
| 97 | + AssertNoTargetException(exception, "BatchUpdateAsync"); |
| 98 | + } |
| 99 | + |
| 100 | + private static void AssertNoTargetException(Exception? exception, string operationName) |
| 101 | + => Assert.False( |
| 102 | + exception is System.Reflection.TargetException, |
| 103 | + $"{operationName} threw TargetException (\"Non-static method requires a target\"). " + |
| 104 | + $"This indicates that CustomQueryCompiler's _queryContextFactory shadow field is missing."); |
| 105 | +} |
0 commit comments