Created
October 17, 2018 06:45
-
-
Save yetanotherchris/053ed090f0e83a8023fbf981916ccfae to your computer and use it in GitHub Desktop.
Integration testing DynamoDB withamazon/dynamodb-local on Docker
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
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