Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integration testing DynamoDB withamazon/dynamodb-local on Docker
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using AutoFixture;
using Shouldly;
using Xunit;
namespace SomeTests
{
// Uses docker amazon/dynamodb-local image for testing
public class DynamoDbRepositoryTests : IClassFixture<DynamoDbLocalFixture>
{
private readonly DynamoDbConfiguration _configuration;
private readonly AmazonDynamoDBClient _dynamoDbClient;
public DynamoDbRepositoryTests()
{
_configuration = new DynamoDbConfiguration()
{
Tablename = "foo",
};
var dynamoDbConfig = new AmazonDynamoDBConfig { ServiceURL = "http://localhost:8000" };
_dynamoDbClient = new AmazonDynamoDBClient(dynamoDbConfig);
}
[Fact]
public async Task should_save_entities()
{
// given
var fixture = new Fixture();
var entities = fixture.CreateMany<MyEntity>(500);
var repository = new DynamoDbRepository(_configuration, _dynamoDbClient);
// when
await repository.SaveMany(entities);
// then
var response = await _dynamoDbClient.ScanAsync(new ScanRequest(_configuration.Tablename));
response.Count.ShouldBe(500);
}
}
public class DynamoDbLocalFixture : IDisposable
{
private string _awsCliArgs =
"dynamodb create-table --endpoint-url http://localhost:8000 --table-name foo --attribute-definitions AttributeName=SomeId,AttributeType=N --key-schema AttributeName=SomeId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5";
public DynamoDbLocalFixture()
{
Process process = Process.Start("docker", "rm -f mytable-dynamoDb");
process.WaitForExit(5000);
process = Process.Start("docker", "run -d -p 8000:8000 --name mytable-dynamoDb amazon/dynamodb-local -jar DynamoDBLocal.jar -inMemory -sharedDb");
process.WaitForExit(5000);
process = Process.Start("aws", _awsCliArgs);
process.WaitForExit(5000);
}
public void Dispose()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.