-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix regression to allow custom value conversion mapping when the ClrT…
…ype is of non-NTS type. plus added unit test. - regression introduced in commit [3ef628f](3ef628f#diff-4f1d25d8c94e4764d1beef032d7ecc17efaf6a2f592db3dab748bac1d7fc0446R87) Refs #1921
- Loading branch information
ANASGAUBA
committed
Jul 31, 2024
1 parent
8070305
commit 9dbad69
Showing
4 changed files
with
119 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
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
96 changes: 96 additions & 0 deletions
96
test/EFCore.MySql.FunctionalTests/NTS/CustomValueConvertersForNonNTSClrTypesMySqlTest.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,96 @@ | ||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.NTS; | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using NetTopologySuite.Geometries; | ||
using Pomelo.EntityFrameworkCore.MySql.Tests; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
public class CustomValueConvertersForNonNTSClrTypesMySqlTest | ||
{ | ||
[Fact] | ||
public async Task NonNTS_ClrType_CanBe_Mapped_to_CustomValueConverters() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddDbContext<CustomDbContext>() | ||
.AddSingleton( | ||
new DbContextOptionsBuilder<CustomDbContext>() | ||
.UseMySql( | ||
AppConfig.ConnectionString, | ||
AppConfig.ServerVersion, | ||
options => | ||
{ | ||
options.UseNetTopologySuite(); | ||
}) | ||
.EnableSensitiveDataLogging() | ||
.LogTo(Console.WriteLine, LogLevel.Debug) | ||
.Options); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
await using var context = provider.GetRequiredService<CustomDbContext>(); | ||
|
||
// Validate `MySqlNetTopologySuiteTypeMappingSourcePlugin.FindMapping()` doesn't throw. | ||
await Should.NotThrowAsync(context.Database.EnsureDeletedAsync()); | ||
await Should.NotThrowAsync(context.Database.EnsureCreatedAsync()); | ||
} | ||
} | ||
|
||
public sealed class CustomDbContext(DbContextOptions<CustomDbContext> options) : DbContext(options) | ||
{ | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
var testClass = modelBuilder.Entity<TestClass>(); | ||
testClass.Property(t => t.Vertices) | ||
.HasColumnType("GEOMETRY") | ||
.HasConversion(new MysqlGeometryWkbValueConverter()); | ||
|
||
var testClass2 = modelBuilder.Entity<TestClass2>(); | ||
testClass2.Property(t => t.Vertices) | ||
.HasColumnType("GEOMETRY"); | ||
} | ||
} | ||
|
||
public class TestClass | ||
{ | ||
public int Id { get; set; } | ||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
public byte[] Vertices { get; set; } | ||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
} | ||
|
||
public class TestClass2 | ||
{ | ||
public int Id { get; set; } | ||
|
||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
public Geometry Vertices { get; set; } | ||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
} | ||
|
||
/// <summary> | ||
/// MySql's internal geometry format is WKB with an initial 4 | ||
/// bytes for the SRID: | ||
/// https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html | ||
/// </summary> | ||
public class MysqlGeometryWkbValueConverter : ValueConverter<byte[], byte[]> | ||
{ | ||
public MysqlGeometryWkbValueConverter() | ||
: base( | ||
clr => AddSRID(clr), | ||
col => StripSRID(col)) | ||
{ | ||
} | ||
|
||
private static byte[] AddSRID(byte[] wkb) => | ||
new byte[] { 0, 0, 0, 0, }.Concat(wkb).ToArray(); | ||
|
||
private static byte[] StripSRID(byte[] col) => | ||
col.Skip(4).ToArray(); | ||
} |