File tree Expand file tree Collapse file tree
tests/PhenX.EntityFrameworkCore.BulkInsert.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ protected TestDbContainer()
2020 DbContainer = GetDbContainer ( ) ;
2121 }
2222
23+ protected string GetRandomContainerName ( ) => "phenx-bulk-insert-test-" + Guid . NewGuid ( ) ;
24+
2325 protected abstract IDatabaseContainer ? GetDbContainer ( ) ;
2426
2527 protected virtual string GetConnectionString ( )
@@ -35,24 +37,31 @@ public async Task InitializeAsync()
3537 {
3638 await DbContainer . StartAsync ( ) ;
3739 }
40+ }
41+
42+ public async Task DisposeAsync ( )
43+ {
44+ if ( DbContainer != null )
45+ {
46+ await DbContainer . DisposeAsync ( ) ;
47+ }
48+ }
3849
50+ public async Task InitializeDbContextAsync ( )
51+ {
3952 DbContext = new TDbContext
4053 {
4154 ConfigureOptions = Configure
4255 } ;
43- DbContext . Database . SetConnectionString ( GetConnectionString ( ) ) ;
4456
57+ DbContext . Database . SetConnectionString ( GetConnectionString ( ) ) ;
4558 await DbContext . Database . EnsureCreatedAsync ( ) ;
4659 }
4760
48- public async Task DisposeAsync ( )
61+ public async Task DisposeDbContextAsync ( )
4962 {
50- // await DbContext.Database.EnsureDeletedAsync();
63+ await DbContext . Database . EnsureDeletedAsync ( ) ;
5164 await DbContext . DisposeAsync ( ) ;
52-
53- if ( DbContainer != null )
54- {
55- await DbContainer . DisposeAsync ( ) ;
56- }
65+ DbContext = null ! ;
5766 }
5867}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ public class TestDbContainerPostgreSql<TDbContext> : TestDbContainer<TDbContext>
1818 . WithDatabase ( "testdb" )
1919 . WithUsername ( "testuser" )
2020 . WithPassword ( "testpassword" )
21+ . WithName ( GetRandomContainerName ( ) )
2122 . Build ( ) ;
2223 }
2324
Original file line number Diff line number Diff line change 11using DotNet . Testcontainers . Containers ;
22
3+ using Microsoft . Data . SqlClient ;
34using Microsoft . EntityFrameworkCore ;
45
56using PhenX . EntityFrameworkCore . BulkInsert . SqlServer ;
@@ -14,7 +15,19 @@ public class TestDbContainerSqlServer<TDbContext> : TestDbContainer<TDbContext>
1415{
1516 protected override IDatabaseContainer ? GetDbContainer ( )
1617 {
17- return new MsSqlBuilder ( ) . Build ( ) ;
18+ return new MsSqlBuilder ( )
19+ . WithName ( GetRandomContainerName ( ) )
20+ . Build ( ) ;
21+ }
22+
23+ protected override string GetConnectionString ( )
24+ {
25+ var connectionString = new SqlConnectionStringBuilder ( base . GetConnectionString ( ) )
26+ {
27+ InitialCatalog = Guid . NewGuid ( ) . ToString ( "D" )
28+ } ;
29+
30+ return connectionString . ToString ( ) ;
1831 }
1932
2033 protected override void Configure ( DbContextOptionsBuilder optionsBuilder )
Original file line number Diff line number Diff line change 1313 <PrivateAssets >all</PrivateAssets >
1414 <IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
1515 </PackageReference >
16- <PackageReference Include =" Meziantou.Xunit.ParallelTestFramework" Version =" 2.3.0" />
1716 <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" 17.13.0" />
1817 <PackageReference Include =" xunit" Version =" 2.9.3" />
1918 <PackageReference Include =" xunit.runner.visualstudio" Version =" 3.0.2" >
Original file line number Diff line number Diff line change 77
88namespace PhenX . EntityFrameworkCore . BulkInsert . Tests . Tests . Basic ;
99
10- public abstract class BasicTestsBase : IAsyncLifetime
10+ public abstract class BasicTestsBase < TFixture > : IClassFixture < TFixture > , IAsyncLifetime
11+ where TFixture : TestDbContainer < TestDbContext >
1112{
12- protected BasicTestsBase ( TestDbContainer < TestDbContext > dbContainer )
13+ protected BasicTestsBase ( TFixture dbContainer )
1314 {
1415 DbContainer = dbContainer ;
1516 }
1617
17- protected TestDbContainer < TestDbContext > DbContainer { get ; }
18+ protected TFixture DbContainer { get ; }
1819
1920 [ Fact ]
2021 public async Task InsertsEntitiesSuccessfully ( )
@@ -362,7 +363,13 @@ public void BulkInsert_WithOpenTransaction_RollsBackOnFailure_Sync()
362363 Assert . DoesNotContain ( insertedEntities , e => e . Name == "EntityWithTxFail2" ) ;
363364 }
364365
365- public Task InitializeAsync ( ) => DbContainer . InitializeAsync ( ) ;
366+ public Task InitializeAsync ( )
367+ {
368+ return DbContainer . InitializeDbContextAsync ( ) ;
369+ }
366370
367- public Task DisposeAsync ( ) => DbContainer . DisposeAsync ( ) ;
371+ public Task DisposeAsync ( )
372+ {
373+ return DbContainer . DisposeDbContextAsync ( ) ;
374+ }
368375}
Original file line number Diff line number Diff line change 66namespace PhenX . EntityFrameworkCore . BulkInsert . Tests . Tests . Basic ;
77
88[ Trait ( "Category" , "PostgreSql" ) ]
9- public class BasicTestsPostgreSql : BasicTestsBase
9+ public class BasicTestsPostgreSql : BasicTestsBase < TestDbContainerPostgreSql < TestDbContext > >
1010{
11- public BasicTestsPostgreSql ( ) : base ( new TestDbContainerPostgreSql < TestDbContext > ( ) )
11+ public BasicTestsPostgreSql ( TestDbContainerPostgreSql < TestDbContext > dbContainer ) : base ( dbContainer )
1212 {
1313 }
1414}
Original file line number Diff line number Diff line change 66namespace PhenX . EntityFrameworkCore . BulkInsert . Tests . Tests . Basic ;
77
88[ Trait ( "Category" , "SqlServer" ) ]
9- public class BasicTestsSqlServer : BasicTestsBase
9+ public class BasicTestsSqlServer : BasicTestsBase < TestDbContainerSqlServer < TestDbContext > >
1010{
11- public BasicTestsSqlServer ( ) : base ( new TestDbContainerSqlServer < TestDbContext > ( ) )
11+ public BasicTestsSqlServer ( TestDbContainerSqlServer < TestDbContext > dbContainer ) : base ( dbContainer )
1212 {
1313 }
1414}
Original file line number Diff line number Diff line change 66namespace PhenX . EntityFrameworkCore . BulkInsert . Tests . Tests . Basic ;
77
88[ Trait ( "Category" , "Sqlite" ) ]
9- public class BasicTestsSqlite : BasicTestsBase
9+ public class BasicTestsSqlite : BasicTestsBase < TestDbContainerSqlite < TestDbContext > >
1010{
11- public BasicTestsSqlite ( ) : base ( new TestDbContainerSqlite < TestDbContext > ( ) )
11+ public BasicTestsSqlite ( TestDbContainerSqlite < TestDbContext > dbContainer ) : base ( dbContainer )
1212 {
1313 }
1414}
You can’t perform that action at this time.
0 commit comments