Skip to content

Commit

Permalink
Add money type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rungwiroon committed Jan 2, 2024
1 parent 70184a6 commit 1429b8f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@ protected override void EntityConfigure(EntityTypeBuilder<EntityA> builder)
});
builder.Property(e => e.Value)
.IsRequired();
builder.OwnsOne(
e => e.Money,
money =>
{
money.Property(m => m.Amount)
.IsRequired();
money.Property(m => m.Currency)
.IsRequired();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Codehard.Common.DomainModel;
using Codehard.Common.DomainModel.Types;

namespace Codehard.Infrastructure.EntityFramework.Tests.Entities;

Expand Down Expand Up @@ -28,7 +29,11 @@ public class EntityA : Entity<EntityAKey>
public override EntityAKey Id { get; protected init; }

public string Value { get; set; } = string.Empty;


public Money? NullableMoney { get; set; }

public Money? Money { get; set; } = new(0, Currency.Thb);

public void UpdateValue(string newValue)
{
this.Value = newValue;
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ private static void UpdateIfChanged(
SetItemAsDeleted(itemsToRemove);
SetItemAsModified(itemsToModify);

return;

IEnumerable CastToIEnumerable(object? collection)
{
// Get the Cast<T>() method from the Enumerable class using reflection
Expand Down

0 comments on commit 1429b8f

Please sign in to comment.