Skip to content

Commit 18c9d71

Browse files
committed
Format SQL output
Add a static flag to enable SQL formatting for SQL Server models and apply SqlFormatter to query SQL before returning it. Update verified SQL fixtures to match the new formatted output (lowercase keywords, no brackets, aligned columns). This ensures consistent, readable SQL output for EF query tests.
1 parent 99510e5 commit 18c9d71

5 files changed

Lines changed: 22 additions & 13 deletions

File tree

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,12 @@ Will result in the following verified files:
467467
<!-- snippet: CoreTests.Queryable.verified.sql -->
468468
<a id='snippet-CoreTests.Queryable.verified.sql'></a>
469469
```sql
470-
SELECT [c].[Id], [c].[Name]
471-
FROM [Companies] AS [c]
472-
WHERE [c].[Name] = N'company name'
470+
select c.Id,
471+
c.Name
472+
from Companies as c
473+
where c.Name = N'company name'
473474
```
474-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.Queryable.verified.sql#L1-L3' title='Snippet source file'>snippet source</a> | <a href='#snippet-CoreTests.Queryable.verified.sql' title='Start of snippet'>anchor</a></sup>
475+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.Queryable.verified.sql#L1-L4' title='Snippet source file'>snippet source</a> | <a href='#snippet-CoreTests.Queryable.verified.sql' title='Start of snippet'>anchor</a></sup>
475476
<!-- endSnippet -->
476477

477478

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
SELECT [companies].[Id], [companies].[Name]
2-
FROM [Companies] AS [companies]
3-
WHERE [companies].[Name] = N'company name'
1+
select companies.Id,
2+
companies.Name
3+
from Companies as companies
4+
where companies.Name = N'company name'
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
SELECT [c].[Id], [c].[Name]
2-
FROM [Companies] AS [c]
3-
WHERE [c].[Name] = N'company name'
1+
select c.Id,
2+
c.Name
3+
from Companies as c
4+
where c.Name = N'company name'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
SELECT [c].[Id]
2-
FROM [Companies] AS [c]
1+
select c.Id
2+
from Companies as c

src/Verify.EntityFramework/VerifyEntityFramework.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public static class VerifyEntityFramework
44
{
55
static List<(Type type, string name)>? modelNavigations;
6+
static bool formatSql;
67

78
public static async IAsyncEnumerable<object> AllData(this DbContext data)
89
{
@@ -125,7 +126,7 @@ public static void Initialize(IModel? model = null)
125126
VerifierSettings.RegisterFileConverter(
126127
QueryableToSql,
127128
(target, _) => QueryableConverter.IsQueryable(target));
128-
var formatSql = model != null && model.IsSqlServer();
129+
formatSql = model != null && model.IsSqlServer();
129130
VerifierSettings.IgnoreMembersWithType(typeof(IDbContextFactory<>));
130131
VerifierSettings.IgnoreMembersWithType<DbContext>();
131132
var converters = DefaultContractResolver.Converters;
@@ -145,6 +146,11 @@ static ConversionResult QueryableToSql(object arg, IReadOnlyDictionary<string, o
145146
var queryable = (IQueryable) arg;
146147

147148
var sql = queryable.ToQueryString();
149+
if (formatSql)
150+
{
151+
sql = SqlFormatter.Format(sql).ToString();
152+
}
153+
148154
QueryableConverter.TryExecuteQueryable(queryable, out var result);
149155
return new(result, [new("sql", sql)]);
150156
}

0 commit comments

Comments
 (0)