Skip to content

Commit

Permalink
(#238) Support IAsyncLifetime for MySQL (#241)
Browse files Browse the repository at this point in the history
* (#238) MySQL database tests use async lifetime.
  • Loading branch information
adrianhall authored Jan 31, 2025
1 parent f906f3d commit f30aa6c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

#pragma warning disable CS9113 // Parameter is unread.

namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class MysqlEntityTableRepository_Tests : RepositoryTests<MysqlEntityMovie>
public class MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<MysqlEntityMovie>, IAsyncLifetime
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<MysqlEntityMovie> movies;
private readonly Lazy<MysqlDbContext> _context;
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
private List<MysqlEntityMovie> movies = [];

public MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
public async Task InitializeAsync()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
this._context = new Lazy<MysqlDbContext>(() => MysqlDbContext.CreateContext(this.connectionString, output));
this.movies = Context.Movies.AsNoTracking().ToList();
Context = await MysqlDbContext.CreateContextAsync(this.connectionString, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}
}

public async Task DisposeAsync()
{
if (Context is not null)
{
await Context.DisposeAsync();
}
}

private MysqlDbContext Context { get => this._context.Value; }
private MysqlDbContext Context { get; set; }

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

protected override Task<MysqlEntityMovie> GetEntityAsync(string id)
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(Context.Movies.Count());
protected override async Task<int> GetEntityCountAsync()
=> await Context.Movies.CountAsync();

protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,53 @@
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class MySQL_Controller_Tests : LiveControllerTests<MysqlEntityMovie>
public class MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<MysqlEntityMovie>, IAsyncLifetime
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<MysqlEntityMovie> movies;
private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
private List<MysqlEntityMovie> movies = [];

public MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
public async Task InitializeAsync()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
output.WriteLine($"MysqlIsInitialized = {this._fixture.MysqlIsInitialized}");
Context = MysqlDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.MysqlIsInitialized);
this.movies = Context.Movies.AsNoTracking().ToList();
this._fixture.MysqlIsInitialized = true;
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
// to the database then change clearEntities to true.
output.WriteLine($"CosmosIsInitialized = {fixture.MysqlIsInitialized}");
Context = await MysqlDbContext.CreateContextAsync(this.connectionString, output, clearEntities: !fixture.MysqlIsInitialized);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
fixture.MysqlIsInitialized = true;
}
}

public async Task DisposeAsync()
{
if (Context is not null)
{
await Context.DisposeAsync();
}
}

private MysqlDbContext Context { get; set; }

protected override string DriverName { get; } = "PgSQL";
protected override string DriverName { get; } = "MySQL";

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

protected override Task<MysqlEntityMovie> GetEntityAsync(string id)
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));
protected override async Task<MysqlEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(Context.Movies.Count());
protected override async Task<int> GetEntityCountAsync()
=> await Context.Movies.CountAsync();

protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
[ExcludeFromCodeCoverage]
public class MysqlDbContext(DbContextOptions<MysqlDbContext> options) : BaseDbContext<MysqlDbContext, MysqlEntityMovie>(options)
{
public static MysqlDbContext CreateContext(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
public static async Task<MysqlDbContext> CreateContextAsync(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
{
if (string.IsNullOrEmpty(connectionString))
{
Expand All @@ -22,18 +22,18 @@ public static MysqlDbContext CreateContext(string connectionString, ITestOutputH
.EnableLogging(output);
MysqlDbContext context = new(optionsBuilder.Options);

context.InitializeDatabase(clearEntities);
context.PopulateDatabase();
await context.InitializeDatabaseAsync(clearEntities);
await context.PopulateDatabaseAsync();
return context;
}

internal void InitializeDatabase(bool clearEntities)
internal async Task InitializeDatabaseAsync(bool clearEntities)
{
Database.EnsureCreated();
await Database.EnsureCreatedAsync();

if (clearEntities)
{
ExecuteRawSqlOnEachEntity(@"DELETE FROM {0}");
await ExecuteRawSqlOnEachEntityAsync("DELETE FROM {0}");
}
}

Expand Down

0 comments on commit f30aa6c

Please sign in to comment.