local

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 13 Imported by: 0

README

Test against DynamoDB local with Testcontainers

Go Reference

Get with:

go get github.com/nguyengg/go-dynamodb-local

Usage:

package app

import (
	"testing"

	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
	local "github.com/nguyengg/go-aws-commons/go-dynamodb-local"
	"github.com/stretchr/testify/require"
)

func TestMyApp(t *testing.T) {
	client := local.Default(t)

	_, err := client.ListTables(t.Context(), &dynamodb.ListTablesInput{})
	require.NoError(t, err)

	// GetItem is a utility method to help do a quick DynamoDB GetItem.
	var avM map[string]types.AttributeValue
	avM = local.GetItem(t, client, "table-name", "pk-name", "pk-value")
	avM = local.GetItem(t, client, "another-table", "pk-name", 4, "sk-name", 7)
	avM = local.GetItem(t, client, "table-with-B-pk", "pk-name", &types.AttributeValueMemberB{})
}

Documentation

Overview

Package local provides testing functionality using DynamoDB local as Docker image.

The DynamoDB local image is started via Testcontainers DynamoDB module. Docker daemon must have been started prior to executing the tests. On Linux, Docker can be installed via system's default package manager. On MacOS and Windows, use Docker Desktop if possible. If using Colima on MacOS, be sure to follow Using Colima with Docker to set DOCKER_HOST and/or TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE accordingly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(t *testing.T, optFns ...func(opts *Options)) *dynamodb.Client

Connect should be called by all tests that require a DynamoDB local instance.

func Default

func Default(t *testing.T, optFns ...func(opts *Options)) *dynamodb.Client

Default is a variation of Connect with sensible defaults.

The settings are enabled by Default:

  • DynamoDB local is started with -inMemory -sharedDb -disableTelemetry per usage notes. This is fine since each test should be creating their own connection.
  • WithStubCredentialsProvider is added.

func DefaultSkippable

func DefaultSkippable(t *testing.T, optFns ...func(opts *Options)) *dynamodb.Client

DefaultSkippable is a variation of Default that skips the test if DOCKER_HOST is not given.

func GetItem

func GetItem(t *testing.T, client *dynamodb.Client, tableName string, hashKeyName string, hashKeyValue any, a ...any) map[string]types.AttributeValue

GetItem makes a GetItem request and fails the test if the call fails.

The hash key name and value pair must be given. The sort key name and value pair is optional; if given, len(a) must be exactly 2.

func WithClientOptions

func WithClientOptions(optFns ...func(opts *dynamodb.Options)) func(opts *Options)

WithClientOptions adds client options passed to every DynamoDB calls by the client returned by Connect.

func WithContainerOptions

func WithContainerOptions(optFns ...testcontainers.ContainerCustomizer) func(opts *Options)

WithContainerOptions adds customisations to the test container such as testcontainersdynamodb.WithSharedDB.

func WithLoadOptions

func WithLoadOptions(optFns ...func(opts *config.LoadOptions) error) func(opts *Options)

WithLoadOptions adds customisations to the config.LoadDefaultConfig.

func WithStubCredentialsProvider

func WithStubCredentialsProvider() func(opts *Options)

WithStubCredentialsProvider modifies the credentials to use stub values since DynamoDB local doesn't care.

Types

type Options

type Options struct {
	// Skip can be given to skip the test by calling t.Skip.
	Skip func(t *testing.T)
	// contains filtered or unexported fields
}

Options customises Connect.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL