Skip to content

Commit ad0395d

Browse files
Fix tests.
1 parent bc05f0b commit ad0395d

11 files changed

Lines changed: 229 additions & 270 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Dialect/SqlDialectBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public virtual string BuildMoveDataSql<T>(
8484

8585
if (returnedColumns.Count != 0)
8686
{
87-
q.Append("RETURNING ");
87+
q.Append(" RETURNING ");
8888
q.AppendJoin(", ", returnedColumns.Select(p => p.QuotedColumName));
8989
q.AppendLine();
9090
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntity.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
88
[PrimaryKey(nameof(Id))]
99
[Index(nameof(Name), IsUnique = true)]
1010
[Table("test_entity")]
11-
public class TestEntity
11+
public class TestEntity : TestEntityBase
1212
{
1313
public int Id { get; set; }
1414

@@ -19,9 +19,6 @@ public class TestEntity
1919
[Column("some_price")]
2020
public decimal Price { get; set; }
2121

22-
[Column("test_run")]
23-
public Guid TestRun { get; set; }
24-
2522
[Column("the_identifier")]
2623
public Guid Identifier { get; set; }
2724

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
3+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
4+
5+
public abstract class TestEntityBase
6+
{
7+
[Column("test_run")]
8+
public Guid TestRun { get; set; }
9+
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntityWithConverters.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
55

66
[Table("test_entity_with_converters")]
7-
public class TestEntityWithConverters
7+
public class TestEntityWithConverters : TestEntityBase
88
{
99
public int Id { get; set; }
1010

@@ -14,8 +14,5 @@ public class TestEntityWithConverters
1414

1515
[Column("created_at")]
1616
public DateTime CreatedAt { get; set; }
17-
18-
[Column("test_run")]
19-
public Guid TestRun { get; set; }
2017
}
2118

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
22
using System.ComponentModel.DataAnnotations.Schema;
33

44
using NetTopologySuite.Geometries;
55

66
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
77

88
[Table("test_entity_geo")]
9-
public class TestEntityWithGeo
9+
public class TestEntityWithGeo : TestEntityBase
1010
{
1111
[Key]
1212
public int Id { get; set; }
1313

1414
public Geometry GeoObject { get; set; } = null!;
15-
16-
[Column("test_run")]
17-
public Guid TestRun { get; set; }
1815
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntityWithGuidId.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
55

66
[Table("test_entity_guids")]
7-
public class TestEntityWithGuidId
7+
public class TestEntityWithGuidId : TestEntityBase
88
{
99
[Key]
1010
public Guid Id { get; set; }
1111

1212
[Column("name")]
1313
[MaxLength(100)]
1414
public string Name { get; set; } = string.Empty;
15-
16-
[Column("test_run")]
17-
public Guid TestRun { get; set; }
1815
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntityWithJson.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
55

66
[Table("test_entity_json")]
7-
public class TestEntityWithJson
7+
public class TestEntityWithJson : TestEntityBase
88
{
99
[Key]
1010
public int Id { get; set; }
1111

1212
public List<int> Json { get; set; } = [];
13-
14-
[Column("test_run")]
15-
public Guid TestRun { get; set; }
1613
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/PhenX.EntityFrameworkCore.BulkInsert.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
@@ -13,8 +13,10 @@
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16+
<PackageReference Include="FluentAssertions" Version="7.2.0" />
1617
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
1718
<PackageReference Include="xunit" Version="2.9.3" />
19+
<PackageReference Include="Xunit.Combinatorial" Version="1.6.24" />
1820
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1921
<PrivateAssets>all</PrivateAssets>
2022
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
using PhenX.EntityFrameworkCore.BulkInsert.Enums;
4+
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
5+
using PhenX.EntityFrameworkCore.BulkInsert.Options;
6+
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
7+
8+
using Xunit;
9+
10+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests;
11+
12+
public enum InsertStrategy
13+
{
14+
Insert,
15+
InsertReturn,
16+
InsertAsync,
17+
InsertReturnAsync
18+
}
19+
20+
public static class TestHelpers
21+
{
22+
public static async Task<List<T>> InsertWithStrategyAsync<T>(
23+
this TestDbContextBase dbContext,
24+
InsertStrategy strategy,
25+
List<T> entities,
26+
Action<BulkInsertOptions>? configure = null,
27+
OnConflictOptions<T>? onConflict = null)
28+
where T : TestEntityBase
29+
{
30+
Skip.If(strategy is InsertStrategy.InsertReturn or InsertStrategy.InsertReturnAsync && dbContext.IsProvider(ProviderType.MySql));
31+
32+
var runId = Guid.NewGuid();
33+
if (entities.Any(x => x.TestRun == default))
34+
{
35+
foreach (var entity in entities)
36+
{
37+
if (entity.TestRun == default)
38+
{
39+
entity.TestRun = runId;
40+
}
41+
}
42+
}
43+
else if (entities.Count > 0)
44+
{
45+
runId = entities[0].TestRun;
46+
}
47+
48+
var actualConfigure = configure ?? (_ => { });
49+
switch (strategy)
50+
{
51+
case InsertStrategy.InsertReturn:
52+
return dbContext.ExecuteBulkInsertReturnEntities(entities, actualConfigure, onConflict);
53+
case InsertStrategy.InsertReturnAsync:
54+
return await dbContext.ExecuteBulkInsertReturnEntitiesAsync(entities, actualConfigure, onConflict);
55+
case InsertStrategy.Insert:
56+
dbContext.ExecuteBulkInsert(entities, actualConfigure, onConflict);
57+
return dbContext.Set<T>().Where(x => x.TestRun == runId).ToList();
58+
case InsertStrategy.InsertAsync:
59+
await dbContext.ExecuteBulkInsertAsync(entities, actualConfigure, onConflict);
60+
return await dbContext.Set<T>().Where(x => x.TestRun == runId).ToListAsync();
61+
default:
62+
throw new NotImplementedException();
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)