Skip to content

Commit ca2650c

Browse files
authored
(#346) Added Mongo TestContainer fixture (#352)
1 parent 07566ce commit ca2650c

File tree

8 files changed

+55
-106
lines changed

8 files changed

+55
-106
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
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.MongoDb" Version="4.4.0" />
4344
<PackageVersion Include="TestContainers.MsSql" Version="4.4.0" />
4445
<PackageVersion Include="TestContainers.MySql" Version="4.4.0" />
4546
<PackageVersion Include="TestContainers.PostgreSql" Version="4.4.0" />

infra/modules/aci-mongodb.bicep

Lines changed: 0 additions & 74 deletions
This file was deleted.

infra/resources.bicep

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,6 @@ module mongodb './modules/cosmos-mongodb.bicep' = {
8888
}
8989
}
9090

91-
module mongoaci './modules/aci-mongodb.bicep' = {
92-
name: 'mongoaci-deployment-${resourceToken}'
93-
params: {
94-
location: location
95-
tags: tags
96-
serverName: mongoaciServerName
97-
administratorPassword: sqlAdminPassword
98-
99-
}
100-
}
101-
10291
module app_service './modules/appservice.bicep' = {
10392
name: 'appsvc-deployment-${resourceToken}'
10493
params: {
@@ -121,5 +110,4 @@ module app_service './modules/appservice.bicep' = {
121110
output AZSQL_CONNECTIONSTRING string = azuresql.outputs.AZSQL_CONNECTIONSTRING
122111
output COSMOS_CONNECTIONSTRING string = cosmos.outputs.COSMOS_CONNECTIONSTRING
123112
output MONGO_CONNECTIONSTRING string = mongodb.outputs.MONGO_CONNECTIONSTRING
124-
output MONGOACI_CONNECTIONSTRING string = mongoaci.outputs.MONGO_CONNECTIONSTRING
125113
output SERVICE_ENDPOINT string = app_service.outputs.SERVICE_ENDPOINT

tests/CommunityToolkit.Datasync.Server.MongoDB.Test/MongoDBRepository_Tests.cs

Lines changed: 5 additions & 7 deletions
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 MongoDB.Bson;
910
using MongoDB.Driver;
@@ -13,19 +14,16 @@
1314
namespace CommunityToolkit.Datasync.Server.MongoDB.Test;
1415

1516
[ExcludeFromCodeCoverage]
16-
public class MongoDBRepository_Tests(ITestOutputHelper output) : RepositoryTests<MongoDBMovie>(), IAsyncLifetime
17+
public class MongoDBRepository_Tests(MongoDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<MongoDBMovie>(), IClassFixture<MongoDatabaseFixture>, IAsyncLifetime
1718
{
1819
#region Setup
1920
private readonly Random random = new();
2021
private List<MongoDBMovie> movies = [];
2122

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

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

3937
public MongoDBContext Context { get; set; }
4038

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

4341
protected override async Task<MongoDBMovie> GetEntityAsync(string id)
4442
=> await Context.Movies.Find(Builders<MongoDBMovie>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using CommunityToolkit.Datasync.Server.MongoDB;
66
using CommunityToolkit.Datasync.Server.Test.Helpers;
77
using CommunityToolkit.Datasync.TestCommon.Databases;
8+
using CommunityToolkit.Datasync.TestCommon.Fixtures;
89
using MongoDB.Bson;
910
using MongoDB.Driver;
1011
using Xunit.Abstractions;
@@ -13,24 +14,16 @@ namespace CommunityToolkit.Datasync.Server.Test.Live;
1314

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

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

3629
public async Task DisposeAsync()
@@ -45,7 +38,7 @@ public async Task DisposeAsync()
4538

4639
protected override string DriverName { get; } = "MongoDB";
4740

48-
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.MongoCommunity);
41+
protected override bool CanRunLiveTests() => true;
4942

5043
protected override async Task<MongoDBMovie> GetEntityAsync(string id)
5144
=> await Context.Movies.Find(Builders<MongoDBMovie>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();

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

Lines changed: 1 addition & 0 deletions
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.MongoDb" />
2324
<PackageReference Include="TestContainers.MsSql" />
2425
<PackageReference Include="TestContainers.MySql" />
2526
<PackageReference Include="TestContainers.PostgreSql" />

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
88
public static class ConnectionStrings
99
{
1010
public static readonly string CosmosDb = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING");
11-
public static readonly string MongoCommunity = Environment.GetEnvironmentVariable("MONGOACI_CONNECTION_STRING");
1211
public static readonly string CosmosMongo = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING");
1312
public static readonly string Service = Environment.GetEnvironmentVariable("SERVICE_ENDPOINT");
1413
public static readonly bool EnableLogging = (Environment.GetEnvironmentVariable("ENABLE_SQL_LOGGING") ?? "false") == "true";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.MongoDb;
6+
using Testcontainers.MsSql;
7+
using Xunit;
8+
9+
namespace CommunityToolkit.Datasync.TestCommon.Fixtures;
10+
11+
[ExcludeFromCodeCoverage]
12+
public class MongoDatabaseFixture : IAsyncLifetime
13+
{
14+
private readonly MongoDbContainer _container;
15+
16+
public MongoDatabaseFixture()
17+
{
18+
this._container = new MongoDbBuilder()
19+
.WithImage("mongo:latest")
20+
.Build();
21+
}
22+
23+
/// <inheritdoc />
24+
public async Task DisposeAsync()
25+
{
26+
if (this._container is not null)
27+
{
28+
await this._container.DisposeAsync();
29+
}
30+
}
31+
32+
/// <inheritdoc />
33+
public async Task InitializeAsync()
34+
{
35+
await this._container.StartAsync();
36+
ConnectionString = this._container.GetConnectionString();
37+
}
38+
39+
/// <summary>
40+
/// The connection string for the MySQL database.
41+
/// </summary>
42+
public string ConnectionString { get; private set; } = string.Empty;
43+
}

0 commit comments

Comments
 (0)