-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d40e0ac
commit b500db4
Showing
12 changed files
with
542 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/DddEntityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
using Codehard.Infrastructure.EntityFramework.Tests.Entities.DomainDrivenDesign; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests; | ||
|
||
public class DddEntityTests | ||
{ | ||
[Fact] | ||
public async Task WhenSaveAggregateRoot_UsingDddDbContext_ThenSaveAllEntities() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDddDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
await using var context = new TestDddDbContext(options); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var a = new DddA { Id = Guid.NewGuid() }; | ||
var b1 = new DddB { Id = Guid.NewGuid() }; | ||
var c1 = new DddC { Id = Guid.NewGuid() }; | ||
|
||
a.AddB(b1); | ||
b1.AddC(c1); | ||
context.As.Add(a); | ||
|
||
await context.SaveChangesAsync(); | ||
|
||
context.ChangeTracker.Clear(); | ||
|
||
// Act | ||
var savedA = await context.As | ||
.Include(dddA => dddA.Bs) | ||
.ThenInclude(dddB => dddB.Cs) | ||
.SingleAsync(dddA => dddA.Id == a.Id); | ||
|
||
// Assert | ||
Assert.NotNull(savedA); | ||
Assert.NotEmpty(savedA.Bs); | ||
Assert.NotEmpty(savedA.Bs.First().Cs); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAddNonAggregateRootDirectly_UsingDddDbContextSet_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDddDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
await using var context = new TestDddDbContext(options); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var b1 = new DddB { Id = Guid.NewGuid() }; | ||
var c1 = new DddC { Id = Guid.NewGuid() }; | ||
|
||
b1.AddC(c1); | ||
|
||
InvalidOperationException? exception = null; | ||
|
||
// Act | ||
try | ||
{ | ||
context.Set<DddB>().Add(b1); | ||
} | ||
catch (InvalidOperationException e) | ||
{ | ||
exception = e; | ||
} | ||
|
||
// Assert | ||
Assert.NotNull(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAddNonAggregateRootDirectly_UsingDddDbContext_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDddDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
await using var context = new TestDddDbContext(options); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var b1 = new DddB { Id = Guid.NewGuid() }; | ||
var c1 = new DddC { Id = Guid.NewGuid() }; | ||
|
||
b1.AddC(c1); | ||
|
||
InvalidOperationException? exception = null; | ||
|
||
// Act | ||
try | ||
{ | ||
context.Add(b1); | ||
} | ||
catch (InvalidOperationException e) | ||
{ | ||
exception = e; | ||
} | ||
|
||
// Assert | ||
Assert.NotNull(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAddNonAggregateRootDirectly_UsingDddDbContextGenericAsync_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDddDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
await using var context = new TestDddDbContext(options); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var b1 = new DddB { Id = Guid.NewGuid() }; | ||
var c1 = new DddC { Id = Guid.NewGuid() }; | ||
|
||
b1.AddC(c1); | ||
|
||
InvalidOperationException? exception = null; | ||
|
||
// Act | ||
try | ||
{ | ||
await context.AddAsync(b1); | ||
} | ||
catch (InvalidOperationException e) | ||
{ | ||
exception = e; | ||
} | ||
|
||
// Assert | ||
Assert.NotNull(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAddNonAggregateRootDirectly_UsingDddDbContextAsync_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDddDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
await using var context = new TestDddDbContext(options); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var b1 = new DddB { Id = Guid.NewGuid() }; | ||
var c1 = new DddC { Id = Guid.NewGuid() }; | ||
|
||
b1.AddC(c1); | ||
|
||
InvalidOperationException? exception = null; | ||
|
||
// Act | ||
try | ||
{ | ||
await context.AddAsync((object)b1); | ||
} | ||
catch (InvalidOperationException e) | ||
{ | ||
exception = e; | ||
} | ||
|
||
// Assert | ||
Assert.NotNull(exception); | ||
} | ||
|
||
private static SqliteConnection CreateInMemoryDatabase() | ||
{ | ||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
connection.Open(); | ||
return connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ructure/Codehard.Infrastructure.EntityFramework.Tests/Entities/DomainDrivenDesign/DddA.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Codehard.Common.DomainModel; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests.Entities.DomainDrivenDesign; | ||
|
||
public class DddA : Entity<Guid>, IAggregateRoot<Guid> | ||
{ | ||
public override Guid Id { get; init; } | ||
|
||
private readonly List<DddB> bs = new(); | ||
|
||
public IReadOnlyCollection<DddB> Bs => bs.AsReadOnly(); | ||
|
||
public void AddB(DddB b) | ||
{ | ||
bs.Add(b); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ructure/Codehard.Infrastructure.EntityFramework.Tests/Entities/DomainDrivenDesign/DddB.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Codehard.Common.DomainModel; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests.Entities.DomainDrivenDesign; | ||
|
||
public class DddB : Entity<Guid> | ||
{ | ||
public override Guid Id { get; init; } | ||
|
||
public DddA A { get; private set; } = null!; | ||
|
||
private readonly List<DddC> cs = new(); | ||
|
||
public IReadOnlyCollection<DddC> Cs => cs.AsReadOnly(); | ||
|
||
public void AddC(DddC c) | ||
{ | ||
cs.Add(c); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ructure/Codehard.Infrastructure.EntityFramework.Tests/Entities/DomainDrivenDesign/DddC.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Codehard.Common.DomainModel; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests.Entities.DomainDrivenDesign; | ||
|
||
public class DddC : Entity<Guid> | ||
{ | ||
public override Guid Id { get; init; } | ||
|
||
public DddB B { get; private set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/TestDddDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Codehard.Infrastructure.EntityFramework.DbContexts; | ||
using Codehard.Infrastructure.EntityFramework.Tests.Entities.DomainDrivenDesign; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests; | ||
|
||
public class TestDddDbContext : DddDbContext | ||
{ | ||
public TestDddDbContext(DbContextOptions options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<DddA> As { get; set; } | ||
} |
Oops, something went wrong.