Skip to content

Commit f20bf82

Browse files
refactor: refactors the MongoDB fixture and improves the configuration
1 parent 3ca9cca commit f20bf82

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

Tests/Integration/Fixtures/MongoDatabaseFixture.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,41 @@ namespace HttpsRichardy.Federation.TestSuite.Integration.Fixtures;
22

33
public sealed class MongoDatabaseFixture : IAsyncLifetime
44
{
5-
public string ConnectionString { get; private set; } = string.Empty;
6-
public string DatabaseName { get; private set; } = "federation-integration-tests";
7-
8-
private readonly IContainer _container;
5+
public string ConnectionString { get; private set; } = default!;
6+
public string DatabaseName { get; private set; } = AppDomain.CurrentDomain.FriendlyName;
97

108
public IMongoDatabase Database { get; private set; } = default!;
119
public IMongoClient Client { get; private set; } = default!;
1210

13-
public MongoDatabaseFixture()
14-
{
15-
_container = new ContainerBuilder("mongo:latest")
16-
.WithCleanUp(true)
17-
.WithExposedPort(27017)
18-
.WithPortBinding(0, 27017)
19-
.WithEnvironment("MONGO_INITDB_ROOT_USERNAME", "admin")
20-
.WithEnvironment("MONGO_INITDB_ROOT_PASSWORD", "admin")
21-
.Build();
22-
}
23-
24-
public async Task DisposeAsync()
25-
{
26-
await _container.StopAsync();
27-
}
28-
11+
// build a container with the official mongo image
12+
// see more: https://dotnet.testcontainers.org/
13+
private readonly IContainer _container = new ContainerBuilder("mongo:latest")
14+
.WithCleanUp(true)
15+
.WithExposedPort(27017)
16+
.WithPortBinding(0, 27017)
17+
.WithEnvironment("MONGO_INITDB_ROOT_USERNAME", "admin")
18+
.WithEnvironment("MONGO_INITDB_ROOT_PASSWORD", "admin")
19+
.Build();
20+
21+
public async Task DisposeAsync() => await _container.StopAsync();
2922
public async Task InitializeAsync()
3023
{
3124
await _container.StartAsync();
3225

3326
var hostPort = _container.GetMappedPublicPort(27017);
34-
35-
ConnectionString = $"mongodb://admin:admin@localhost:{hostPort}/{DatabaseName}?authSource=admin";
27+
var url = new MongoUrlBuilder
28+
{
29+
Scheme = ConnectionStringScheme.MongoDB,
30+
Server = new MongoServerAddress("localhost", hostPort),
31+
Username = "admin",
32+
Password = "admin",
33+
AuthenticationSource = "admin",
34+
DatabaseName = DatabaseName
35+
};
36+
37+
// mongo database connection string format:
38+
// https://www.mongodb.com/docs/manual/reference/connection-string/
39+
ConnectionString = url.ToString();
3640

3741
Client = new MongoClient(ConnectionString);
3842
Database = Client.GetDatabase(DatabaseName);
@@ -44,9 +48,9 @@ public async Task CleanDatabaseAsync()
4448
.ListCollectionNames()
4549
.ToListAsync();
4650

47-
foreach (var collectionName in collections)
51+
foreach (var collection in collections)
4852
{
49-
await Database.DropCollectionAsync(collectionName);
53+
await Database.DropCollectionAsync(collection);
5054
}
5155
}
5256
}

0 commit comments

Comments
 (0)