Skip to content

Commit

Permalink
fix dynamoDb container
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Sokol committed Jan 11, 2023
1 parent 21425a6 commit 80da773
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*using System;
using System;
using System.Threading.Tasks;
using FluentAssertions;
using ManagedCode.Database.Tests.BaseTests;
Expand All @@ -9,10 +9,10 @@

namespace ManagedCode.Database.Tests.DynamoDbTests;

[Collection("DynamoDB collection")]
[Collection(nameof(DynamoDBTestContainer))]
public class DynamoDBQueryableTests : BaseQueryableTests<string, TestDynamoDbItem>
{
public DynamoDBQueryableTests() : base(new DynamoDBTestContainer())
public DynamoDBQueryableTests(DynamoDBTestContainer container) : base(container)
{
}

Expand Down Expand Up @@ -55,4 +55,4 @@ await itemsResult
.Should()
.ThrowAsync<ArgumentNullException>();
}
}*/
}
54 changes: 27 additions & 27 deletions ManagedCode.Database.Tests/DynamoDbTests/DynamoDbCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*using FluentAssertions;
using FluentAssertions;
using ManagedCode.Database.Core.Exceptions;
using ManagedCode.Database.Tests.BaseTests;
using ManagedCode.Database.Tests.Common;
Expand All @@ -10,10 +10,10 @@

namespace ManagedCode.Database.Tests.DynamoDbTests;

[Collection("DynamoDB collection")]
[Collection(nameof(DynamoDBTestContainer))]
public class DynamoDbCollectionTests : BaseCollectionTests<string, TestDynamoDbItem>
{
public DynamoDbCollectionTests() : base(new DynamoDBTestContainer())
public DynamoDbCollectionTests(DynamoDBTestContainer container) : base(container)
{
}

Expand Down Expand Up @@ -51,30 +51,30 @@ public override async Task DeleteItemById_WhenItemDoesntExists()
deleted.Should().ThrowExactlyAsync<DatabaseException>();
}

*//* [Fact]
public override async Task GetById_ReturnOk()
[Fact]
public override async Task GetById_ReturnOk()
{
// Arrange
var itemId = GenerateId();
await Collection.InsertAsync(CreateNewItem(itemId));

try
{
// Act
var getItemResult = await Collection.GetAsync(itemId);

// Assert
getItemResult.Should().NotBeNull();
}
catch (DatabaseException e)
{
// Arrange
var itemId = GenerateId();
await Collection.InsertAsync(CreateNewItem(itemId));
try
{
// Act
var getItemResult = await Collection.GetAsync(itemId);
// Assert
getItemResult.Should().NotBeNull();
}
catch (DatabaseException e)
{
// Act
var getItemResult = () => Collection.GetAsync(itemId);
// Assert
await getItemResult.Should().ThrowAsync<DatabaseException>();
}
}*//*
// Act
var getItemResult = () => Collection.GetAsync(itemId);

// Assert
await getItemResult.Should().ThrowAsync<DatabaseException>();
}
}

[Fact]
public override async Task InsertItem_WhenItemExist_ShouldThrowDatabaseException()
Expand Down Expand Up @@ -102,4 +102,4 @@ public override async Task UpdateItem_WhenItemDoesntExists()
// Assert
updateItem.Should().BeNull();
}
}*/
}
22 changes: 18 additions & 4 deletions ManagedCode.Database.Tests/TestContainers/DynamoDBTestContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
using ManagedCode.Database.Core;
using ManagedCode.Database.DynamoDB;
using ManagedCode.Database.Tests.Common;
using Xunit;
using Xunit.Abstractions;

namespace ManagedCode.Database.Tests.TestContainers;

public class DynamoDBTestContainer : ITestContainer<string, TestDynamoDbItem>
[CollectionDefinition(nameof(DynamoDBTestContainer))]
public class DynamoDBTestContainer : ITestContainer<string, TestDynamoDbItem>, ICollectionFixture<DynamoDBTestContainer>, IDisposable
{
// private readonly ITestOutputHelper _testOutputHelper;
private readonly TestcontainersContainer _dynamoDBTestContainer;
Expand All @@ -21,7 +23,7 @@ public class DynamoDBTestContainer : ITestContainer<string, TestDynamoDbItem>
private const string containerName = "dynamoContainer";
private const ushort privatePort = 8000;
private bool containerExsist = false;

private string containerId;

public DynamoDBTestContainer()
{
Expand Down Expand Up @@ -64,13 +66,17 @@ public async Task InitializeAsync()
if (!containerExsist)
{
publicPort = _dynamoDBTestContainer.GetMappedPublicPort(privatePort);

containerId = _dynamoDBTestContainer.Id;
}
else
{
var listContainers = await _dockerClient.Containers.ListContainersAsync(new ContainersListParameters());

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

containerId = containerListResponse.ID;

publicPort = containerListResponse.Ports.Single(port => port.PrivatePort == privatePort).PublicPort;
}

Expand All @@ -93,10 +99,18 @@ public async Task InitializeAsync()
public async Task DisposeAsync()
{
await _dbDatabase.DisposeAsync();
//await _dynamoDBContainer.StopAsync();
//await _dynamoDBContainer.CleanUpAsync();

//_testOutputHelper.WriteLine($"DynamoDb container State:{_dynamoDBContainer.State}");
//_testOutputHelper.WriteLine("=STOP=");
}

public async void Dispose()
{

await _dockerClient.Containers.RemoveContainerAsync(containerId,
new ContainerRemoveParameters
{
Force = true
});
}
}

0 comments on commit 80da773

Please sign in to comment.