-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Throw error for unhandled derived query root expression (#26579)
Query root design Core exposes QueryRootExpression which is base class for any query root so we can access EntityType out of it. This is needed for nav expansion and any potential future use case. Since any derived query root would derive from it, core assembly translates it only when type is exact match to QueryRootExpression. Relational layer also processes QueryRootExpression when they are mapped to default SqlQuery, which also uses exact type match now. Apart from QueryRootExpression, no other kind of query root which can appear in different providers should be derivable (else they need to use exact type match too). This rule makes relational ones sealed class. In case any one needs to derive from it, they need to add additional processing anyway. Provider specific derived query roots can be non-sealed. If anyone is deriving from it then they should be using their derived provider which process those nodes too and if the derived provider wasn't used and shipped provider is used then it is an error from user perspective. If derived query root is used on other provider (targeting diff database) then it will fail since even the base shipped query root is unknown. Resolves #26502
- Loading branch information
Showing
7 changed files
with
300 additions
and
13 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,266 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
|
||
// ReSharper disable InconsistentNaming | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Query.Internal; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class QueryTest | ||
{ | ||
public static IEnumerable<object[]> IsAsyncData = new[] { new object[] { false }, new object[] { true } }; | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task AsSplitQuery_does_not_throw_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.Include(e => e.Posts).AsSplitQuery(); | ||
if (async) | ||
{ | ||
await query.ToListAsync(); | ||
} | ||
else | ||
{ | ||
query.ToList(); | ||
} | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task AsSingleQuery_does_not_throw_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.Include(e => e.Posts).AsSingleQuery(); | ||
if (async) | ||
{ | ||
await query.ToListAsync(); | ||
} | ||
else | ||
{ | ||
query.ToList(); | ||
} | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task FromSqlRaw_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.FromSqlRaw("Select 1"); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(FromSqlQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task FromSqlInterpolated_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.FromSqlInterpolated($"Select 1"); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(FromSqlQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalAsOf_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.TemporalAsOf(DateTime.Now); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalAsOfQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalAll_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.TemporalAll(); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalAllQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalBetween_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.TemporalBetween(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalBetweenQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalContainedIn_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.TemporalContainedIn(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalContainedInQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalFromTo_throws_for_InMemory(bool async) | ||
{ | ||
using var context = new InMemoryQueryContext(); | ||
var query = context.Blogs.TemporalFromTo(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalFromToQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalAsOf_throws_for_Sqlite(bool async) | ||
{ | ||
using var context = new SqliteQueryContext(); | ||
var query = context.Blogs.TemporalAsOf(DateTime.Now); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalAsOfQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalAll_throws_for_Sqlite(bool async) | ||
{ | ||
using var context = new SqliteQueryContext(); | ||
var query = context.Blogs.TemporalAll(); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalAllQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalBetween_throws_for_Sqlite(bool async) | ||
{ | ||
using var context = new SqliteQueryContext(); | ||
var query = context.Blogs.TemporalBetween(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalBetweenQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalContainedIn_throws_for_Sqlite(bool async) | ||
{ | ||
using var context = new SqliteQueryContext(); | ||
var query = context.Blogs.TemporalContainedIn(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalContainedInQueryRootExpression)), message); | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public async Task TemporalFromTo_throws_for_Sqlite(bool async) | ||
{ | ||
using var context = new SqliteQueryContext(); | ||
var query = context.Blogs.TemporalFromTo(DateTime.Now, DateTime.Now.AddDays(7)); | ||
|
||
var message = async | ||
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message | ||
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message; | ||
|
||
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(TemporalFromToQueryRootExpression)), message); | ||
} | ||
|
||
private class Blog | ||
{ | ||
public int Id { get; set; } | ||
public List<Post> Posts { get; set; } | ||
} | ||
|
||
private class Post | ||
{ | ||
public int Id { get; set; } | ||
public Blog Blog { get; set; } | ||
} | ||
|
||
private abstract class QueryContextBase : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
public DbSet<Post> Posts { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
} | ||
} | ||
|
||
private class InMemoryQueryContext : QueryContextBase | ||
{ | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder | ||
.UseInMemoryDatabase(nameof(InMemoryQueryContext)); | ||
} | ||
|
||
|
||
private class SqliteQueryContext : QueryContextBase | ||
{ | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder | ||
.UseSqlite(((RelationalTestStore)SqliteTestStoreFactory.Instance.Create(nameof(SqliteQueryContext))).ConnectionString); | ||
} | ||
|
||
private class SqlServerQueryContext : QueryContextBase | ||
{ | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder | ||
.UseSqlite(((RelationalTestStore)SqlServerTestStoreFactory.Instance.Create(nameof(SqlServerQueryContext))).ConnectionString); | ||
} | ||
} | ||
} |