Skip to content

Commit 3fc8395

Browse files
author
Oleksii Sokol
committed
mongo container
1 parent 97cfbc8 commit 3fc8395

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

ManagedCode.Database.Tests/BaseTests/BaseCollectionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public virtual async Task InsertOneItem_ReturnsInsertedItem()
5353
// Assert
5454
insertItem.Should().NotBeNull();
5555
}
56-
/*
56+
/*
5757
[Fact]
5858
public virtual async Task InsertItem_WhenItemExist_ShouldThrowDatabaseException()
5959
{
@@ -67,7 +67,7 @@ public virtual async Task InsertItem_WhenItemExist_ShouldThrowDatabaseException(
6767
// Assert
6868
await insertAction.Should().ThrowAsync<DatabaseException>();
6969
}
70-
70+
/*
7171
[Fact]
7272
public virtual async Task InsertListOfItems_ReturnsItemsCount()
7373
{

ManagedCode.Database.Tests/MongoDBTests/MongoDBCollectionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace ManagedCode.Database.Tests.MongoDBTests;
1111

12-
[Collection("MongoDB collection")]
12+
[Collection(nameof(MongoDBTestContainer))]
1313
public class MongoDBCollectionTests : BaseCollectionTests<ObjectId, TestMongoDBItem>
1414
{
1515
public MongoDBCollectionTests() : base(new MongoDBTestContainer())

ManagedCode.Database.Tests/TestContainers/MongoDBTestContainer.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
using ManagedCode.Database.MongoDB;
1010
using ManagedCode.Database.Tests.Common;
1111
using MongoDB.Bson;
12+
using Xunit;
1213
using Xunit.Abstractions;
1314

1415
namespace ManagedCode.Database.Tests.TestContainers;
1516

16-
public class MongoDBTestContainer : ITestContainer<ObjectId, TestMongoDBItem>
17+
[CollectionDefinition(nameof(MongoDBTestContainer))]
18+
public class MongoDBTestContainer : ITestContainer<ObjectId, TestMongoDBItem>, IDisposable
1719
{
1820
//private readonly ITestOutputHelper _testOutputHelper;
1921
private readonly TestcontainersContainer _mongoDBTestContainer;
2022
private MongoDBDatabase _dbDatabase;
2123
private DockerClient _dockerClient;
22-
private string containerName = $"mongoContainer{Guid.NewGuid().ToString("N")}";
24+
private const string containerName = "mongoContainer";
2325
private const ushort privatePort = 27017;
2426
private bool containerExsist = false;
27+
private string containerId;
2528

2629
public MongoDBTestContainer()
2730
{
@@ -36,7 +39,7 @@ public MongoDBTestContainer()
3639
.UntilPortIsAvailable(privatePort))
3740
.Build();
3841

39-
// _dockerClient = new DockerClientConfiguration().CreateClient();
42+
_dockerClient = new DockerClientConfiguration().CreateClient();
4043
}
4144

4245
public IDatabaseCollection<ObjectId, TestMongoDBItem> Collection =>
@@ -51,10 +54,9 @@ public async Task InitializeAsync()
5154
{
5255
ushort publicPort = 0;
5356

54-
await _mongoDBTestContainer.StartAsync();
55-
/*
5657
try
5758
{
59+
await _mongoDBTestContainer.StartAsync();
5860

5961
containerExsist = false;
6062
}
@@ -66,19 +68,22 @@ public async Task InitializeAsync()
6668
if (!containerExsist)
6769
{
6870
publicPort = _mongoDBTestContainer.GetMappedPublicPort(privatePort);
71+
containerId = _mongoDBTestContainer.Id;
6972
}
7073
else
7174
{
7275
var listContainers = await _dockerClient.Containers.ListContainersAsync(new ContainersListParameters());
7376

7477
ContainerListResponse containerListResponse = listContainers.Single(container => container.Names.Contains($"/{containerName}"));
7578

79+
containerId = containerListResponse.ID;
80+
7681
publicPort = containerListResponse.Ports.Single(port => port.PrivatePort == privatePort).PublicPort;
77-
}*/
82+
}
7883

7984
_dbDatabase = new MongoDBDatabase(new MongoDBOptions()
8085
{
81-
ConnectionString = $"mongodb://localhost:{_mongoDBTestContainer.GetMappedPublicPort(privatePort)}",
86+
ConnectionString = $"mongodb://localhost:{publicPort}",
8287
DataBaseName = $"db{Guid.NewGuid().ToString("N")}",
8388
});
8489

@@ -91,10 +96,17 @@ public async Task InitializeAsync()
9196
public async Task DisposeAsync()
9297
{
9398
await _dbDatabase.DisposeAsync();
94-
await _mongoDBTestContainer.StopAsync();
95-
await _mongoDBTestContainer.CleanUpAsync();
99+
//await _dockerClient.Containers.StopContainerAsync(_mongoDBTestContainer.Id, new ContainerStopParameters());
96100

97101
// _testOutputHelper.WriteLine($"Mongo container State:{_mongoDBContainer.State}");
98102
//_testOutputHelper.WriteLine("=STOP=");
99103
}
104+
105+
public async void Dispose()
106+
{
107+
await _dockerClient.Containers.StopContainerAsync(containerId, new ContainerStopParameters());
108+
109+
// await _dockerClient.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters());
110+
111+
}
100112
}

0 commit comments

Comments
 (0)