Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,23 @@ await Verify(target)
<!-- endSnippet -->


## DisableSqlFormatting

By default SQL captured against SQL Server is reformatted via [SqlFormatter](https://github.com/SimonCropp/SqlFormatter) before being written to the snapshot. This applies to both [Recording](#recording) output and [Queryable](#queryable) `.sql` files.

Reformatting can be disabled globally:

<!-- snippet: DisableSqlFormatting -->
<a id='snippet-DisableSqlFormatting'></a>
```cs
VerifyEntityFramework.DisableSqlFormatting = true;
```
<sup><a href='/src/Verify.EntityFramework.StaticSettingsTests/StaticSettingsTests.cs#L20-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableSqlFormatting' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

When disabled, the SQL is written verbatim as produced by EntityFramework.


## Icon

[Database](https://thenounproject.com/term/database/310841/) designed by [Creative Stall](https://thenounproject.com/creativestall/) from [The Noun Project](https://thenounproject.com).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using System.ComponentModel.DataAnnotations.Schema;
global using Argon;
global using Microsoft.EntityFrameworkCore;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore.Metadata;

public static class ModuleInitializer
{
static IModel GetDbModel()
{
var options = new DbContextOptionsBuilder<SampleDbContext>();
options.UseSqlServer("fake");
using var data = new SampleDbContext(options.Options);
return data.Model;
}

[ModuleInitializer]
public static void Init()
{
var model = GetDbModel();
VerifyEntityFramework.Initialize(model);
VerifierSettings.InitializePlugins();
Recording.IgnoreNames("sql");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT [c].[Id], [c].[Name]
FROM [Companies] AS [c]
WHERE [c].[Name] = N'Title'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
ef: {
Type: ReaderExecutedAsync,
HasTransaction: false,
Text:
SELECT [c].[Id], [c].[Name]
FROM [Companies] AS [c]
WHERE [c].[Name] = N'Title'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[TestFixture]
[NonParallelizable]
public class StaticSettingsTests
{
static SqlInstance<SampleDbContext> sqlInstance = new(
buildTemplate: data => data.Database.EnsureCreatedAsync(),
constructInstance: builder =>
{
builder.EnableRecording();
return new(builder.Options);
});

[TearDown]
public void TearDown() =>
VerifyEntityFramework.DisableSqlFormatting = false;

[Test]
public async Task DisableSqlFormattingRecording()
{
#region DisableSqlFormatting

VerifyEntityFramework.DisableSqlFormatting = true;

#endregion

await using var database = await sqlInstance.Build();
var data = database.Context;

Recording.Start();

await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();

await Verify();
}

[Test]
public async Task DisableSqlFormattingQueryable()
{
VerifyEntityFramework.DisableSqlFormatting = true;

await using var database = await sqlInstance.Build();
var data = database.Context;

var queryable = data
.Companies
.Where(_ => _.Name == "Title");

await Verify(queryable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EfLocalDb" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Verify.NUnit" />
<PackageReference Include="Argon" />
<PackageReference Include="Verify" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<ProjectReference Include="..\Verify.EntityFramework\Verify.EntityFramework.csproj" />
<Compile Include="..\Verify.EntityFramework.Tests\Snippets\DataContext\Company.cs" Link="DataContext\Company.cs" />
<Compile Include="..\Verify.EntityFramework.Tests\Snippets\DataContext\Employee.cs" Link="DataContext\Employee.cs" />
<Compile Include="..\Verify.EntityFramework.Tests\Snippets\DataContext\SampleDbContext.cs" Link="DataContext\SampleDbContext.cs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Verify.EntityFramework.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<File Path="global.json" />
<File Path="mdsnippets.json" />
</Folder>
<Project Path="Verify.EntityFramework.StaticSettingsTests/Verify.EntityFramework.StaticSettingsTests.csproj" />
<Project Path="Verify.EntityFramework.Tests/Verify.EntityFramework.Tests.csproj" />
<Project Path="Verify.EntityFramework/Verify.EntityFramework.csproj" />
<Project Path="Verify.EntityFrameworkClassic.Tests/Verify.EntityFrameworkClassic.Tests.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.EntityFramework/Converters/LogEntryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public override void Write(VerifyJsonWriter writer, LogEntry logEntry)
writer.WriteMember(logEntry, logEntry.HasTransaction, "HasTransaction");
writer.WriteMember(logEntry, logEntry.Exception, "Exception");
writer.WriteMember(logEntry, logEntry.Parameters, "Parameters");
if (logEntry.IsSqlServer)
if (logEntry.IsSqlServer && !VerifyEntityFramework.DisableSqlFormatting)
{
var text = SqlFormatter.Format(logEntry.Text);
writer.WriteMember(logEntry, text.ToString(), "Text");
Expand Down
7 changes: 7 additions & 0 deletions src/Verify.EntityFramework/VerifyEntityFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static void IgnoreNavigationProperties(this VerifySettings settings, IMod
}
}

public static bool DisableSqlFormatting { get; set; }

public static void ScrubInlineEfDateTimes() =>
VerifierSettings.ScrubInlineDateTimes("yyyy-MM-ddTHH:mm:ss.fffffffZ");

Expand Down Expand Up @@ -155,6 +157,11 @@ static ConversionResult QueryableToSql(object arg, IReadOnlyDictionary<string, o

internal static bool ShouldFormatSql(this IQueryable queryable)
{
if (DisableSqlFormatting)
{
return false;
}

var model = FindModel(queryable.Expression);
return model != null && model.IsSqlServer();
}
Expand Down
Loading