diff --git a/tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs b/tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs index 42d639d..2e53932 100644 --- a/tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/MySqlEntityTableRepository_Tests.cs @@ -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 +public class MysqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests, IAsyncLifetime { #region Setup - private readonly DatabaseFixture _fixture; private readonly Random random = new(); - private readonly string connectionString; - private readonly List movies; - private readonly Lazy _context; + private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING"); + private List 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.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 GetEntityAsync(string id) - => Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id)); + protected override async Task GetEntityAsync(string id) + => await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id); - protected override Task GetEntityCountAsync() - => Task.FromResult(Context.Movies.Count()); + protected override async Task GetEntityCountAsync() + => await Context.Movies.CountAsync(); protected override Task> GetPopulatedRepositoryAsync() => Task.FromResult>(new EntityTableRepository(Context)); diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Live/MySQL_Controller_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Live/MySQL_Controller_Tests.cs index 83eff51..bcef70c 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Live/MySQL_Controller_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Live/MySQL_Controller_Tests.cs @@ -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 +public class MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests, IAsyncLifetime { #region Setup - private readonly DatabaseFixture _fixture; private readonly Random random = new(); - private readonly string connectionString; - private readonly List movies; + private readonly string connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING"); + private List 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 GetEntityAsync(string id) - => Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id)); + protected override async Task GetEntityAsync(string id) + => await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id); - protected override Task GetEntityCountAsync() - => Task.FromResult(Context.Movies.Count()); + protected override async Task GetEntityCountAsync() + => await Context.Movies.CountAsync(); protected override Task> GetPopulatedRepositoryAsync() => Task.FromResult>(new EntityTableRepository(Context)); diff --git a/tests/CommunityToolkit.Datasync.TestCommon/Databases/MySQL/MysqlDbContext.cs b/tests/CommunityToolkit.Datasync.TestCommon/Databases/MySQL/MysqlDbContext.cs index bfc135f..df951d2 100644 --- a/tests/CommunityToolkit.Datasync.TestCommon/Databases/MySQL/MysqlDbContext.cs +++ b/tests/CommunityToolkit.Datasync.TestCommon/Databases/MySQL/MysqlDbContext.cs @@ -10,7 +10,7 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases; [ExcludeFromCodeCoverage] public class MysqlDbContext(DbContextOptions options) : BaseDbContext(options) { - public static MysqlDbContext CreateContext(string connectionString, ITestOutputHelper output = null, bool clearEntities = true) + public static async Task CreateContextAsync(string connectionString, ITestOutputHelper output = null, bool clearEntities = true) { if (string.IsNullOrEmpty(connectionString)) { @@ -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}"); } }