Skip to content

Commit f30aa6c

Browse files
authored
(#238) Support IAsyncLifetime for MySQL (#241)
* (#238) MySQL database tests use async lifetime.
1 parent f906f3d commit f30aa6c

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,45 @@
77
using Microsoft.EntityFrameworkCore;
88
using Xunit.Abstractions;
99

10+
#pragma warning disable CS9113 // Parameter is unread.
11+
1012
namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;
1113

1214
[ExcludeFromCodeCoverage]
1315
[Collection("LiveTestsCollection")]
14-
public class MysqlEntityTableRepository_Tests : RepositoryTests<MysqlEntityMovie>
16+
public class MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<MysqlEntityMovie>, IAsyncLifetime
1517
{
1618
#region Setup
17-
private readonly DatabaseFixture _fixture;
1819
private readonly Random random = new();
19-
private readonly string connectionString;
20-
private readonly List<MysqlEntityMovie> movies;
21-
private readonly Lazy<MysqlDbContext> _context;
20+
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
21+
private List<MysqlEntityMovie> movies = [];
2222

23-
public MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
23+
public async Task InitializeAsync()
2424
{
25-
this._fixture = fixture;
26-
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
2725
if (!string.IsNullOrEmpty(this.connectionString))
2826
{
29-
this._context = new Lazy<MysqlDbContext>(() => MysqlDbContext.CreateContext(this.connectionString, output));
30-
this.movies = Context.Movies.AsNoTracking().ToList();
27+
Context = await MysqlDbContext.CreateContextAsync(this.connectionString, output);
28+
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
29+
}
30+
}
31+
32+
public async Task DisposeAsync()
33+
{
34+
if (Context is not null)
35+
{
36+
await Context.DisposeAsync();
3137
}
3238
}
3339

34-
private MysqlDbContext Context { get => this._context.Value; }
40+
private MysqlDbContext Context { get; set; }
3541

3642
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
3743

38-
protected override Task<MysqlEntityMovie> GetEntityAsync(string id)
39-
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));
44+
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
45+
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
4046

41-
protected override Task<int> GetEntityCountAsync()
42-
=> Task.FromResult(Context.Movies.Count());
47+
protected override async Task<int> GetEntityCountAsync()
48+
=> await Context.Movies.CountAsync();
4349

4450
protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
4551
=> Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));

tests/CommunityToolkit.Datasync.Server.Test/Live/MySQL_Controller_Tests.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,53 @@
66
using CommunityToolkit.Datasync.Server.Test.Helpers;
77
using CommunityToolkit.Datasync.TestCommon.Databases;
88
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.VisualStudio.TestPlatform.Utilities;
910
using Xunit.Abstractions;
1011

1112
namespace CommunityToolkit.Datasync.Server.Test.Live;
1213

1314
[ExcludeFromCodeCoverage]
1415
[Collection("LiveTestsCollection")]
15-
public class MySQL_Controller_Tests : LiveControllerTests<MysqlEntityMovie>
16+
public class MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<MysqlEntityMovie>, IAsyncLifetime
1617
{
1718
#region Setup
18-
private readonly DatabaseFixture _fixture;
1919
private readonly Random random = new();
20-
private readonly string connectionString;
21-
private readonly List<MysqlEntityMovie> movies;
20+
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
21+
private List<MysqlEntityMovie> movies = [];
2222

23-
public MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
23+
public async Task InitializeAsync()
2424
{
25-
this._fixture = fixture;
26-
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
2725
if (!string.IsNullOrEmpty(this.connectionString))
2826
{
29-
output.WriteLine($"MysqlIsInitialized = {this._fixture.MysqlIsInitialized}");
30-
Context = MysqlDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.MysqlIsInitialized);
31-
this.movies = Context.Movies.AsNoTracking().ToList();
32-
this._fixture.MysqlIsInitialized = true;
27+
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
28+
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
29+
// to the database then change clearEntities to true.
30+
output.WriteLine($"CosmosIsInitialized = {fixture.MysqlIsInitialized}");
31+
Context = await MysqlDbContext.CreateContextAsync(this.connectionString, output, clearEntities: !fixture.MysqlIsInitialized);
32+
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
33+
fixture.MysqlIsInitialized = true;
34+
}
35+
}
36+
37+
public async Task DisposeAsync()
38+
{
39+
if (Context is not null)
40+
{
41+
await Context.DisposeAsync();
3342
}
3443
}
3544

3645
private MysqlDbContext Context { get; set; }
3746

38-
protected override string DriverName { get; } = "PgSQL";
47+
protected override string DriverName { get; } = "MySQL";
3948

4049
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
4150

42-
protected override Task<MysqlEntityMovie> GetEntityAsync(string id)
43-
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));
51+
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
52+
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
4453

45-
protected override Task<int> GetEntityCountAsync()
46-
=> Task.FromResult(Context.Movies.Count());
54+
protected override async Task<int> GetEntityCountAsync()
55+
=> await Context.Movies.CountAsync();
4756

4857
protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
4958
=> Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));

tests/CommunityToolkit.Datasync.TestCommon/Databases/MySQL/MysqlDbContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
1010
[ExcludeFromCodeCoverage]
1111
public class MysqlDbContext(DbContextOptions<MysqlDbContext> options) : BaseDbContext<MysqlDbContext, MysqlEntityMovie>(options)
1212
{
13-
public static MysqlDbContext CreateContext(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
13+
public static async Task<MysqlDbContext> CreateContextAsync(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
1414
{
1515
if (string.IsNullOrEmpty(connectionString))
1616
{
@@ -22,18 +22,18 @@ public static MysqlDbContext CreateContext(string connectionString, ITestOutputH
2222
.EnableLogging(output);
2323
MysqlDbContext context = new(optionsBuilder.Options);
2424

25-
context.InitializeDatabase(clearEntities);
26-
context.PopulateDatabase();
25+
await context.InitializeDatabaseAsync(clearEntities);
26+
await context.PopulateDatabaseAsync();
2727
return context;
2828
}
2929

30-
internal void InitializeDatabase(bool clearEntities)
30+
internal async Task InitializeDatabaseAsync(bool clearEntities)
3131
{
32-
Database.EnsureCreated();
32+
await Database.EnsureCreatedAsync();
3333

3434
if (clearEntities)
3535
{
36-
ExecuteRawSqlOnEachEntity(@"DELETE FROM {0}");
36+
await ExecuteRawSqlOnEachEntityAsync("DELETE FROM {0}");
3737
}
3838
}
3939

0 commit comments

Comments
 (0)