66
77namespace LcmCrdt . Tests . Data ;
88
9- public class RegressionTestHelper ( string dbName ) : IAsyncDisposable
9+ public class RegressionTestHelper ( string projectName ) : IAsyncLifetime
1010{
1111 private IHost _host = null ! ;
1212 private AsyncServiceScope _asyncScope ;
13+ //unique db path per instance, so CurrentProjectService doesn't think a db has already run migrations
14+ private readonly CrdtProject _crdtProject = new ( projectName , $ "{ projectName } -{ Guid . NewGuid ( ) : N} .sqlite") ;
1315 public IServiceProvider Services => _asyncScope . ServiceProvider ;
1416
1517 private async Task InitDbFromScripts ( RegressionVersion version )
1618 {
1719 var initialSqlFile = GetFilePath ( $ "Scripts/{ version } .sql") ;
1820 var projectsService = _asyncScope . ServiceProvider . GetRequiredService < CurrentProjectService > ( ) ;
19- var crdtProject = new CrdtProject ( dbName , $ "{ dbName } .sqlite") ;
20- if ( File . Exists ( crdtProject . DbPath ) )
21- {
22- using var clearConn = new SqliteConnection ( $ "Data Source={ crdtProject . DbPath } ") ;
23- SqliteConnection . ClearPool ( clearConn ) ;
24- File . Delete ( crdtProject . DbPath ) ;
25- }
26- projectsService . SetupProjectContextForNewDb ( crdtProject ) ;
21+ projectsService . SetupProjectContextForNewDb ( _crdtProject ) ;
2722 await using var lcmCrdtDbContext = await _asyncScope . ServiceProvider . GetRequiredService < IDbContextFactory < LcmCrdtDbContext > > ( ) . CreateDbContextAsync ( ) ;
2823 var sql = await File . ReadAllTextAsync ( initialSqlFile ) ;
2924 var dbConnection = lcmCrdtDbContext . Database . GetDbConnection ( ) ;
@@ -39,7 +34,7 @@ private async Task InitDbFromScripts(RegressionVersion version)
3934 await dbConnection . CloseAsync ( ) ;
4035
4136 //setup again to trigger migrations
42- await projectsService . SetupProjectContext ( crdtProject ) ;
37+ await projectsService . SetupProjectContext ( _crdtProject ) ;
4338 }
4439
4540 public Task InitializeAsync ( )
@@ -57,14 +52,21 @@ public async Task InitializeAsync(RegressionVersion version)
5752 await InitDbFromScripts ( version ) ;
5853 }
5954
60- public async ValueTask DisposeAsync ( )
55+ public async Task DisposeAsync ( )
6156 {
6257 await _asyncScope . DisposeAsync ( ) ;
6358 if ( _host is IAsyncDisposable asyncDisposable ) await asyncDisposable . DisposeAsync ( ) ;
6459 else
6560 {
6661 _host . Dispose ( ) ;
6762 }
63+
64+ if ( File . Exists ( _crdtProject . DbPath ) )
65+ {
66+ using var connection = new SqliteConnection ( $ "Data Source={ _crdtProject . DbPath } ") ;
67+ SqliteConnection . ClearPool ( connection ) ;
68+ File . Delete ( _crdtProject . DbPath ) ;
69+ }
6870 }
6971
7072 private static string GetFilePath ( string name , [ CallerFilePath ] string sourceFile = "" )
0 commit comments