Skip to content

Commit

Permalink
Make SQL query testing portable
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Apr 9, 2024
1 parent 0e045ee commit f9095d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ protected override IServiceCollection AddServices(IServiceCollection serviceColl
=> base.AddServices(serviceCollection)
.AddScoped<IQueryCompiler, NonCompilingQueryCompiler>();

public new RelationalTestStore TestStore
=> (RelationalTestStore)base.TestStore;

protected override async Task SeedAsync(PrecompiledQueryRelationalTestBase.PrecompiledQueryContext context)
{
context.Blogs.AddRange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
""");
Expand All @@ -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]
""");
Expand Down

0 comments on commit f9095d8

Please sign in to comment.