Skip to content

Commit 37b32a7

Browse files
Fix tests
1 parent 009f6b1 commit 37b32a7

13 files changed

Lines changed: 184 additions & 159 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert.SqlServer/SqlServerDialectBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ public override string BuildMoveDataSql<T>(DbContext context, string source,
8484

8585
protected override string GetExcludedColumnName(string columnName)
8686
{
87-
return $"SOURCE.{Quote(columnName)}";
87+
return $"SOURCE.{columnName}";
8888
}
8989
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContainer/TestDbContainer.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public abstract class TestDbContainer<TDbContext> : IAsyncLifetime
1414
private static readonly TimeSpan WaitTime = TimeSpan.FromSeconds(30);
1515
protected readonly IDatabaseContainer? DbContainer;
1616

17-
public TDbContext DbContext { get; private set; } = null!;
18-
1917
protected TestDbContainer()
2018
{
2119
DbContainer = GetDbContainer();
@@ -36,32 +34,41 @@ public async Task InitializeAsync()
3634
{
3735
await DbContainer.StartAsync();
3836
}
37+
}
3938

40-
DbContext = new TDbContext
39+
public async Task<TDbContext> CreateContextAsync()
40+
{
41+
var dbContext = new TDbContext
4142
{
4243
ConfigureOptions = Configure
4344
};
44-
DbContext.Database.SetConnectionString(GetConnectionString());
4545

46-
await EnsureConnectedAsync();
46+
dbContext.Database.SetConnectionString(GetConnectionString());
4747

48-
await DbContext.Database.EnsureCreatedAsync();
48+
await EnsureConnectedAsync(dbContext);
49+
try
50+
{
51+
await dbContext.Database.EnsureCreatedAsync();
52+
}
53+
catch
54+
{
55+
// Often fails with SQL server.
56+
}
57+
58+
return dbContext;
4959
}
5060

51-
protected virtual async Task EnsureConnectedAsync()
61+
protected virtual async Task EnsureConnectedAsync(TDbContext context)
5262
{
5363
using var cts = new CancellationTokenSource(WaitTime);
54-
while (!await DbContext.Database.CanConnectAsync(cts.Token))
64+
while (!await context.Database.CanConnectAsync(cts.Token))
5565
{
5666
await Task.Delay(100, cts.Token);
5767
}
5868
}
5969

6070
public async Task DisposeAsync()
6171
{
62-
// await DbContext.Database.EnsureDeletedAsync();
63-
await DbContext.DisposeAsync();
64-
6572
if (DbContainer != null)
6673
{
6774
await DbContainer.DisposeAsync();

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContainer/TestDbContainerMySql.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class TestDbContainerMySql<TDbContext> : TestDbContainer<TDbContext>
1515
protected override IDatabaseContainer? GetDbContainer()
1616
{
1717
return new MySqlBuilder()
18+
.WithReuse(true)
1819
.WithCommand("--log-bin-trust-function-creators=1", "--local-infile=1")
1920
.Build();
2021
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContainer/TestDbContainerPostgreSql.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using DotNet.Testcontainers.Containers;
1+
using DotNet.Testcontainers.Containers;
22

33
using Microsoft.EntityFrameworkCore;
44

@@ -15,6 +15,7 @@ public class TestDbContainerPostgreSql<TDbContext> : TestDbContainer<TDbContext>
1515
protected override IDatabaseContainer? GetDbContainer()
1616
{
1717
return new PostgreSqlBuilder()
18+
.WithReuse(true)
1819
.WithDatabase("testdb")
1920
.WithUsername("testuser")
2021
.WithPassword("testpassword")

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContainer/TestDbContainerSqlServer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using DotNet.Testcontainers.Containers;
1+
using DotNet.Testcontainers.Containers;
22

33
using Microsoft.EntityFrameworkCore;
44

@@ -14,7 +14,9 @@ public class TestDbContainerSqlServer<TDbContext> : TestDbContainer<TDbContext>
1414
{
1515
protected override IDatabaseContainer? GetDbContainer()
1616
{
17-
return new MsSqlBuilder().Build();
17+
return new MsSqlBuilder()
18+
.WithReuse(true)
19+
.Build();
1820
}
1921

2022
protected override void Configure(DbContextOptionsBuilder optionsBuilder)

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContainer/TestDbContainerSqlite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override void Configure(DbContextOptionsBuilder optionsBuilder)
2525
.UseBulkInsertSqlite();
2626
}
2727

28-
protected override Task EnsureConnectedAsync()
28+
protected override Task EnsureConnectedAsync(TDbContext context)
2929
{
3030
return Task.CompletedTask;
3131
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
22
using System.ComponentModel.DataAnnotations.Schema;
33

44
using Microsoft.EntityFrameworkCore;
@@ -20,6 +20,9 @@ public class TestEntity
2020
[Column("some_price")]
2121
public decimal Price { get; set; }
2222

23+
[Column("test_run")]
24+
public Guid TestRun { get; set; }
25+
2326
[Column("the_identifier")]
2427
public Guid Identifier { get; set; }
2528

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ 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; }
1720
}
1821

0 commit comments

Comments
 (0)