-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add in testcontainers so that contributors do not need to run a repli…
…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
1 parent
ea7fc08
commit 4b6a6bc
Showing
5 changed files
with
80 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters