dynamock

package module
v0.0.0-...-42bb93b Latest Latest
Warning

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

Go to latest
Published: May 3, 2020 License: MIT Imports: 7 Imported by: 0

README

GoDoc Go Report Card Build Status

go-dynamock

Amazon Dynamo DB Mock Driver for Golang to Test Database Interactions

Install

go get github.com/gusaul/go-dynamock

Examples Usage

Visit godoc for general examples and public api reference.

DynamoDB configuration

First of all, change the dynamodb configuration to use the dynamodb interface. see code below:

package main

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

type MyDynamo struct {
    Db dynamodbiface.DynamoDBAPI
}

var Dyna *MyDynamo

func ConfigureDynamoDB() {
	Dyna = new(MyDynamo)
	awsSession, _ := session.NewSession(&aws.Config{Region: aws.String("ap-southeast-2")})
	svc := dynamodb.New(awsSession)
	Dyna.Db = dynamodbiface.DynamoDBAPI(svc)
}

the purpose of code above is to make your dynamoDB object can be mocked by dynamock through the dynamodbiface.

Something you may wanna test
package main

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

func GetName(id string) (*string, error) {
	parameter := &dynamodb.GetItemInput{
		Key: map[string]*dynamodb.AttributeValue{
			"id": {
				N: aws.String(id),
			},
		},
		TableName: aws.String("employee"),
	}

	response, err := Dyna.Db.GetItem(parameter)
	if err != nil {
		return nil, err
	}

	name := response.Item["name"].S
	return name, nil
}
Test with DynaMock
package examples

import (
	"testing"

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

var mock *dynamock.DynaMock

func init() {
	Dyna = new(MyDynamo)
	Dyna.Db, mock = dynamock.New()
}

func TestGetName(t *testing.T) {
	expectKey := map[string]*dynamodb.AttributeValue{
		"id": {
			N: aws.String("1"),
		},
	}

	expectedResult := aws.String("jaka")
	result := dynamodb.GetItemOutput{
		Item: map[string]*dynamodb.AttributeValue{
			"name": {
				S: expectedResult,
			},
		},
	}

	//lets start dynamock in action
	mock.ExpectGetItem().ToTable("employee").WithKeys(expectKey).WillReturns(result)

	actualResult, _ := GetName("1")
	if actualResult != expectedResult {
		t.Errorf("Test Fail")
	}
}

if you just wanna expect the table

mock.ExpectGetItem().ToTable("employee").WillReturns(result)

or maybe you didn't care with any arguments, you just need to determine the result

mock.ExpectGetItem().WillReturns(result)

and you can do multiple expectations at once, then the expectation will be executed sequentially.

mock.ExpectGetItem().WillReturns(resultOne)
mock.ExpectUpdateItem().WillReturns(resultTwo)
mock.ExpectGetItem().WillReturns(resultThree)

/* Result
the first call of GetItem will return resultOne
the second call of GetItem will return resultThree
and the only call of UpdateItem will return resultTwo */
Currently Supported Functions
CreateTable(*dynamodb.CreateTableInput) (*dynamodb.CreateTableOutput, error)
DescribeTable(*dynamodb.DescribeTableInput) (*dynamodb.DescribeTableOutput, error)
GetItem(*dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error)
GetItemWithContext(aws.Context, *dynamodb.GetItemInput, ...request.Option) (*dynamodb.GetItemOutput, error)
PutItem(*dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error)
UpdateItem(*dynamodb.UpdateItemInput) (*dynamodb.UpdateItemOutput, error)
UpdateItemWithContext(aws.Context, *dynamodb.UpdateItemInput, ...request.Option) (*dynamodb.UpdateItemOutput, error)
DeleteItem(*dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error)
BatchGetItem(*dynamodb.BatchGetItemInput) (*dynamodb.BatchGetItemOutput, error)
BatchGetItemWithContext(aws.Context, *dynamodb.BatchGetItemInput, ...request.Option) (*dynamodb.BatchGetItemOutput, error)
BatchWriteItem(*dynamodb.BatchWriteItemInput) (*dynamodb.BatchWriteItemOutput, error)
WaitUntilTableExists(*dynamodb.DescribeTableInput) error
Scan(input *dynamodb.ScanInput) (*dynamodb.ScanOutput, error)
Query(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error)

Contributions

Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) - please open an issue before, to discuss whether these changes can be accepted. All backward incompatible changes are and will be treated cautiously

License

The MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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) WillReturns

WillReturns - 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) WillReturns

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) WillReturns

WillReturns - 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) ToTable

ToTable - method for set Table expectation

func (*DeleteItemExpectation) WillReturns

WillReturns - 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) WillReturns

WillReturns - 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
	TransactWriteItemsExpect []TransactWriteItemsExpectation
}

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) ExpectTransactWriteItems

func (e *DynaMock) ExpectTransactWriteItems() *TransactWriteItemsExpectation

ExpectTransactWriteItems - 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) ToTable

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

ToTable - method for set Table expectation

func (*GetItemExpectation) WillReturns

WillReturns - method for set desired result

func (*GetItemExpectation) WithKeys

WithKeys - method for set Keys expectation

type MockDynamoDB

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

MockDynamoDB struct hold DynamoDBAPI implementation and mock object

func (*MockDynamoDB) BatchGetItem

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

func (*MockDynamoDB) BatchGetItemWithContext

func (e *MockDynamoDB) BatchGetItemWithContext(ctx aws.Context, input *dynamodb.BatchGetItemInput, opt ...request.Option) (*dynamodb.BatchGetItemOutput, error)

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

func (*MockDynamoDB) BatchWriteItem

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

func (*MockDynamoDB) BatchWriteItemWithContext

func (e *MockDynamoDB) BatchWriteItemWithContext(ctx aws.Context, input *dynamodb.BatchWriteItemInput, opt ...request.Option) (*dynamodb.BatchWriteItemOutput, error)

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

func (*MockDynamoDB) CreateTable

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

func (*MockDynamoDB) DeleteItem

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

func (*MockDynamoDB) DeleteItemWithContext

func (e *MockDynamoDB) DeleteItemWithContext(ctx aws.Context, input *dynamodb.DeleteItemInput, options ...request.Option) (*dynamodb.DeleteItemOutput, error)

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

func (*MockDynamoDB) DescribeTable

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

func (*MockDynamoDB) GetItem

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

func (*MockDynamoDB) GetItemWithContext

func (e *MockDynamoDB) GetItemWithContext(ctx aws.Context, input *dynamodb.GetItemInput, opt ...request.Option) (*dynamodb.GetItemOutput, error)

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

func (*MockDynamoDB) PutItem

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

func (*MockDynamoDB) PutItemWithContext

func (e *MockDynamoDB) PutItemWithContext(ctx aws.Context, input *dynamodb.PutItemInput, opt ...request.Option) (*dynamodb.PutItemOutput, error)

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

func (*MockDynamoDB) Query

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

func (*MockDynamoDB) QueryWithContext

func (e *MockDynamoDB) QueryWithContext(ctx aws.Context, input *dynamodb.QueryInput, options ...request.Option) (*dynamodb.QueryOutput, error)

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

func (*MockDynamoDB) Scan

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

func (*MockDynamoDB) TransactWriteItems

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

func (*MockDynamoDB) UpdateItem

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

func (*MockDynamoDB) UpdateItemWithContext

func (e *MockDynamoDB) UpdateItemWithContext(ctx aws.Context, input *dynamodb.UpdateItemInput, opt ...request.Option) (*dynamodb.UpdateItemOutput, error)

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

func (*MockDynamoDB) WaitUntilTableExists

func (e *MockDynamoDB) WaitUntilTableExists(input *dynamodb.DescribeTableInput) 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) ToTable

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

ToTable - method for set Table expectation

func (*PutItemExpectation) WillReturns

WillReturns - 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) WillReturns

WillReturns - 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) WillReturns

func (e *ScanExpectation) WillReturns(res dynamodb.ScanOutput) *ScanExpectation

WillReturns - method for set desired result

type TransactWriteItemsExpectation

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

TransactWriteItemsExpectation struct holds field, err, and result

func (*TransactWriteItemsExpectation) Table

Table - method for set Table expectation

func (*TransactWriteItemsExpectation) WillReturns

WillReturns - method for set desired result

func (*TransactWriteItemsExpectation) WithItems

WithItems - method for set Items expectation

type UpdateItemExpectation

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

UpdateItemExpectation struct hold expectation field, err, and result

func (*UpdateItemExpectation) ExpressionAttributeValues

func (e *UpdateItemExpectation) ExpressionAttributeValues(attrsValues map[string]*dynamodb.AttributeValue) *UpdateItemExpectation

ExpressionAttributeValues - method for set ExpressionAttributeValues expectation

func (*UpdateItemExpectation) ToTable

ToTable - method for set Table expectation

func (*UpdateItemExpectation) Updates

Updates - method for set Updates expectation

func (*UpdateItemExpectation) WillReturns

WillReturns - method for set desired result

func (*UpdateItemExpectation) WithError

WithError - method for set errors expectation

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) WillReturns

WillReturns - method for set desired result

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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