Skip to content

Commit 07566ce

Browse files
authored
(#347) Added MSSQL TestContainer Fixture (#351)
1 parent 5c624a3 commit 07566ce

File tree

9 files changed

+53
-124
lines changed

9 files changed

+53
-124
lines changed

Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<PackageVersion Include="System.Text.Json" Version="9.0.4" />
4141
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
4242
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="9.0.4" />
43+
<PackageVersion Include="TestContainers.MsSql" Version="4.4.0" />
4344
<PackageVersion Include="TestContainers.MySql" Version="4.4.0" />
4445
<PackageVersion Include="TestContainers.PostgreSql" Version="4.4.0" />
4546
<PackageVersion Include="xunit" Version="2.9.3" />

infra/modules/postgresql.bicep

-89
This file was deleted.

infra/resources.bicep

-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ var appServicePlanName = 'asp-${resourceToken}'
3333
var appServiceName = 'web-${resourceToken}'
3434
var azsqlServerName = 'sql-${resourceToken}'
3535
var cosmosServerName = 'cosmos-${resourceToken}'
36-
var pgsqlServerName = 'pgsql-${resourceToken}'
3736
var mongoServerName = 'mongo-${resourceToken}'
3837
var mongoaciServerName = 'mongoaci-${resourceToken}'
3938

@@ -67,19 +66,6 @@ module azuresql './modules/azuresql.bicep' = {
6766
}
6867
}
6968

70-
module pgsql './modules/postgresql.bicep' = {
71-
name: 'pgsql-deployment-${resourceToken}'
72-
params: {
73-
location: location
74-
tags: tags
75-
databaseName: testDatabaseName
76-
firewallRules: clientIpFirewallRules
77-
sqlServerName: pgsqlServerName
78-
sqlAdminUsername: sqlAdminUsername
79-
sqlAdminPassword: sqlAdminPassword
80-
}
81-
}
82-
8369
module cosmos './modules/cosmos.bicep' = {
8470
name: 'cosmos-deployment-${resourceToken}'
8571
params: {
@@ -136,5 +122,4 @@ output AZSQL_CONNECTIONSTRING string = azuresql.outputs.AZSQL_CONNECTIONSTRING
136122
output COSMOS_CONNECTIONSTRING string = cosmos.outputs.COSMOS_CONNECTIONSTRING
137123
output MONGO_CONNECTIONSTRING string = mongodb.outputs.MONGO_CONNECTIONSTRING
138124
output MONGOACI_CONNECTIONSTRING string = mongoaci.outputs.MONGO_CONNECTIONSTRING
139-
output PGSQL_CONNECTIONSTRING string = pgsql.outputs.PGSQL_CONNECTIONSTRING
140125
output SERVICE_ENDPOINT string = app_service.outputs.SERVICE_ENDPOINT

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using CommunityToolkit.Datasync.TestCommon;
66
using CommunityToolkit.Datasync.TestCommon.Databases;
7+
using CommunityToolkit.Datasync.TestCommon.Fixtures;
78
using Microsoft.EntityFrameworkCore;
89
using Xunit.Abstractions;
910

@@ -13,19 +14,16 @@ namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;
1314

1415
[ExcludeFromCodeCoverage]
1516
[Collection("LiveTestsCollection")]
16-
public class AzureSqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<AzureSqlEntityMovie>, IAsyncLifetime
17+
public class AzureSqlEntityTableRepository_Tests(MsSqlDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<AzureSqlEntityMovie>, IClassFixture<MsSqlDatabaseFixture>, IAsyncLifetime
1718
{
1819
#region Setup
1920
private readonly Random random = new();
2021
private List<AzureSqlEntityMovie> movies = [];
2122

2223
public async Task InitializeAsync()
2324
{
24-
if (!string.IsNullOrEmpty(ConnectionStrings.AzureSql))
25-
{
26-
Context = await AzureSqlDbContext.CreateContextAsync(ConnectionStrings.AzureSql, output);
27-
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
28-
}
25+
Context = await AzureSqlDbContext.CreateContextAsync(fixture.ConnectionString, output);
26+
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
2927
}
3028

3129
public async Task DisposeAsync()
@@ -38,7 +36,7 @@ public async Task DisposeAsync()
3836

3937
private AzureSqlDbContext Context { get; set; }
4038

41-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.AzureSql);
39+
protected override bool CanRunLiveTests() => true;
4240

4341
protected override async Task<AzureSqlEntityMovie> GetEntityAsync(string id)
4442
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);

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

+5-12
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,24 @@
55
using CommunityToolkit.Datasync.Server.EntityFrameworkCore;
66
using CommunityToolkit.Datasync.Server.Test.Helpers;
77
using CommunityToolkit.Datasync.TestCommon.Databases;
8+
using CommunityToolkit.Datasync.TestCommon.Fixtures;
89
using Microsoft.EntityFrameworkCore;
910
using Xunit.Abstractions;
1011

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

1314
[ExcludeFromCodeCoverage]
1415
[Collection("LiveTestsCollection")]
15-
public class AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<AzureSqlEntityMovie>, IAsyncLifetime
16+
public class AzureSQL_Controller_Tests(MsSqlDatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<AzureSqlEntityMovie>, IClassFixture<MsSqlDatabaseFixture>, IAsyncLifetime
1617
{
1718
#region Setup
1819
private readonly Random random = new();
1920
private List<AzureSqlEntityMovie> movies = [];
2021

2122
public async Task InitializeAsync()
2223
{
23-
if (!string.IsNullOrEmpty(ConnectionStrings.AzureSql))
24-
{
25-
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
26-
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
27-
// to the database then change clearEntities to true.
28-
output.WriteLine($"AzureSqlIsInitialized = {fixture.AzureSqlIsInitialized}");
29-
Context = await AzureSqlDbContext.CreateContextAsync(ConnectionStrings.AzureSql, output, clearEntities: !fixture.AzureSqlIsInitialized);
30-
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
31-
fixture.AzureSqlIsInitialized = true;
32-
}
24+
Context = await AzureSqlDbContext.CreateContextAsync(fixture.ConnectionString, output);
25+
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
3326
}
3427

3528
public async Task DisposeAsync()
@@ -44,7 +37,7 @@ public async Task DisposeAsync()
4437

4538
protected override string DriverName { get; } = "AzureSQL";
4639

47-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.AzureSql);
40+
protected override bool CanRunLiveTests() => true;
4841

4942
protected override Task<AzureSqlEntityMovie> GetEntityAsync(string id)
5043
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));

tests/CommunityToolkit.Datasync.TestCommon/CommunityToolkit.Datasync.TestCommon.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PackageReference Include="Microsoft.Spatial" />
2121
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
2222
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
23+
<PackageReference Include="TestContainers.MsSql" />
2324
<PackageReference Include="TestContainers.MySql" />
2425
<PackageReference Include="TestContainers.PostgreSql" />
2526
</ItemGroup>

tests/CommunityToolkit.Datasync.TestCommon/Databases/ConnectionStrings.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
77
[ExcludeFromCodeCoverage]
88
public static class ConnectionStrings
99
{
10-
public static readonly string AzureSql = Environment.GetEnvironmentVariable("AZSQL_CONNECTION_STRING");
1110
public static readonly string CosmosDb = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING");
1211
public static readonly string MongoCommunity = Environment.GetEnvironmentVariable("MONGOACI_CONNECTION_STRING");
1312
public static readonly string CosmosMongo = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Testcontainers.MsSql;
6+
using Xunit;
7+
8+
namespace CommunityToolkit.Datasync.TestCommon.Fixtures;
9+
10+
[ExcludeFromCodeCoverage]
11+
public class MsSqlDatabaseFixture : IAsyncLifetime
12+
{
13+
private readonly MsSqlContainer _container;
14+
15+
public MsSqlDatabaseFixture()
16+
{
17+
this._container = new MsSqlBuilder()
18+
.Build();
19+
}
20+
21+
/// <inheritdoc />
22+
public async Task DisposeAsync()
23+
{
24+
if (this._container is not null)
25+
{
26+
await this._container.DisposeAsync();
27+
}
28+
}
29+
30+
/// <inheritdoc />
31+
public async Task InitializeAsync()
32+
{
33+
await this._container.StartAsync();
34+
ConnectionString = this._container.GetConnectionString();
35+
}
36+
37+
/// <summary>
38+
/// The connection string for the MySQL database.
39+
/// </summary>
40+
public string ConnectionString { get; private set; } = string.Empty;
41+
}

tests/CommunityToolkit.Datasync.TestCommon/Fixtures/MySqlTestFixture.cs

Whitespace-only changes.

0 commit comments

Comments
 (0)