|
6 | 6 | using CommunityToolkit.Datasync.Server.Test.Helpers;
|
7 | 7 | using CommunityToolkit.Datasync.TestCommon.Databases;
|
8 | 8 | using Microsoft.EntityFrameworkCore;
|
| 9 | +using Microsoft.VisualStudio.TestPlatform.Utilities; |
9 | 10 | using Xunit.Abstractions;
|
10 | 11 |
|
11 | 12 | namespace CommunityToolkit.Datasync.Server.Test.Live;
|
12 | 13 |
|
13 | 14 | [ExcludeFromCodeCoverage]
|
14 | 15 | [Collection("LiveTestsCollection")]
|
15 |
| -public class MySQL_Controller_Tests : LiveControllerTests<MysqlEntityMovie> |
| 16 | +public class MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<MysqlEntityMovie>, IAsyncLifetime |
16 | 17 | {
|
17 | 18 | #region Setup
|
18 |
| - private readonly DatabaseFixture _fixture; |
19 | 19 | 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 = []; |
22 | 22 |
|
23 |
| - public MySQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base() |
| 23 | + public async Task InitializeAsync() |
24 | 24 | {
|
25 |
| - this._fixture = fixture; |
26 |
| - this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_MYSQL_CONNECTIONSTRING"); |
27 | 25 | if (!string.IsNullOrEmpty(this.connectionString))
|
28 | 26 | {
|
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(); |
33 | 42 | }
|
34 | 43 | }
|
35 | 44 |
|
36 | 45 | private MysqlDbContext Context { get; set; }
|
37 | 46 |
|
38 |
| - protected override string DriverName { get; } = "PgSQL"; |
| 47 | + protected override string DriverName { get; } = "MySQL"; |
39 | 48 |
|
40 | 49 | protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
|
41 | 50 |
|
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); |
44 | 53 |
|
45 |
| - protected override Task<int> GetEntityCountAsync() |
46 |
| - => Task.FromResult(Context.Movies.Count()); |
| 54 | + protected override async Task<int> GetEntityCountAsync() |
| 55 | + => await Context.Movies.CountAsync(); |
47 | 56 |
|
48 | 57 | protected override Task<IRepository<MysqlEntityMovie>> GetPopulatedRepositoryAsync()
|
49 | 58 | => Task.FromResult<IRepository<MysqlEntityMovie>>(new EntityTableRepository<MysqlEntityMovie>(Context));
|
|
0 commit comments