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 Dec 27, 2023
1 parent 99a61d2 commit a0e5da2
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 @@ -355,20 +355,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 @@ -380,13 +396,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 @@ -500,6 +515,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 @@ -513,6 +529,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 @@ -531,6 +550,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 @@ -586,6 +612,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 @@ -272,15 +272,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 @@ -320,7 +327,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 @@ -332,14 +348,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 @@ -479,6 +494,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 @@ -492,6 +508,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 @@ -510,6 +529,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 @@ -559,6 +585,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 @@ -284,15 +284,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 @@ -318,7 +325,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 @@ -330,13 +346,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 @@ -461,6 +476,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 @@ -474,6 +490,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 @@ -492,6 +511,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 @@ -541,6 +567,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 a0e5da2

Please sign in to comment.