Skip to content

(#346) Added Mongo TestContainer fixture #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageVersion Include="System.Text.Json" Version="9.0.4" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="9.0.4" />
<PackageVersion Include="TestContainers.MongoDb" Version="4.4.0" />
<PackageVersion Include="TestContainers.MsSql" Version="4.4.0" />
<PackageVersion Include="TestContainers.MySql" Version="4.4.0" />
<PackageVersion Include="TestContainers.PostgreSql" Version="4.4.0" />
Expand Down
74 changes: 0 additions & 74 deletions infra/modules/aci-mongodb.bicep

This file was deleted.

12 changes: 0 additions & 12 deletions infra/resources.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ module mongodb './modules/cosmos-mongodb.bicep' = {
}
}

module mongoaci './modules/aci-mongodb.bicep' = {
name: 'mongoaci-deployment-${resourceToken}'
params: {
location: location
tags: tags
serverName: mongoaciServerName
administratorPassword: sqlAdminPassword

}
}

module app_service './modules/appservice.bicep' = {
name: 'appsvc-deployment-${resourceToken}'
params: {
Expand All @@ -121,5 +110,4 @@ module app_service './modules/appservice.bicep' = {
output AZSQL_CONNECTIONSTRING string = azuresql.outputs.AZSQL_CONNECTIONSTRING
output COSMOS_CONNECTIONSTRING string = cosmos.outputs.COSMOS_CONNECTIONSTRING
output MONGO_CONNECTIONSTRING string = mongodb.outputs.MONGO_CONNECTIONSTRING
output MONGOACI_CONNECTIONSTRING string = mongoaci.outputs.MONGO_CONNECTIONSTRING
output SERVICE_ENDPOINT string = app_service.outputs.SERVICE_ENDPOINT
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using CommunityToolkit.Datasync.TestCommon;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Fixtures;
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using MongoDB.Driver;
Expand All @@ -13,19 +14,16 @@
namespace CommunityToolkit.Datasync.Server.MongoDB.Test;

[ExcludeFromCodeCoverage]
public class MongoDBRepository_Tests(ITestOutputHelper output) : RepositoryTests<MongoDBMovie>(), IAsyncLifetime
public class MongoDBRepository_Tests(MongoDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<MongoDBMovie>(), IClassFixture<MongoDatabaseFixture>, IAsyncLifetime
{
#region Setup
private readonly Random random = new();
private List<MongoDBMovie> movies = [];

public async Task InitializeAsync()
{
if (!string.IsNullOrEmpty(ConnectionStrings.MongoCommunity))
{
Context = await MongoDBContext.CreateContextAsync(ConnectionStrings.MongoCommunity, output);
this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync();
}
Context = await MongoDBContext.CreateContextAsync(fixture.ConnectionString, output);
this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync();
}

public async Task DisposeAsync()
Expand All @@ -38,7 +36,7 @@ public async Task DisposeAsync()

public MongoDBContext Context { get; set; }

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.CosmosDb);
protected override bool CanRunLiveTests() => true;

protected override async Task<MongoDBMovie> GetEntityAsync(string id)
=> await Context.Movies.Find(Builders<MongoDBMovie>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CommunityToolkit.Datasync.Server.MongoDB;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Fixtures;
using MongoDB.Bson;
using MongoDB.Driver;
using Xunit.Abstractions;
Expand All @@ -13,24 +14,16 @@ namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class MongoDB_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<MongoDBMovie>(), IAsyncLifetime
public class MongoDB_Controller_Tests(MongoDatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<MongoDBMovie>(), IClassFixture<MongoDatabaseFixture>, IAsyncLifetime
{
#region Setup
private readonly Random random = new();
private List<MongoDBMovie> movies = [];

public async Task InitializeAsync()
{
if (!string.IsNullOrEmpty(ConnectionStrings.MongoCommunity))
{
// 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($"MongoCommunityIsInitialized = {fixture.MongoCommunityIsInitialized}");
Context = await MongoDBContext.CreateContextAsync(ConnectionStrings.MongoCommunity, output, clearEntities: !fixture.MongoCommunityIsInitialized);
this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync();
fixture.MongoCommunityIsInitialized = true;
}
Context = await MongoDBContext.CreateContextAsync(fixture.ConnectionString, output);
this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync();
}

public async Task DisposeAsync()
Expand All @@ -45,7 +38,7 @@ public async Task DisposeAsync()

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

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.MongoCommunity);
protected override bool CanRunLiveTests() => true;

protected override async Task<MongoDBMovie> GetEntityAsync(string id)
=> await Context.Movies.Find(Builders<MongoDBMovie>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Spatial" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
<PackageReference Include="TestContainers.MongoDb" />
<PackageReference Include="TestContainers.MsSql" />
<PackageReference Include="TestContainers.MySql" />
<PackageReference Include="TestContainers.PostgreSql" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace CommunityToolkit.Datasync.TestCommon.Databases;
public static class ConnectionStrings
{
public static readonly string CosmosDb = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING");
public static readonly string MongoCommunity = Environment.GetEnvironmentVariable("MONGOACI_CONNECTION_STRING");
public static readonly string CosmosMongo = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING");
public static readonly string Service = Environment.GetEnvironmentVariable("SERVICE_ENDPOINT");
public static readonly bool EnableLogging = (Environment.GetEnvironmentVariable("ENABLE_SQL_LOGGING") ?? "false") == "true";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Testcontainers.MongoDb;
using Testcontainers.MsSql;
using Xunit;

namespace CommunityToolkit.Datasync.TestCommon.Fixtures;

[ExcludeFromCodeCoverage]
public class MongoDatabaseFixture : IAsyncLifetime
{
private readonly MongoDbContainer _container;

public MongoDatabaseFixture()
{
this._container = new MongoDbBuilder()
.WithImage("mongo:latest")
.Build();
}

/// <inheritdoc />
public async Task DisposeAsync()
{
if (this._container is not null)
{
await this._container.DisposeAsync();
}
}

/// <inheritdoc />
public async Task InitializeAsync()
{
await this._container.StartAsync();
ConnectionString = this._container.GetConnectionString();
}

/// <summary>
/// The connection string for the MySQL database.
/// </summary>
public string ConnectionString { get; private set; } = string.Empty;
}