-
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
70184a6
commit 1429b8f
Showing
4 changed files
with
75 additions
and
1 deletion.
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
58 changes: 58 additions & 0 deletions
58
src/Codehard.Infrastructure/Codehard.Infrastructure.EntityFramework.Tests/MoneyTypeTests.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,58 @@ | ||
using System.Reflection; | ||
using Codehard.Infrastructure.EntityFramework.Extensions; | ||
using Codehard.Infrastructure.EntityFramework.Tests.Entities; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
|
||
namespace Codehard.Infrastructure.EntityFramework.Tests; | ||
|
||
public class MoneyTypeTests | ||
{ | ||
private static SqliteConnection CreateInMemoryDatabase() | ||
{ | ||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
connection.Open(); | ||
return connection; | ||
} | ||
|
||
[Fact] | ||
public async void WhenAddNewEntity_ShouldPersistedToDb() | ||
{ | ||
// Arrange | ||
var options = new DbContextOptionsBuilder<TestDbContext>() | ||
.UseSqlite(CreateInMemoryDatabase()) | ||
.Options; | ||
|
||
var assembly = Assembly.GetExecutingAssembly(); | ||
|
||
var loggerMock = new Mock<ILogger<TestDbContext>>(); | ||
var logger = loggerMock.Object; | ||
|
||
await using var context = new TestDbContext( | ||
options, | ||
builder => builder.ApplyConfigurationsFromAssemblyFor<TestDbContext>(assembly), | ||
logger); | ||
|
||
await context.Database.EnsureCreatedAsync(); | ||
|
||
// Act | ||
var newEntity = EntityA.Create(); | ||
|
||
context.As.Add(newEntity); | ||
context.SaveChanges(); | ||
context.Entry(newEntity).State = EntityState.Detached; | ||
|
||
// Assert | ||
var actual = | ||
context.As | ||
.Include(entityA => entityA.NullableMoney) | ||
.Include(entityA => entityA.Money) | ||
.First(); | ||
|
||
Assert.Null(actual.NullableMoney); | ||
Assert.NotNull(actual.Money); | ||
Assert.Equal(0, actual.Money.Amount); | ||
} | ||
} |
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