Skip to content

Commit

Permalink
add in testcontainers so that contributors do not need to run a repli…
Browse files Browse the repository at this point in the history
…ca set locally (#231)

* add in testcontainers so that contributors do not need to run a replica set locally

* Use TestContainers conditionally based on the enviroment variable 'MONGODB_ENTITIES_TESTCONTAINERS' being set
  • Loading branch information
dberry-rcs authored Oct 18, 2024
1 parent ea7fc08 commit 4b6a6bc
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
7 changes: 3 additions & 4 deletions Tests/EntityIdTests/TestMultiDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class MultiDbEntity
[TestMethod]
public async Task save_entity_works()
{
await DB.InitAsync(dbName);

await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<BookCover>(dbName);
DB.DatabaseFor<BookMark>(dbName);

Expand All @@ -38,7 +37,7 @@ public async Task save_entity_works()
[TestMethod]
public async Task relationships_work()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<BookCover>(dbName);
DB.DatabaseFor<BookMark>(dbName);

Expand Down Expand Up @@ -97,7 +96,7 @@ public async Task multiple_initializations_should_not_throw()
[TestMethod]
public async Task dropping_collections()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<BookMark>(dbName);
DB.DatabaseFor<BookCover>(dbName);

Expand Down
26 changes: 25 additions & 1 deletion Tests/Init.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace MongoDB.Entities.Tests;

[TestClass]
public static class InitTest
{
private static MongoClientSettings ClientSettings { get; set; }
static bool UseTestContainers;

[AssemblyInitialize]
public static async Task Init(TestContext _)
{
await DB.InitAsync("mongodb-entities-test");
UseTestContainers = System.Environment.GetEnvironmentVariable("MONGODB_ENTITIES_TESTCONTAINERS") != null;

if (UseTestContainers)
{
var testContainer = await TestDatabase.CreateDatabase();
ClientSettings = MongoClientSettings.FromConnectionString(testContainer.GetConnectionString());
}

await InitTestDatabase("mongodb-entities-test");
}

public static async Task InitTestDatabase(string databaseName)
{
if (UseTestContainers)
{
await DB.InitAsync(databaseName, ClientSettings);
}
else
{
await DB.InitAsync(databaseName);
}
}
}
42 changes: 42 additions & 0 deletions Tests/TestDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;
using Testcontainers.MongoDb;

public static class TestDatabase
{
private static SemaphoreSlim _semaphore = new(1, 1);
private static MongoDbContainer? _testContainer;

public static async Task<MongoDbContainer> CreateDatabase()
{
await _semaphore.WaitAsync();
try
{
var database = await CreateTestDatabase();

return database;
}
finally
{
_semaphore.Release();
}
}

private static async Task<MongoDbContainer> CreateTestDatabase()
{
if (_testContainer != null)
{
return _testContainer;
}
_testContainer = new MongoDbBuilder()
.WithPortBinding(27017)
.WithPassword("username")
.WithUsername("password")
.WithReplicaSet()
.Build();

await _testContainer.StartAsync();

return _testContainer;
}
}
18 changes: 9 additions & 9 deletions Tests/TestFileEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task uploading_data_from_http_stream()
[TestMethod]
public async Task uploading_data_from_file_stream()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 800, Width = 600, Name = "Test.Png" };
Expand All @@ -61,7 +61,7 @@ public async Task uploading_data_from_file_stream()
[TestMethod]
public async Task uploading_with_wrong_hash()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 800, Width = 600, Name = "Test-bad-hash.png", MD5 = "wrong-hash" };
Expand All @@ -76,7 +76,7 @@ await Assert.ThrowsExceptionAsync<InvalidDataException>(async ()
[TestMethod]
public async Task uploading_with_correct_hash()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 800, Width = 600, Name = "Test-correct-hash.png", MD5 = "cccfa116f0acf41a217cbefbe34cd599" };
Expand All @@ -96,7 +96,7 @@ public async Task uploading_with_correct_hash()
[TestMethod]
public async Task file_smaller_than_chunk_size()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 100, Width = 100, Name = "Test-small.Png" };
Expand All @@ -116,7 +116,7 @@ public async Task file_smaller_than_chunk_size()
[TestMethod]
public async Task deleting_entity_deletes_all_chunks()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { ID = Guid.NewGuid().ToString(), Height = 400, Width = 400, Name = "Test-Delete.Png" };
Expand Down Expand Up @@ -148,7 +148,7 @@ await DB.Database(dbName).GetCollection<FileChunk>(DB.CollectionName<FileChunk>(
[TestMethod]
public async Task deleting_only_chunks()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 400, Width = 400, Name = "Test-Delete.Png" };
Expand Down Expand Up @@ -179,7 +179,7 @@ await DB.Database(dbName).GetCollection<FileChunk>(DB.CollectionName<FileChunk>(
[TestMethod]
public async Task downloading_file_chunks_works()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 500, Width = 500, Name = "Test-Download.Png" };
Expand All @@ -205,7 +205,7 @@ public async Task downloading_file_chunks_works()
[TestMethod]
public async Task downloading_file_chunks_directly()
{
await DB.InitAsync(dbName);
await InitTest.InitTestDatabase(dbName);
DB.DatabaseFor<Image>(dbName);

var img = new Image { Height = 500, Width = 500, Name = "Test-Download.Png" };
Expand All @@ -231,7 +231,7 @@ public async Task downloading_file_chunks_directly()
[TestMethod]
public void trying_to_download_when_no_chunks_present()
{
DB.InitAsync(dbName).GetAwaiter().GetResult();
InitTest.InitTestDatabase(dbName).GetAwaiter().GetResult();

Assert.ThrowsException<InvalidOperationException>(
() =>
Expand Down
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2"/>
<PackageReference Include="MSTest.TestFramework" Version="3.5.2"/>
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 4b6a6bc

Please sign in to comment.