dynamock

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 7 Imported by: 1

README

go-dynamock-v2

GoDoc Go Report Card Build Status Codecov

Amazon DynamoDB mock for unit testing, fully compatible with SDK.

Visit GoDoc for public API documentation.

Thanks to gusaul for the first version of package go-dynamock.

Requirements

Usage

To use mock you should depend in your code on ClientAPI interface, instead of dependency on specific DynamoDB instance.

package main

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

type Service struct {
    DynamoDB dynamodbiface.ClientAPI
}

func NewService (dynamo dynamodbiface.ClientAPI) *Service {
    return &Service{
        DynamoDB: dynamo,
    }
}
Function you want to test
package main

import (
    "context"
    "strconv"

    "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/dynamodbattribute"
)

func GetNameByID(ID int) (*string, error) {
	param := &dynamodb.GetItemInput{
		Key: map[string]dynamodb.AttributeValue{
			"id": {
				N: aws.String(strconv.Itoa(ID)),
			},
		},
		TableName: aws.String("employee"),
	}

	req := Fake.DB.GetItemRequest(param)
	if req.Error != nil {
		return nil, req.Error
	}

	var value *string
	output, err := req.Send(context.Background())
	if err != nil {
		return nil, err
	}

	if v, ok := output.Item["name"]; ok {
		err := dynamodbattribute.Unmarshal(&v, &value)
		if err != nil {
			return value, err
		}
	}

	return value, nil
}
Test
package examples

import (
	"strconv"
	"testing"

	dynamock "github.com/picante-io/go-dynamock-v2"

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

func init() {
	Fake = new(FakeDynamo)
	Fake.DB, Mock = dynamock.New()
}

func TestGetItem(t *testing.T) {
	ID := 123
	expectKey := map[string]dynamodb.AttributeValue{
		"id": {
			N: aws.String(strconv.Itoa(ID)),
		},
	}

	expectedResult := "rick sanchez"
	result := dynamodb.GetItemResponse{
		GetItemOutput: &dynamodb.GetItemOutput{
			Item: map[string]dynamodb.AttributeValue{
				"id": {
					N: aws.String(strconv.Itoa(ID)),
				},
				"name": {
					S: aws.String(expectedResult),
				},
			},
		},
	}

	Mock.ExpectGetItem().Table("employee").WithKeys(expectKey).WillReturn(result)

	actualResult, err := GetNameByID(ID)
	if err != nil {
		t.Fatal(err)
	}

	if aws.StringValue(actualResult) != expectedResult {
		t.Fatalf("Fail: expected: %s, got: %s", expectedResult, aws.StringValue(actualResult))
	}
}

Currently Supported Functions

GetItemRequest(*dynamodb.GetItemInput) dynamodb.GetItemRequest
PutItemRequest(*dynamodb.PutItemInput) dynamodb.PutItemRequest
UpdateItemRequest(*dynamodb.UpdateItemInput) dynamodb.UpdateItemRequest
DeleteItemRequest(*dynamodb.DeleteItemInput) dynamodb.DeleteItemRequest
BatchGetItemRequest(*dynamodb.BatchGetItemInput) dynamodb.BatchGetItemRequest
BatchWriteItemRequest(*dynamodb.BatchWriteItemInput) dynamodb.BatchWriteItemRequest
ScanRequest(*dynamodb.ScanInput) dynamodb.ScanRequest
QueryRequest(*dynamodb.QueryInput) dynamodb.QueryRequest
CreateTableRequest(*dynamodb.CreateTableInput) dynamodb.CreateTableRequest
DescribeTableRequest(*dynamodb.DescribeTableInput) dynamodb.DescribeTableRequest
WaitUntilTableExists(context.Context, *dynamodb.DescribeTableInput, ...aws.WaiterOption) error

Contributions

Feel free to open a pull request.

License

The MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidInput             = errors.New("invalid input")
	ErrNoExpectation            = errors.New("expectations not found")
	ErrNoTable                  = errors.New("expectations table not found")
	ErrNoKey                    = errors.New("expectations key not found")
	ErrNoItem                   = errors.New("expectations item not found")
	ErrTableExpectationMismatch = errors.New("expected table was not matched")
	ErrKeyExpectationMismatch   = errors.New("expected key was not matched")
	ErrItemExpectationMismatch  = errors.New("expected item was not matched")
)

Functions

This section is empty.

Types

type BatchGetItemExpectation

type BatchGetItemExpectation struct {
	// contains filtered or unexported fields
}

BatchGetItemExpectation struct hold expectation field, err, and result

func (*BatchGetItemExpectation) WillReturn

WillReturn - method for set desired result

func (*BatchGetItemExpectation) WithRequest

WithRequest - method for set Request expectation

type BatchWriteItemExpectation

type BatchWriteItemExpectation struct {
	// contains filtered or unexported fields
}

BatchWriteItemExpectation struct hold expectation field, err, and result

func (*BatchWriteItemExpectation) WillReturn

WillReturns - method for set desired result

func (*BatchWriteItemExpectation) WithRequest

WithRequest - method for set Request expectation

type CreateTableExpectation

type CreateTableExpectation struct {
	// contains filtered or unexported fields
}

CreateTableExpectation struct hold expectation field, err, and result

func (*CreateTableExpectation) KeySchema

KeySchema - method for set KeySchema expectation

func (*CreateTableExpectation) Name

Name - method for set Name expectation

func (*CreateTableExpectation) WillReturn

WillReturn - method for set desired result

type DeleteItemExpectation

type DeleteItemExpectation struct {
	// contains filtered or unexported fields
}

DeleteItemExpectation struct hold expectation field, err, and result

func (*DeleteItemExpectation) Table

Table - method for set Table expectation

func (*DeleteItemExpectation) WillReturn

WillReturn - method for set desired result

func (*DeleteItemExpectation) WithKeys

WithKeys - method for set Keys expectation

type DescribeTableExpectation

type DescribeTableExpectation struct {
	// contains filtered or unexported fields
}

DescribeTableExpectation struct hold expectation field, err, and result

func (*DescribeTableExpectation) Table

Table - method for set Table expectation

func (*DescribeTableExpectation) WillReturn

WillReturn - method for set desired result

type DynaMock

type DynaMock struct {
	GetItemExpect        []*GetItemExpectation
	BatchGetItemExpect   []*BatchGetItemExpectation
	UpdateItemExpect     []*UpdateItemExpectation
	PutItemExpect        []*PutItemExpectation
	DeleteItemExpect     []*DeleteItemExpectation
	BatchWriteItemExpect []*BatchWriteItemExpectation
	CreateTableExpect    []*CreateTableExpectation
	DescribeTableExpect  []*DescribeTableExpectation
	WaitTableExistExpect []*WaitTableExistExpectation
	ScanExpect           []*ScanExpectation
	QueryExpect          []*QueryExpectation
}

DynaMock mock struct hold all expectation types

func New

New - constructor for mock instantiation Return : 1st => DynamoDBAPI implementation, used to inject app object

2nd => mock object, used to set expectation and desired result

func (*DynaMock) ExpectBatchGetItem

func (e *DynaMock) ExpectBatchGetItem() *BatchGetItemExpectation

ExpectBatchGetItem - method to start do expectation

func (*DynaMock) ExpectBatchWriteItem

func (e *DynaMock) ExpectBatchWriteItem() *BatchWriteItemExpectation

ExpectBatchWriteItem - method to start do expectation

func (*DynaMock) ExpectCreateTable

func (e *DynaMock) ExpectCreateTable() *CreateTableExpectation

ExpectCreateTable - method to start do expectation

func (*DynaMock) ExpectDeleteItem

func (e *DynaMock) ExpectDeleteItem() *DeleteItemExpectation

ExpectDeleteItem - method to start do expectation

func (*DynaMock) ExpectDescribeTable

func (e *DynaMock) ExpectDescribeTable() *DescribeTableExpectation

ExpectDescribeTable - method to start do expectation

func (*DynaMock) ExpectGetItem

func (e *DynaMock) ExpectGetItem() *GetItemExpectation

ExpectGetItem - method to start do expectation

func (*DynaMock) ExpectPutItem

func (e *DynaMock) ExpectPutItem() *PutItemExpectation

ExpectPutItem - method to start do expectation

func (*DynaMock) ExpectQuery

func (e *DynaMock) ExpectQuery() *QueryExpectation

ExpectQuery - method to start do expectation

func (*DynaMock) ExpectScan

func (e *DynaMock) ExpectScan() *ScanExpectation

ExpectScan - method to start do expectation

func (*DynaMock) ExpectUpdateItem

func (e *DynaMock) ExpectUpdateItem() *UpdateItemExpectation

ExpectUpdateItem - method to start do expectation

func (*DynaMock) ExpectWaitTableExist

func (e *DynaMock) ExpectWaitTableExist() *WaitTableExistExpectation

ExpectWaitTableExist - method to start do expectation

type GetItemExpectation

type GetItemExpectation struct {
	// contains filtered or unexported fields
}

GetItemExpectation struct hold expectation field, err, and result

func (*GetItemExpectation) Table

func (e *GetItemExpectation) Table(table string) *GetItemExpectation

Table - method for set Table expectation

func (*GetItemExpectation) WillReturn

WillReturn - method for set desired result

func (*GetItemExpectation) WithKeys

WithKeys - method for set Keys expectation

type MockDynamoDB

type MockDynamoDB struct {
	dynamodbiface.ClientAPI
	// contains filtered or unexported fields
}

MockDynamoDB struct hold DynamoDBAPI implementation and mock object

func (*MockDynamoDB) BatchGetItemRequest

func (e *MockDynamoDB) BatchGetItemRequest(input *dynamodb.BatchGetItemInput) dynamodb.BatchGetItemRequest

BatchGetItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) BatchWriteItemRequest

func (e *MockDynamoDB) BatchWriteItemRequest(input *dynamodb.BatchWriteItemInput) dynamodb.BatchWriteItemRequest

BatchWriteItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) CreateTableRequest

func (e *MockDynamoDB) CreateTableRequest(input *dynamodb.CreateTableInput) dynamodb.CreateTableRequest

CreateTableRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) DeleteItemRequest

func (e *MockDynamoDB) DeleteItemRequest(input *dynamodb.DeleteItemInput) dynamodb.DeleteItemRequest

DeleteItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) DescribeTableRequest

func (e *MockDynamoDB) DescribeTableRequest(input *dynamodb.DescribeTableInput) dynamodb.DescribeTableRequest

DescribeTableRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) GetItemRequest

func (e *MockDynamoDB) GetItemRequest(input *dynamodb.GetItemInput) dynamodb.GetItemRequest

GetItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) PutItemRequest

func (e *MockDynamoDB) PutItemRequest(input *dynamodb.PutItemInput) dynamodb.PutItemRequest

PutItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) QueryRequest

func (e *MockDynamoDB) QueryRequest(input *dynamodb.QueryInput) dynamodb.QueryRequest

QueryRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) ScanRequest

func (e *MockDynamoDB) ScanRequest(input *dynamodb.ScanInput) dynamodb.ScanRequest

ScanRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) UpdateItemRequest

func (e *MockDynamoDB) UpdateItemRequest(input *dynamodb.UpdateItemInput) dynamodb.UpdateItemRequest

UpdateItemRequest - this func will be invoked when test running matching expectation with actual input

func (*MockDynamoDB) WaitUntilTableExists

func (e *MockDynamoDB) WaitUntilTableExists(ctx context.Context, input *dynamodb.DescribeTableInput, opt ...aws.WaiterOption) error

WaitUntilTableExists - this func will be invoked when test running matching expectation with actual input

type PutItemExpectation

type PutItemExpectation struct {
	// contains filtered or unexported fields
}

PutItemExpectation struct hold expectation field, err, and result

func (*PutItemExpectation) Table

func (e *PutItemExpectation) Table(table string) *PutItemExpectation

Table - method for set Table expectation

func (*PutItemExpectation) WillReturn

WillReturn - method for set desired result

func (*PutItemExpectation) WithItems

WithItems - method for set Items expectation

type QueryExpectation

type QueryExpectation struct {
	// contains filtered or unexported fields
}

QueryExpectation struct hold expectation field, err, and result

func (*QueryExpectation) Table

func (e *QueryExpectation) Table(table string) *QueryExpectation

Table - method for set Table expectation

func (*QueryExpectation) WillReturn

WillReturn - method for set desired result

type ScanExpectation

type ScanExpectation struct {
	// contains filtered or unexported fields
}

ScanExpectation struct hold expectation field, err, and result

func (*ScanExpectation) Table

func (e *ScanExpectation) Table(table string) *ScanExpectation

Table - method for set Table expectation

func (*ScanExpectation) WillReturn

WillReturn - method for set desired result

type UpdateItemExpectation

type UpdateItemExpectation struct {
	// contains filtered or unexported fields
}

UpdateItemExpectation struct hold expectation field, err, and result

func (*UpdateItemExpectation) Table

Table - method for set Table expectation

func (*UpdateItemExpectation) Updates

Updates - method for set Updates expectation

func (*UpdateItemExpectation) WillReturn

WillReturn - method for set desired result

func (*UpdateItemExpectation) WithKeys

WithKeys - method for set Keys expectation

type WaitTableExistExpectation

type WaitTableExistExpectation struct {
	// contains filtered or unexported fields
}

WaitTableExistExpectation struct hold expectation field, err, and result

func (*WaitTableExistExpectation) Table

Table - method for set Table expectation

func (*WaitTableExistExpectation) WillReturn

WillReturn - method for set desired result

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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