modulepackage
1.0.2
Repository: https://github.com/figleafcom/go-dynamock-v2.git
Documentation: pkg.go.dev
# README
go-dynamock-v2
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
- Go >= 1.11.x
- AWS SDK GO V2 >= v0.9.0
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/groovili/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
# Functions
New - constructor for mock instantiation Return : 1st => DynamoDBAPI implementation, used to inject app object 2nd => mock object, used to set expectation and desired result.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author