Skip to content

Commit

Permalink
Add unit tests for converters
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed May 17, 2024
1 parent 217f5d1 commit 9930eb2
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 30 deletions.
15 changes: 15 additions & 0 deletions test/StronglyTypedIds.IntegrationTests.ExternalIds/Converters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using StronglyTypedIds.IntegrationTests.Types;

namespace StronglyTypedIds.IntegrationTests;

[StronglyTypedIdConverters<GuidId1>("guid-dapper", "guid-efcore")]
internal partial struct Guid1Converters { }

[StronglyTypedIdConverters<IntId>("int-dapper", "int-efcore")]
internal partial struct IntConverters { }

[StronglyTypedIdConverters<LongId>("long-dapper", "long-efcore")]
internal partial struct LongConverters { }

[StronglyTypedIdConverters<StringId>("string-dapper", "string-efcore")]
internal partial struct StringConverters { }
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<IsPackable>false</IsPackable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\StronglyTypedIds\StronglyTypedIds.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)..\..\src\StronglyTypedIds.Templates\*.typedid" Link="Templates\%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\StronglyTypedIds.IntegrationTests\*.cs" Link="%(Filename)%(Extension)" />
Expand Down
4 changes: 4 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static void AddHandlers()
SqlMapper.AddTypeHandler(new ConvertersStringId2.DapperTypeHandler());
SqlMapper.AddTypeHandler(new NullableStringId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new NewIdId1.DapperTypeHandler());
SqlMapper.AddTypeHandler(new Guid1Converters.DapperTypeHandler());
SqlMapper.AddTypeHandler(new IntConverters.DapperTypeHandler());
SqlMapper.AddTypeHandler(new LongConverters.DapperTypeHandler());
SqlMapper.AddTypeHandler(new StringConverters.DapperTypeHandler());
}
}
}
11 changes: 11 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ internal readonly partial struct VeryNestedId
}
}

[StronglyTypedIdConverters<GuidId1>("guid-dapper", "guid-efcore")]
internal partial struct Guid1Converters { }

[StronglyTypedIdConverters<IntId>("int-dapper", "int-efcore")]
internal partial struct IntConverters { }

[StronglyTypedIdConverters<LongId>("long-dapper", "long-efcore")]
internal partial struct LongConverters { }

[StronglyTypedIdConverters<StringId>("string-dapper", "string-efcore")]
internal partial struct StringConverters { }
45 changes: 38 additions & 7 deletions test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,36 @@ public void WhenEfCoreValueConverterUsesValueConverter()
}

[Fact]
public async Task WhenDapperValueConverterUsesValueConverter()
public Task WhenDapperValueConverterUsesValueConverter_Id()
=> WhenDapperValueConverterUsesValueConverter(g => new ConvertersGuidId(g));

[Fact]
public Task WhenDapperValueConverterUsesValueConverter_Converter()
=> WhenDapperValueConverterUsesValueConverter(g => new GuidId1(g));

private async Task WhenDapperValueConverterUsesValueConverter<T>(Func<Guid, T> newFunc)
{
using var connection = new SqliteConnection("DataSource=:memory:");
await connection.OpenAsync();

var results = await connection.QueryAsync<ConvertersGuidId>("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'");
var results = await connection.QueryAsync<T>("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'");

var value = Assert.Single(results);
Assert.Equal(value, new ConvertersGuidId(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")));
Assert.Equal(value, newFunc(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")));
}

#if NET6_0_OR_GREATER
[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Id()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(x => x.Entities,
new TestEntity { Id = ConvertersGuidId.New() });

[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Converter()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(c => c.Entities3,
new TestEntity3 { Id = GuidId1.New() });

private void WhenConventionBasedEfCoreValueConverterUsesValueConverter<T>(Func<ConventionsDbContext, DbSet<T>> dbsetFunc, T entity) where T : class
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
Expand All @@ -399,13 +415,12 @@ public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
using (var context = new ConventionsDbContext(options))
{
context.Database.EnsureCreated();
context.Entities.Add(
new TestEntity { Id = ConvertersGuidId.New() });
dbsetFunc(context).Add(entity);
context.SaveChanges();
}
using (var context = new ConventionsDbContext(options))
{
var all = context.Entities.ToList();
var all = dbsetFunc(context).ToList();
Assert.Single(all);
}
}
Expand Down Expand Up @@ -519,6 +534,7 @@ internal class ConventionsDbContext : DbContext
{
public DbSet<TestEntity> Entities { get; set; }
public DbSet<TestEntity2> Entities2 { get; set; }
public DbSet<TestEntity3> Entities3 { get; set; }

public ConventionsDbContext(DbContextOptions options) : base(options)
{
Expand All @@ -532,6 +548,9 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura
configurationBuilder
.Properties<ConvertersGuidId2>()
.HaveConversion<ConvertersGuidId2.EfCoreValueConverter>();
configurationBuilder
.Properties<GuidId1>()
.HaveConversion<Guid1Converters.EfCoreValueConverter>();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand All @@ -550,6 +569,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.Property(x => x.Id)
.ValueGeneratedNever();
});
modelBuilder
.Entity<TestEntity3>(builder =>
{
builder
.Property(x => x.Id)
.ValueGeneratedNever();
});
}
}
#endif
Expand Down Expand Up @@ -605,6 +631,11 @@ internal class TestEntity2
public ConvertersGuidId2 Id { get; set; }
}

internal class TestEntity3
{
public GuidId1 Id { get; set; }
}

internal class EntityWithNullableId2
{
public ConvertersGuidId2? Id { get; set; }
Expand Down
45 changes: 38 additions & 7 deletions test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,22 @@ public void WhenEfCoreValueConverterUsesValueConverter()
}

[Fact]
public async Task WhenDapperValueConverterUsesValueConverter()
public Task WhenDapperValueConverterUsesValueConverter_Id()
=> WhenDapperValueConverterUsesValueConverter(g => new ConvertersIntId(g));

[Fact]
public Task WhenDapperValueConverterUsesValueConverter_Converter()
=> WhenDapperValueConverterUsesValueConverter(g => new IntId(g));

private async Task WhenDapperValueConverterUsesValueConverter<T>(Func<int, T> newFunc)
{
using var connection = new SqliteConnection("DataSource=:memory:");
await connection.OpenAsync();

var results = await connection.QueryAsync<ConvertersIntId>("SELECT 123");
var results = await connection.QueryAsync<T>("SELECT 123");

var value = Assert.Single(results);
Assert.Equal(new ConvertersIntId(123), value);
Assert.Equal(newFunc(123), value);
}

[Fact(Skip = "Requires localdb to be available")]
Expand Down Expand Up @@ -321,7 +328,16 @@ public void TypeConverter_CanConvertToAndFrom(object value)

#if NET6_0_OR_GREATER
[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Id()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(x => x.Entities,
new TestEntity { Id = new ConvertersIntId(123) });

[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Converter()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(c => c.Entities3,
new TestEntity3 { Id = new IntId(123) });

private void WhenConventionBasedEfCoreValueConverterUsesValueConverter<T>(Func<ConventionsDbContext, DbSet<T>> dbsetFunc, T entity) where T : class
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
Expand All @@ -333,14 +349,13 @@ public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
using (var context = new ConventionsDbContext(options))
{
context.Database.EnsureCreated();
context.Entities.Add(
new TestEntity {Id = new ConvertersIntId(123)});
dbsetFunc(context).Add(entity);
context.SaveChanges();
}

using (var context = new ConventionsDbContext(options))
{
var all = context.Entities.ToList();
var all = dbsetFunc(context).ToList();
Assert.Single(all);
}
}
Expand Down Expand Up @@ -499,6 +514,7 @@ internal class ConventionsDbContext : DbContext
{
public DbSet<TestEntity> Entities { get; set; }
public DbSet<TestEntity2> Entities2 { get; set; }
public DbSet<TestEntity3> Entities3 { get; set; }

public ConventionsDbContext(DbContextOptions options) : base(options)
{
Expand All @@ -512,6 +528,9 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura
configurationBuilder
.Properties<ConvertersIntId2>()
.HaveConversion<ConvertersIntId2.EfCoreValueConverter>();
configurationBuilder
.Properties<IntId>()
.HaveConversion<IntConverters.EfCoreValueConverter>();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand All @@ -530,6 +549,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.Property(x => x.Id)
.ValueGeneratedNever();
});
modelBuilder
.Entity<TestEntity3>(builder =>
{
builder
.Property(x => x.Id)
.ValueGeneratedNever();
});
}
}
#endif
Expand Down Expand Up @@ -579,6 +605,11 @@ internal class TestEntity2
public ConvertersIntId2 Id { get; set; }
}

internal class TestEntity3
{
public IntId Id { get; set; }
}

internal class EntityWithNullableId2
{
public ConvertersIntId2? Id { get; set; }
Expand Down
45 changes: 38 additions & 7 deletions test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,22 @@ public void WhenEfCoreValueConverterUsesValueConverter()
}

[Fact]
public async Task WhenDapperValueConverterUsesValueConverter()
public Task WhenDapperValueConverterUsesValueConverter_Id()
=> WhenDapperValueConverterUsesValueConverter(g => new ConvertersLongId(g));

[Fact]
public Task WhenDapperValueConverterUsesValueConverter_Converter()
=> WhenDapperValueConverterUsesValueConverter(g => new LongId(g));

private async Task WhenDapperValueConverterUsesValueConverter<T>(Func<int, T> newFunc)
{
using var connection = new SqliteConnection("DataSource=:memory:");
await connection.OpenAsync();

var results = await connection.QueryAsync<ConvertersLongId>("SELECT 123");
var results = await connection.QueryAsync<T>("SELECT 123");

var value = Assert.Single(results);
Assert.Equal(value, new ConvertersLongId(123));
Assert.Equal(value, newFunc(123));
}

[Fact(Skip = "Requires localdb to be available")]
Expand All @@ -319,7 +326,16 @@ public void WhenDapperValueConverterAndDecimalUsesValueConverter()

#if NET6_0_OR_GREATER
[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Id()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(x => x.Entities,
new TestEntity { Id = new ConvertersLongId(123) });

[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter_Converter()
=> WhenConventionBasedEfCoreValueConverterUsesValueConverter(c => c.Entities3,
new TestEntity3 { Id = new LongId(123) });

private void WhenConventionBasedEfCoreValueConverterUsesValueConverter<T>(Func<ConventionsDbContext, DbSet<T>> dbsetFunc, T entity) where T : class
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
Expand All @@ -331,13 +347,12 @@ public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
using (var context = new ConventionsDbContext(options))
{
context.Database.EnsureCreated();
context.Entities.Add(
new TestEntity { Id = new ConvertersLongId(123) });
dbsetFunc(context).Add(entity);
context.SaveChanges();
}
using (var context = new ConventionsDbContext(options))
{
var all = context.Entities.ToList();
var all = dbsetFunc(context).ToList();
Assert.Single(all);
}
}
Expand Down Expand Up @@ -481,6 +496,7 @@ internal class ConventionsDbContext : DbContext
{
public DbSet<TestEntity> Entities { get; set; }
public DbSet<TestEntity2> Entities2 { get; set; }
public DbSet<TestEntity3> Entities3 { get; set; }

public ConventionsDbContext(DbContextOptions options) : base(options)
{
Expand All @@ -494,6 +510,9 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura
configurationBuilder
.Properties<ConvertersLongId2>()
.HaveConversion<ConvertersLongId2.EfCoreValueConverter>();
configurationBuilder
.Properties<LongId>()
.HaveConversion<LongConverters.EfCoreValueConverter>();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand All @@ -512,6 +531,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.Property(x => x.Id)
.ValueGeneratedNever();
});
modelBuilder
.Entity<TestEntity3>(builder =>
{
builder
.Property(x => x.Id)
.ValueGeneratedNever();
});
}
}
#endif
Expand Down Expand Up @@ -561,6 +587,11 @@ internal class TestEntity2
public ConvertersLongId2 Id { get; set; }
}

internal class TestEntity3
{
public LongId Id { get; set; }
}

internal class EntityWithNullableId2
{
public ConvertersLongId2? Id { get; set; }
Expand Down
Loading

0 comments on commit 9930eb2

Please sign in to comment.