Skip to content

Commit 614691c

Browse files
authored
add DbUpdateException support (#467)
1 parent bbc1b66 commit 614691c

9 files changed

Lines changed: 122 additions & 11 deletions

File tree

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Enable VerifyEntityFramework once at assembly load time:
2525
```cs
2626
VerifyEntityFramework.Enable();
2727
```
28-
<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>
28+
<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>
2929
<!-- endSnippet -->
3030

3131

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;CS8632;EF1001</NoWarn>
5-
<Version>6.4.0</Version>
5+
<Version>6.5.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<PackageTags>EntityFramework, Verify</PackageTags>
88
<Description>Extends Verify (https://github.com/VerifyTests/Verify) to allow verification of EntityFramework bits.</Description>

src/Verify.EntityFramework.Tests/CoreTests.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,4 @@ static DbContextOptions<SampleDbContext> DbContextOptions(
470470
new DbContextOptionsBuilder<SampleDbContext>()
471471
.UseInMemoryDatabase(databaseName)
472472
.Options;
473-
474-
static CoreTests()
475-
{
476-
#region EnableCore
477-
478-
VerifyEntityFramework.Enable();
479-
480-
#endregion
481-
}
482473
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
Type: DbUpdateException,
3+
Entries: [
4+
{
5+
EntryProperties: [
6+
{
7+
PropertyName: Id,
8+
OriginalValue: Guid_1,
9+
CurrentValue: Guid_1
10+
},
11+
{
12+
PropertyName: Property,
13+
OriginalValue: Item1,
14+
CurrentValue: Item1
15+
}
16+
],
17+
State: Added
18+
}
19+
]
20+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using EfLocalDb;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
[TestFixture]
5+
public class DbUpdateExceptionTests
6+
{
7+
[Test]
8+
public async Task Run()
9+
{
10+
var instance = new SqlInstance<TestDbContext>(builder => new(builder.Options));
11+
var id = Guid.NewGuid();
12+
var entity = new TestEntity
13+
{
14+
Id = id,
15+
Property = "Item1"
16+
};
17+
await using var database = await instance.Build(new List<object>
18+
{
19+
entity
20+
});
21+
22+
var duplicate = new TestEntity
23+
{
24+
Id = id,
25+
Property = "Item1"
26+
};
27+
var testDbContext = database.NewDbContext();
28+
testDbContext.Add(duplicate);
29+
await ThrowsTask(() => testDbContext.SaveChangesAsync())
30+
.IgnoreStackTrack();
31+
}
32+
33+
public class TestEntity
34+
{
35+
public Guid Id { get; set; }
36+
public string? Property { get; set; }
37+
}
38+
39+
public class TestDbContext :
40+
DbContext
41+
{
42+
public DbSet<TestEntity> TestEntities { get; set; } = null!;
43+
44+
public TestDbContext(DbContextOptions options) :
45+
base(options)
46+
{
47+
}
48+
49+
protected override void OnModelCreating(ModelBuilder model) => model.Entity<TestEntity>();
50+
}
51+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public static class ModuleInitializer
2+
{
3+
[ModuleInitializer]
4+
public static void Init()
5+
{
6+
#region EnableCore
7+
8+
VerifyEntityFramework.Enable();
9+
10+
#endregion
11+
}
12+
}

src/Verify.EntityFramework.Tests/Verify.EntityFramework.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<TargetFramework>net6.0</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6+
<PackageReference Include="EfLocalDb" Version="13.6.0" />
67
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.5" />
78
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
89
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
class DbUpdateExceptionConverter :
4+
WriteOnlyJsonConverter<DbUpdateException>
5+
{
6+
public override void Write(VerifyJsonWriter writer, DbUpdateException exception)
7+
{
8+
writer.WriteStartObject();
9+
10+
writer.WriteProperty(exception, exception.GetType(), "Type");
11+
writer.WriteProperty(exception, exception.InnerException, "InnerException");
12+
13+
var entriesValue = exception.Entries
14+
.Select(
15+
e => new
16+
{
17+
EntryProperties = e.Properties.Select(
18+
p => new
19+
{
20+
PropertyName = p.Metadata.Name,
21+
p.OriginalValue,
22+
p.CurrentValue,
23+
p.IsTemporary,
24+
p.IsModified,
25+
}),
26+
e.State,
27+
})
28+
.ToList();
29+
30+
writer.WriteProperty(exception, entriesValue, "Entries");
31+
writer.WriteProperty(exception, exception.StackTrace, "StackTrace");
32+
33+
writer.WriteEndObject();
34+
}
35+
}

src/Verify.EntityFramework/VerifyEntityFramework.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static void Enable()
5757
settings.AddExtraSettings(serializer =>
5858
{
5959
var converters = serializer.Converters;
60+
converters.Add(new DbUpdateExceptionConverter());
6061
converters.Add(new TrackerConverter());
6162
converters.Add(new QueryableConverter());
6263
});

0 commit comments

Comments
 (0)