-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExecuteDelete: Use Any path for unsupported query operator (#28755)
Work-around for #28745
- Loading branch information
Showing
8 changed files
with
259 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
test/EFCore.Relational.Specification.Tests/BulkUpdates/NonSharedModelBulkUpdatesTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.BulkUpdates; | ||
|
||
public abstract class NonSharedModelBulkUpdatesTestBase : NonSharedModelTestBase | ||
{ | ||
protected override string StoreName => "NonSharedModelBulkUpdatesTests"; | ||
|
||
#nullable enable | ||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public virtual async Task Delete_predicate_based_on_optional_navigation(bool async) | ||
{ | ||
var contextFactory = await InitializeAsync<Context28745>(); | ||
await AssertDelete(async, contextFactory.CreateContext, | ||
context => context.Posts.Where(p => p.Blog!.Title!.StartsWith("Arthur")), rowsAffectedCount: 1); | ||
} | ||
|
||
protected class Context28745 : DbContext | ||
{ | ||
public Context28745(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Blog> Blogs => Set<Blog>(); | ||
public DbSet<Post> Posts => Set<Post>(); | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Blog>() | ||
.HasData(new Blog { Id = 1, Title = "Arthur" }, new Blog { Id = 2, Title = "Brice" }); | ||
|
||
modelBuilder.Entity<Post>() | ||
.HasData( | ||
new { Id = 1, BlogId = 1 }, | ||
new { Id = 2, BlogId = 2 }, | ||
new { Id = 3, BlogId = 2 }); | ||
} | ||
} | ||
|
||
public class Blog | ||
{ | ||
public int Id { get; set; } | ||
public string? Title { get; set; } | ||
|
||
public virtual ICollection<Post> Posts { get; } = new List<Post>(); | ||
} | ||
|
||
public class Post | ||
{ | ||
public int Id { get; set; } | ||
public virtual Blog? Blog { get; set; } | ||
} | ||
|
||
#nullable disable | ||
|
||
|
||
#region HelperMethods | ||
|
||
public async Task AssertDelete<TContext, TResult>( | ||
bool async, | ||
Func<TContext> contextCreator, | ||
Func<TContext, IQueryable<TResult>> query, | ||
int rowsAffectedCount) | ||
where TContext : DbContext | ||
{ | ||
if (async) | ||
{ | ||
await TestHelpers.ExecuteWithStrategyInTransactionAsync( | ||
contextCreator, UseTransaction, | ||
async context => | ||
{ | ||
var processedQuery = query(context); | ||
var result = await processedQuery.ExecuteDeleteAsync(); | ||
Assert.Equal(rowsAffectedCount, result); | ||
}); | ||
} | ||
else | ||
{ | ||
TestHelpers.ExecuteWithStrategyInTransaction( | ||
contextCreator, UseTransaction, | ||
context => | ||
{ | ||
var processedQuery = query(context); | ||
var result = processedQuery.ExecuteDelete(); | ||
Assert.Equal(rowsAffectedCount, result); | ||
}); | ||
} | ||
} | ||
|
||
public async Task AssertUpdate<TContext, TResult>( | ||
bool async, | ||
Func<TContext> contextCreator, | ||
Func<TContext, IQueryable<TResult>> query, | ||
Expression<Func<SetPropertyStatements<TResult>, SetPropertyStatements<TResult>>> setPropertyStatements, | ||
int rowsAffectedCount) | ||
where TResult : class | ||
where TContext : DbContext | ||
{ | ||
if (async) | ||
{ | ||
await TestHelpers.ExecuteWithStrategyInTransactionAsync( | ||
contextCreator, UseTransaction, | ||
async context => | ||
{ | ||
var processedQuery = query(context); | ||
var result = await processedQuery.ExecuteUpdateAsync(setPropertyStatements); | ||
Assert.Equal(rowsAffectedCount, result); | ||
}); | ||
} | ||
else | ||
{ | ||
TestHelpers.ExecuteWithStrategyInTransaction( | ||
contextCreator, UseTransaction, | ||
context => | ||
{ | ||
var processedQuery = query(context); | ||
|
||
var result = processedQuery.ExecuteUpdate(setPropertyStatements); | ||
|
||
Assert.Equal(rowsAffectedCount, result); | ||
}); | ||
} | ||
} | ||
|
||
public void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction) | ||
=> facade.UseTransaction(transaction.GetDbTransaction()); | ||
|
||
protected TestSqlLoggerFactory TestSqlLoggerFactory | ||
=> (TestSqlLoggerFactory)ListLoggerFactory; | ||
|
||
protected void ClearLog() | ||
=> TestSqlLoggerFactory.Clear(); | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/EFCore.SqlServer.FunctionalTests/BulkUpdates/NonSharedModelBulkUpdatesSqlServerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.BulkUpdates; | ||
|
||
public class NonSharedModelBulkUpdatesSqlServerTest : NonSharedModelBulkUpdatesTestBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => SqlServerTestStoreFactory.Instance; | ||
|
||
[ConditionalFact] | ||
public virtual void Check_all_tests_overridden() | ||
=> TestHelpers.AssertAllMethodsOverridden(GetType()); | ||
|
||
public override async Task Delete_predicate_based_on_optional_navigation(bool async) | ||
{ | ||
await base.Delete_predicate_based_on_optional_navigation(async); | ||
|
||
AssertSql( | ||
@"DELETE FROM [p] | ||
FROM [Posts] AS [p] | ||
LEFT JOIN [Blogs] AS [b] ON [p].[BlogId] = [b].[Id] | ||
WHERE [b].[Title] IS NOT NULL AND ([b].[Title] LIKE N'Arthur%')"); | ||
} | ||
|
||
private void AssertSql(params string[] expected) | ||
=> TestSqlLoggerFactory.AssertBaseline(expected); | ||
|
||
private void AssertExecuteUpdateSql(params string[] expected) | ||
=> TestSqlLoggerFactory.AssertBaseline(expected, forUpdate: true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
test/EFCore.Sqlite.FunctionalTests/BulkUpdates/NonSharedModelBulkUpdatesSqliteTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.BulkUpdates; | ||
|
||
public class NonSharedModelBulkUpdatesSqliteTest : NonSharedModelBulkUpdatesTestBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => SqliteTestStoreFactory.Instance; | ||
|
||
[ConditionalFact] | ||
public virtual void Check_all_tests_overridden() | ||
=> TestHelpers.AssertAllMethodsOverridden(GetType()); | ||
|
||
public override async Task Delete_predicate_based_on_optional_navigation(bool async) | ||
{ | ||
await base.Delete_predicate_based_on_optional_navigation(async); | ||
|
||
AssertSql( | ||
@"DELETE FROM ""Posts"" AS ""p"" | ||
WHERE EXISTS ( | ||
SELECT 1 | ||
FROM ""Posts"" AS ""p0"" | ||
LEFT JOIN ""Blogs"" AS ""b"" ON ""p0"".""BlogId"" = ""b"".""Id"" | ||
WHERE ""b"".""Title"" IS NOT NULL AND (""b"".""Title"" LIKE 'Arthur%') AND ""p0"".""Id"" = ""p"".""Id"")"); | ||
} | ||
|
||
private void AssertSql(params string[] expected) | ||
=> TestSqlLoggerFactory.AssertBaseline(expected); | ||
|
||
private void AssertExecuteUpdateSql(params string[] expected) | ||
=> TestSqlLoggerFactory.AssertBaseline(expected, forUpdate: true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters