Skip to content

Commit

Permalink
Update MongoDB.Driver to v3.0.0, switch from Mongo2Go to Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlerman committed Oct 26, 2024
1 parent 04b42f4 commit c4f9535
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 75 deletions.
38 changes: 24 additions & 14 deletions src/Kot.MongoDB.Migrations.DI.IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Mongo2Go;
using MongoDB.Driver;
using NUnit.Framework;
using Serilog;
Expand All @@ -17,6 +16,7 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Testcontainers.MongoDb;

namespace Kot.MongoDB.Migrations.DI.IntegrationTests
{
Expand All @@ -30,23 +30,32 @@ public class IntegrationTests

private StringWriter _logWriter;
private Assembly _externalMigrationsAssembly;
private MongoDbRunner _runner;
private MongoDbContainer _container;
private string _connectionString;
private IMongoClient _client;
private IMongoDatabase _db;
private IMongoCollection<MigrationHistory> _histCollection;
private IMongoCollection<TestDoc> _docCollection;

[OneTimeSetUp]
public void OneTimeSetUp()
public async Task OneTimeSetUp()
{
_externalMigrationsAssembly = CompileAndLoadAssemblyWithMigration();

Microsoft.Extensions.Logging.ILogger logger = LoggerFactory
var logger = LoggerFactory
.Create(config => config.SetMinimumLevel(LogLevel.Error).AddConsole())
.CreateLogger("Mongo2Go");
.CreateLogger("MongoDbContainer");

_runner = MongoDbRunner.Start(singleNodeReplSet: true, logger: logger);
_client = new MongoClient(_runner.ConnectionString);
_container = new MongoDbBuilder()
.WithImage("mongo:8.0")
.WithReplicaSet()
.WithLogger(logger)
.Build();

await _container.StartAsync();

_connectionString = _container.GetConnectionString() + "?directConnection=true";
_client = new MongoClient(_connectionString);
_db = _client.GetDatabase(DatabaseName);
_histCollection = _db.GetCollection<MigrationHistory>(MigrationsCollectionName);
_docCollection = _db.GetCollection<TestDoc>(TestDoc.CollectionName);
Expand All @@ -66,9 +75,9 @@ public async Task TearDown()
}

[OneTimeTearDown]
public void OneTimeTearDown()
public async Task OneTimeTearDown()
{
_runner.Dispose();
await _container.StopAsync();
}

[Test]
Expand Down Expand Up @@ -288,7 +297,7 @@ private async Task MigrateWithConnectionString(Action<DIMigrationsLocationConfig
{
await TestMigration((services, options) =>
{
services.AddMongoMigrations(_runner.ConnectionString, options, configure);
services.AddMongoMigrations(_connectionString, options, configure);
},
expectedVersions,
withLogs);
Expand Down Expand Up @@ -366,10 +375,11 @@ public SimpleMigration004(ITestService testService) : base(""0.0.4"", testServic
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);

using var memoryStream = new MemoryStream();
compilation.Emit(memoryStream);

return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
using (var memoryStream = new MemoryStream())
{
compilation.Emit(memoryStream);
return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
}
}

class HostedService : IHostedService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net472;net6</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -14,7 +15,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
Expand All @@ -24,6 +24,7 @@
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.TextWriter" Version="2.1.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void MultipleLoadSources_ThrowsException(
Assert.Throws<InvalidOperationException>(() => second(configurator));
}

private static Action<DIMigrationsLocationConfigurator>[] ConfiguratorActions() => new[]
private static Action<DIMigrationsLocationConfigurator>[] ConfiguratorActions() => new Action<DIMigrationsLocationConfigurator>[]
{
(DIMigrationsLocationConfigurator config) => config.LoadMigrationsFromCurrentDomain(),
(DIMigrationsLocationConfigurator config) => config.LoadMigrationsFromAssembly(Assembly.GetExecutingAssembly()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net472;net6</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net472;net6</TargetFrameworks>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
<PackageVersion>2.1.0</PackageVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<PackageVersion>3.0.0</PackageVersion>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<Description>DI support for Kot.MongoDB.Migrations</Description>
<PackageTags>mongodb;mongodb-driver;mongo;migration;migrations;transactions;di</PackageTags>
</PropertyGroup>
Expand All @@ -20,7 +20,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net472;net6</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -15,7 +16,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -31,6 +31,7 @@
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.TextWriter" Version="2.1.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 30 additions & 20 deletions src/Kot.MongoDB.Migrations.IntegrationTests/MigratorBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Logging;
using Mongo2Go;
using MongoDB.Driver;
using NUnit.Framework;
using Serilog.Extensions.Logging;
Expand All @@ -16,6 +15,7 @@
using System.Reflection;
using System.Threading.Tasks;
using NSubstitute;
using Testcontainers.MongoDb;

namespace Kot.MongoDB.Migrations.IntegrationTests
{
Expand All @@ -25,29 +25,38 @@ public class MigratorBuilderTests
private const string DatabaseName = "IntegrationTest";
private const string MigrationsCollectionName = "MigrationHistory";

private static readonly MigrationOptions Options = new(DatabaseName)
private static readonly MigrationOptions Options = new MigrationOptions(DatabaseName)
{
MigrationsCollectionName = MigrationsCollectionName
};

private MongoDbRunner _runner;
private MongoDbContainer _container;
private string _connectionString;
private IMongoClient _client;
private IMongoDatabase _db;
private IMongoCollection<MigrationHistory> _histCollection;
private IMongoCollection<TestDoc> _docCollection;
private Assembly _externalMigrationsAssembly;

[OneTimeSetUp]
public void OneTimeSetUp()
public async Task OneTimeSetUp()
{
_externalMigrationsAssembly = CompileAndLoadAssemblyWithMigration();

Microsoft.Extensions.Logging.ILogger logger = LoggerFactory
var logger = LoggerFactory
.Create(config => config.SetMinimumLevel(LogLevel.Error).AddConsole())
.CreateLogger("Mongo2Go");
.CreateLogger("MongoDbContainer");

_runner = MongoDbRunner.Start(singleNodeReplSet: true, logger: logger);
_client = new MongoClient(_runner.ConnectionString);
_container = new MongoDbBuilder()
.WithImage("mongo:8.0")
.WithReplicaSet()
.WithLogger(logger)
.Build();

await _container.StartAsync();

_connectionString = _container.GetConnectionString() + "?directConnection=true";
_client = new MongoClient(_connectionString);
_db = _client.GetDatabase(DatabaseName);
_histCollection = _db.GetCollection<MigrationHistory>(MigrationsCollectionName);
_docCollection = _db.GetCollection<TestDoc>(TestDoc.CollectionName);
Expand All @@ -61,9 +70,9 @@ public async Task TearDown()
}

[OneTimeTearDown]
public void OneTimeTearDown()
public async Task OneTimeTearDown()
{
_runner.Dispose();
await _container.StopAsync();
}

[Test]
Expand All @@ -84,7 +93,7 @@ public async Task FromMongoClient_FromCurrentDomain()
public async Task FromConnectionString_FromCurrentDomain()
{
// Arrange
IMigrator migrator = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
IMigrator migrator = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrationsFromCurrentDomain()
.Build();

Expand Down Expand Up @@ -112,7 +121,7 @@ public async Task FromMongoClient_FromExecutingAssembly()
public async Task FromConnectionString_FromExecutingAssembly()
{
// Arrange
IMigrator migrator = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
IMigrator migrator = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrationsFromExecutingAssembly()
.Build();

Expand Down Expand Up @@ -140,7 +149,7 @@ public async Task FromMongoClient_FromAssembly()
public async Task FromConnectionString_FromAssembly()
{
// Arrange
IMigrator migrator = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
IMigrator migrator = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrationsFromAssembly(_externalMigrationsAssembly)
.Build();

Expand Down Expand Up @@ -168,7 +177,7 @@ public async Task FromMongoClient_FromNamespace()
public async Task FromConnectionString_FromNamespace()
{
// Arrange
IMigrator migrator = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
IMigrator migrator = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrationsFromNamespace("Kot.MongoDB.Migrations.IntegrationTests.Migrations.Subfolder")
.Build();

Expand Down Expand Up @@ -208,7 +217,7 @@ public async Task FromConnectionString_MigrationsCollection()
new SimpleMigration003()
};

IMigrator migrator = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
IMigrator migrator = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrations(migrations)
.Build();

Expand All @@ -225,7 +234,7 @@ public async Task Logs(bool withLogger)
// Arrange
var stringWriter = new StringWriter();

MigratorBuilder migratorBuilder = MigratorBuilder.FromConnectionString(_runner.ConnectionString, Options)
MigratorBuilder migratorBuilder = MigratorBuilder.FromConnectionString(_connectionString, Options)
.LoadMigrations(Enumerable.Empty<IMongoMigration>());

if (withLogger)
Expand Down Expand Up @@ -326,10 +335,11 @@ public SimpleMigration004() : base(""0.0.4"")
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);

using var memoryStream = new MemoryStream();
compilation.Emit(memoryStream);

return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
using (var memoryStream = new MemoryStream())
{
compilation.Emit(memoryStream);
return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
}
}

private static IEnumerable<TestCaseData> FromConnectionStringTests() => new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Kot.MongoDB.Migrations.MigrationsLocatorTests
public class FailureMigrationsLocatorTests
{
private const string MigrationsNamespace = "Kot.MongoDB.Migrations.MigrationsLocatorTests.Migrations.Duplicates";
private static readonly Random Rand = new Random();
private static Assembly CreatedAssembly;

[OneTimeSetUp]
Expand Down Expand Up @@ -79,17 +80,18 @@ private static Assembly CompileAndLoadAssemblyWithMigrations(string name, IEnume
.AddReferences(references)
.AddSyntaxTrees(syntaxTrees);

using var memoryStream = new MemoryStream();
compilation.Emit(memoryStream);

return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
using (var memoryStream = new MemoryStream())
{
compilation.Emit(memoryStream);
return AppDomain.CurrentDomain.Load(memoryStream.ToArray());
}
}

private static string GenerateMigrationCode(string version)
{
string className = "DuplicateMigration" + version.Replace(".", "_") + "_" + Random.Shared.Next();
string className = "DuplicateMigration" + version.Replace(".", "_") + "_" + Rand.Next();

return @$"
return $@"
using MongoDB.Driver;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net472;net6</TargetFrameworks>
<LangVersion>7.3</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Loading

0 comments on commit c4f9535

Please sign in to comment.