-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension method AddEnumAndValueTypeConverters is using ctors of the …
…value types for reading instead of factories because DB is "source of truth"
- Loading branch information
Showing
10 changed files
with
163 additions
and
8 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
78 changes: 78 additions & 0 deletions
78
...workCore.Tests/EntityFrameworkCore/ValueConversion/ValueTypeValueConverterFactoryTests.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,78 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Thinktecture.Runtime.Tests.TestEntities; | ||
using Thinktecture.Runtime.Tests.TestEnums; | ||
using Thinktecture.Runtime.Tests.TestValueTypes; | ||
using Xunit; | ||
|
||
namespace Thinktecture.Runtime.Tests.EntityFrameworkCore.ValueConversion | ||
{ | ||
public class ValueTypeValueConverterFactoryTests : IDisposable | ||
{ | ||
private readonly TestDbContext _ctx; | ||
|
||
public ValueTypeValueConverterFactoryTests() | ||
{ | ||
_ctx = new(); | ||
_ctx.Database.OpenConnection(); | ||
_ctx.Database.EnsureCreated(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_write_and_read_enums_and_value_types() | ||
{ | ||
var entity = new TestEntity_with_Enum_and_ValueTypes | ||
{ | ||
Id = new Guid("A53F60CD-B53E-40E3-B16F-05E9A223E238"), | ||
TestEnum = TestEnum.Item1, | ||
IntBasedReferenceValueType = IntBasedReferenceValueType.Create(42), | ||
IntBasedStructValueType = IntBasedStructValueType.Create(43), | ||
StringBasedReferenceValueType = StringBasedReferenceValueType.Create("value 1"), | ||
StringBasedStructValueType = StringBasedStructValueType.Create("value 2"), | ||
Boundary = Boundary.Create(10, 20) | ||
}; | ||
_ctx.Add(entity); | ||
await _ctx.SaveChangesAsync(); | ||
|
||
_ctx.ChangeTracker.Clear(); | ||
(await _ctx.TestEntities_with_Enum_and_ValueTypes.SingleAsync()) | ||
.Should().BeEquivalentTo(entity); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_use_ctor_of_value_types_instead_of_factory_because_EF_is_source_of_truth() | ||
{ | ||
var entity = new TestEntity_with_Enum_and_ValueTypes | ||
{ | ||
Id = new Guid("A53F60CD-B53E-40E3-B16F-05E9A223E238"), | ||
StringBasedReferenceValueType = StringBasedReferenceValueType.Create("value"), | ||
StringBasedStructValueType = StringBasedStructValueType.Create("other value"), | ||
Boundary = Boundary.Create(10, 20) | ||
}; | ||
_ctx.Add(entity); | ||
await _ctx.SaveChangesAsync(); | ||
|
||
await using var command = _ctx.Database.GetDbConnection().CreateCommand(); | ||
command.CommandText = @" | ||
UPDATE TestEntities_with_Enum_and_ValueTypes | ||
SET | ||
StringBasedStructValueType = '', | ||
Boundary_Lower = 30 | ||
"; | ||
await command.ExecuteNonQueryAsync(); | ||
|
||
_ctx.ChangeTracker.Clear(); | ||
var loadedEntity = await _ctx.TestEntities_with_Enum_and_ValueTypes.SingleAsync(); | ||
loadedEntity.StringBasedStructValueType.Property.Should().Be(String.Empty); | ||
loadedEntity.Boundary.Lower.Should().Be(30); | ||
loadedEntity.Boundary.Upper.Should().Be(20); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_ctx.Dispose(); | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
....Extensions.EntityFrameworkCore.Tests/TestEntities/TestEntity_with_Enum_and_ValueTypes.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,31 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Thinktecture.Runtime.Tests.TestEnums; | ||
using Thinktecture.Runtime.Tests.TestValueTypes; | ||
|
||
namespace Thinktecture.Runtime.Tests.TestEntities | ||
{ | ||
public class TestEntity_with_Enum_and_ValueTypes | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public TestEnum TestEnum { get; set; } | ||
public IntBasedReferenceValueType IntBasedReferenceValueType { get; set; } | ||
public IntBasedStructValueType IntBasedStructValueType { get; set; } | ||
public StringBasedReferenceValueType StringBasedReferenceValueType { get; set; } | ||
public StringBasedStructValueType StringBasedStructValueType { get; set; } | ||
public Boundary Boundary { get; set; } | ||
|
||
public static void Configure(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<TestEntity_with_Enum_and_ValueTypes>(builder => | ||
{ | ||
// struct are not added bey EF by default | ||
builder.Property(p => p.StringBasedStructValueType); | ||
builder.Property(p => p.IntBasedStructValueType); | ||
|
||
builder.OwnsOne(e => e.Boundary); | ||
}); | ||
} | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
test/Thinktecture.Runtime.Extensions.Tests.Shared/TestValueTypes/Boundary.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 System.ComponentModel.DataAnnotations; | ||
|
||
namespace Thinktecture.Runtime.Tests.TestValueTypes | ||
{ | ||
[ValueType] | ||
public partial class Boundary | ||
{ | ||
public decimal Lower { get; } | ||
public decimal Upper { get; } | ||
|
||
static partial void ValidateFactoryArguments(ref ValidationResult? validationResult, ref decimal lower, ref decimal upper) | ||
{ | ||
if (lower <= upper) | ||
return; | ||
|
||
validationResult = new ValidationResult($"Lower boundary '{lower}' must be less than upper boundary '{upper}'"); | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...cture.Runtime.Extensions.Tests.Shared/Thinktecture.Runtime.Extensions.Tests.Shared.csproj
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