forked from PhenX/PhenX.EntityFrameworkCore.BulkInsert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHelpers.cs
More file actions
65 lines (58 loc) · 2.2 KB
/
TestHelpers.cs
File metadata and controls
65 lines (58 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Microsoft.EntityFrameworkCore;
using PhenX.EntityFrameworkCore.BulkInsert.Enums;
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
using Xunit;
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests;
public enum InsertStrategy
{
Insert,
InsertReturn,
InsertAsync,
InsertReturnAsync
}
public static class TestHelpers
{
public static async Task<List<T>> InsertWithStrategyAsync<T>(
this TestDbContextBase dbContext,
InsertStrategy strategy,
List<T> entities,
Action<BulkInsertOptions>? configure = null,
OnConflictOptions<T>? onConflict = null)
where T : TestEntityBase
{
Skip.If(strategy is InsertStrategy.InsertReturn or InsertStrategy.InsertReturnAsync && dbContext.IsProvider(ProviderType.MySql));
var runId = Guid.NewGuid();
if (entities.Any(x => x.TestRun == default))
{
foreach (var entity in entities)
{
if (entity.TestRun == default)
{
entity.TestRun = runId;
}
}
}
else if (entities.Count > 0)
{
runId = entities[0].TestRun;
}
var actualConfigure = configure ?? (_ => { });
switch (strategy)
{
case InsertStrategy.InsertReturn:
return dbContext.ExecuteBulkInsertReturnEntities(entities, actualConfigure, onConflict);
case InsertStrategy.InsertReturnAsync:
return await dbContext.ExecuteBulkInsertReturnEntitiesAsync(entities, actualConfigure, onConflict);
case InsertStrategy.Insert:
dbContext.ExecuteBulkInsert(entities, actualConfigure, onConflict);
return dbContext.Set<T>().Where(x => x.TestRun == runId).ToList();
case InsertStrategy.InsertAsync:
await dbContext.ExecuteBulkInsertAsync(entities, actualConfigure, onConflict);
return await dbContext.Set<T>().Where(x => x.TestRun == runId).ToListAsync();
default:
throw new NotImplementedException();
}
}
}