From f9095d8c71d5de631364a5b6ac602b579dd60fa0 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 9 Apr 2024 20:37:29 +0200 Subject: [PATCH] Make SQL query testing portable --- .../Query/PrecompiledQueryRelationalFixture.cs | 3 +++ .../Query/PrecompiledQueryRelationalTestBase.cs | 10 +++++++--- .../Query/PrecompiledQuerySqlServerTest.cs | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalFixture.cs b/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalFixture.cs index d0b2cd519dc..e2ecee44abd 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalFixture.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalFixture.cs @@ -19,6 +19,9 @@ protected override IServiceCollection AddServices(IServiceCollection serviceColl => base.AddServices(serviceCollection) .AddScoped(); + public new RelationalTestStore TestStore + => (RelationalTestStore)base.TestStore; + protected override async Task SeedAsync(PrecompiledQueryRelationalTestBase.PrecompiledQueryContext context) { context.Blogs.AddRange( diff --git a/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalTestBase.cs index b9d871d01a1..50290f7c1fa 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalTestBase.cs @@ -788,14 +788,15 @@ public virtual Task Contains_with_parameterized_collection() _ = await context.Blogs.Where(b => ids.Contains(b.Id)).ToListAsync(); """); - // TODO: SQL Server-specific [ConditionalFact] public virtual Task FromSqlRaw() - => Test("""_ = await context.Blogs.FromSqlRaw("SELECT * FROM Blogs").OrderBy(b => b.Id).ToListAsync();"""); + => Test( + $""""_ = await context.Blogs.FromSqlRaw("""{NormalizeDelimitersInRawString("SELECT * FROM [Blogs] WHERE [Id] > 8")}""").OrderBy(b => b.Id).ToListAsync();""""); [ConditionalFact] public virtual Task FromSql_with_FormattableString_parameters() - => Test("""_ = await context.Blogs.FromSql($"SELECT * FROM Blogs WHERE Id > {8} AND Id < {9}").OrderBy(b => b.Id).ToListAsync();"""); + => Test( + $""""_ = await context.Blogs.FromSql($"""{NormalizeDelimitersInRawString("SELECT * FROM [Blogs] WHERE [Id] > {8} AND [Id] < {9}")}""").OrderBy(b => b.Id).ToListAsync();""""); #endregion SQL expression quotability @@ -1105,6 +1106,9 @@ protected virtual Task FullSourceTest( protected virtual bool AlwaysPrintGeneratedSources => false; + protected string NormalizeDelimitersInRawString(string sql) + => Fixture.TestStore.NormalizeDelimitersInRawString(sql); + public class Blog { public Blog() diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs index e1a67abb005..c56b17b6239 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs @@ -1538,7 +1538,7 @@ public override async Task FromSqlRaw() """ SELECT [m].[Id], [m].[Name] FROM ( - SELECT * FROM Blogs + SELECT * FROM "Blogs" WHERE "Id" > 8 ) AS [m] ORDER BY [m].[Id] """); @@ -1555,7 +1555,7 @@ public override async Task FromSql_with_FormattableString_parameters() SELECT [m].[Id], [m].[Name] FROM ( - SELECT * FROM Blogs WHERE Id > @p0 AND Id < @p1 + SELECT * FROM "Blogs" WHERE "Id" > @p0 AND "Id" < @p1 ) AS [m] ORDER BY [m].[Id] """);