Categorygithub.com/rozen03/dynamotest
modulepackage
0.0.0-20240627115501-e3c6c575bf2d
Repository: https://github.com/rozen03/dynamotest.git
Documentation: pkg.go.dev

# README

dynamotest

Leverage the power of DynamoDB Local with ory/dockertest to create your DynamoDB test cases effortlessly.

🌄 What is dynamotest?

dynamotest is a package designed to help set up a DynamoDB Local Docker instance on your machine as part of Go test code. It uses ory/dockertest to start the DynamoDB Local instance in your Go test code, and is configured so that each call to dynamotest.NewDynamoDB(t) will create a dedicated instance, allowing parallel testing on multiple Docker instances. The function returns a new DynamoDB client which is already connected to the instance, enabling you to start using the client immediately. Additionally, it provides a clean-up function to ensure that the Docker instance gets deleted if clean-up is preferred. If you do not call the clean-up function, the instance will keep running, which may be useful for debugging and investigation.

It is also worth noting that this package uses only the v2 version of the AWS SDK.

NOTE: It is a prerequisite that you are able to start up a Docker container for DynamoDB Local.

🚀 Example

Here's a simple example to get you started with dynamotest.

Step 1: Define Table Schema

package usage_example

import (
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

func getSchema() dynamodb.CreateTableInput {
	return dynamodb.CreateTableInput{
		AttributeDefinitions: []types.AttributeDefinition{
			{
				AttributeName: aws.String("pk"),
				AttributeType: types.ScalarAttributeTypeS,
			},
			{
				AttributeName: aws.String("sk"),
				AttributeType: types.ScalarAttributeTypeS,
			},
		},
		KeySchema: []types.KeySchemaElement{
			{
				AttributeName: aws.String("pk"),
				KeyType:       types.KeyTypeHash,
			},
			{
				AttributeName: aws.String("sk"),
				KeyType:       types.KeyTypeRange,
			},
		},
	}
}

Step 2: Write Test Code

package usage_example

import (
	"context"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/rozen03/dynamotest"
)

func TestMain(m *testing.M) {
	code := dynamotest.RunTestAndCleanup(m)
	os.Exit(code)
}

func TestRepositoryExample_Create(t *testing.T) {
	client := dynamotest.DynamoDBClient()
	table := client.CreateTestingTable(t, "test", getSchema())

	repo := RepositoryExample{client: client, table: &table}
	model := ExampleModel{
		ID:    "1",
		SK:    "1",
		Value: "example",
	}

	err := repo.Create(context.Background(), model)
	assert.NoError(t, err)
}

Refer to usage_example/example_test.go for the complete code and more detailed examples.

# Packages

No description provided by the author

# Functions

No description provided by the author
NewDynamoDB creates a Docker container with DynamoDB Local, and returns the connected DynamoDB client.
No description provided by the author

# Variables

No description provided by the author

# Structs

No description provided by the author