Skip to content

Commit dc8a637

Browse files
committed
Change RegressionTestHelper fix
1 parent 25cffd9 commit dc8a637

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

backend/FwLite/LcmCrdt.Tests/Data/MigrationTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
namespace LcmCrdt.Tests.Data;
1111

1212
[Collection("MigrationTests")]
13-
public class MigrationTests
13+
public class MigrationTests : IAsyncLifetime
1414
{
15+
private readonly RegressionTestHelper _helper = new("MigrationTest");
1516
private static readonly JsonSerializerOptions IndentedHarmonyJsonOptions = new(TestJsonOptions.Harmony())
1617
{
1718
WriteIndented = true
@@ -24,12 +25,21 @@ internal static void Init()
2425
VerifierSettings.OmitContentFromException();
2526
}
2627

28+
public Task InitializeAsync()
29+
{
30+
return Task.CompletedTask;
31+
}
32+
33+
public async Task DisposeAsync()
34+
{
35+
await _helper.DisposeAsync();
36+
}
37+
2738
[Theory]
2839
[InlineData(RegressionTestHelper.RegressionVersion.v1)]
2940
[InlineData(RegressionTestHelper.RegressionVersion.v2)]
3041
public async Task GetEntries_WorksAfterMigrationFromScriptedDb(RegressionTestHelper.RegressionVersion regressionVersion)
3142
{
32-
await using RegressionTestHelper _helper = new($"{nameof(GetEntries_WorksAfterMigrationFromScriptedDb)}-{regressionVersion}");
3343
await _helper.InitializeAsync(regressionVersion);
3444
var api = _helper.Services.GetRequiredService<IMiniLcmApi>();
3545
var hasEntries = false;
@@ -48,7 +58,6 @@ public async Task GetEntries_WorksAfterMigrationFromScriptedDb(RegressionTestHel
4858
[Trait("Category", "Verified")]
4959
public async Task VerifyAfterMigrationFromScriptedDb(RegressionTestHelper.RegressionVersion regressionVersion)
5060
{
51-
await using RegressionTestHelper _helper = new($"{nameof(VerifyAfterMigrationFromScriptedDb)}-{regressionVersion}");
5261
await _helper.InitializeAsync(regressionVersion);
5362
var api = _helper.Services.GetRequiredService<IMiniLcmApi>();
5463
var crdtConfig = _helper.Services.GetRequiredService<IOptions<CrdtConfig>>().Value;
@@ -101,7 +110,6 @@ await Task.WhenAll(
101110
[Trait("Category", "Verified")]
102111
public async Task VerifyRegeneratedSnapshotsAfterMigrationFromScriptedDb(RegressionTestHelper.RegressionVersion regressionVersion)
103112
{
104-
await using RegressionTestHelper _helper = new($"{nameof(VerifyRegeneratedSnapshotsAfterMigrationFromScriptedDb)}-{regressionVersion}");
105113
await _helper.InitializeAsync(regressionVersion);
106114
var api = _helper.Services.GetRequiredService<IMiniLcmApi>();
107115
var crdtConfig = _helper.Services.GetRequiredService<IOptions<CrdtConfig>>().Value;

backend/FwLite/LcmCrdt.Tests/Data/RegressionTestHelper.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@
66

77
namespace 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

Comments
 (0)