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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Enable VerifyEntityFramework once at assembly load time:
```cs
VerifyEntityFramework.Enable();
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L476-L480' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablecore' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/ModuleInitializer.cs#L6-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablecore' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;CS8632;EF1001</NoWarn>
<Version>6.4.0</Version>
<Version>6.5.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>EntityFramework, Verify</PackageTags>
<Description>Extends Verify (https://github.com/VerifyTests/Verify) to allow verification of EntityFramework bits.</Description>
Expand Down
9 changes: 0 additions & 9 deletions src/Verify.EntityFramework.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,4 @@ static DbContextOptions<SampleDbContext> DbContextOptions(
new DbContextOptionsBuilder<SampleDbContext>()
.UseInMemoryDatabase(databaseName)
.Options;

static CoreTests()
{
#region EnableCore

VerifyEntityFramework.Enable();

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
Type: DbUpdateException,
Entries: [
{
EntryProperties: [
{
PropertyName: Id,
OriginalValue: Guid_1,
CurrentValue: Guid_1
},
{
PropertyName: Property,
OriginalValue: Item1,
CurrentValue: Item1
}
],
State: Added
}
]
}
51 changes: 51 additions & 0 deletions src/Verify.EntityFramework.Tests/DbUpdateExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using EfLocalDb;
using Microsoft.EntityFrameworkCore;

[TestFixture]
public class DbUpdateExceptionTests
{
[Test]
public async Task Run()
{
var instance = new SqlInstance<TestDbContext>(builder => new(builder.Options));
var id = Guid.NewGuid();
var entity = new TestEntity
{
Id = id,
Property = "Item1"
};
await using var database = await instance.Build(new List<object>
{
entity
});

var duplicate = new TestEntity
{
Id = id,
Property = "Item1"
};
var testDbContext = database.NewDbContext();
testDbContext.Add(duplicate);
await ThrowsTask(() => testDbContext.SaveChangesAsync())
.IgnoreStackTrack();
}

public class TestEntity
{
public Guid Id { get; set; }
public string? Property { get; set; }
}

public class TestDbContext :
DbContext
{
public DbSet<TestEntity> TestEntities { get; set; } = null!;

public TestDbContext(DbContextOptions options) :
base(options)
{
}

protected override void OnModelCreating(ModelBuilder model) => model.Entity<TestEntity>();
}
}
12 changes: 12 additions & 0 deletions src/Verify.EntityFramework.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
#region EnableCore

VerifyEntityFramework.Enable();

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EfLocalDb" Version="13.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.5" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore;

class DbUpdateExceptionConverter :
WriteOnlyJsonConverter<DbUpdateException>
{
public override void Write(VerifyJsonWriter writer, DbUpdateException exception)
{
writer.WriteStartObject();

writer.WriteProperty(exception, exception.GetType(), "Type");
writer.WriteProperty(exception, exception.InnerException, "InnerException");

var entriesValue = exception.Entries
.Select(
e => new
{
EntryProperties = e.Properties.Select(
p => new
{
PropertyName = p.Metadata.Name,
p.OriginalValue,
p.CurrentValue,
p.IsTemporary,
p.IsModified,
}),
e.State,
})
.ToList();

writer.WriteProperty(exception, entriesValue, "Entries");
writer.WriteProperty(exception, exception.StackTrace, "StackTrace");

writer.WriteEndObject();
}
}
1 change: 1 addition & 0 deletions src/Verify.EntityFramework/VerifyEntityFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static void Enable()
settings.AddExtraSettings(serializer =>
{
var converters = serializer.Converters;
converters.Add(new DbUpdateExceptionConverter());
converters.Add(new TrackerConverter());
converters.Add(new QueryableConverter());
});
Expand Down