https://github.com/groovili/go-dynamock-v2
Amazon DynamoDB mock for unit testing
https://github.com/groovili/go-dynamock-v2
aws dynamo go golang-package mock test
Last synced: 6 months ago
JSON representation
Amazon DynamoDB mock for unit testing
- Host: GitHub
- URL: https://github.com/groovili/go-dynamock-v2
- Owner: groovili
- License: mit
- Fork: true (gusaul/go-dynamock)
- Created: 2019-05-29T13:37:37.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-06T10:46:48.000Z (about 7 years ago)
- Last Synced: 2024-11-17T12:42:33.094Z (over 1 year ago)
- Topics: aws, dynamo, go, golang-package, mock, test
- Language: Go
- Homepage:
- Size: 78.1 KB
- Stars: 4
- Watchers: 0
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-dynamock-v2
[](https://godoc.org/github.com/groovili/go-dynamock-v2)
[](https://goreportcard.com/report/github.com/groovili/go-dynamock-v2)
[](https://travis-ci.org/groovili/go-dynamock-v2)
[](https://codecov.io/gh/groovili/go-dynamock-v2)
Amazon DynamoDB mock for unit testing, fully compatible with [SDK](https://github.com/aws/aws-sdk-go-v2).
Visit [GoDoc](https://godoc.org/github.com/groovili/go-dynamock-v2) for public API documentation.
Thanks to [gusaul](https://github.com/gusaul) for the first version of package [go-dynamock](https://github.com/gusaul/go-dynamock).
## Requirements
- Go >= 1.11.x
- [AWS SDK GO V2](https://github.com/aws/aws-sdk-go-v2) >= v0.9.0
## Usage
To use mock you should depend in your code on [ClientAPI interface](https://github.com/aws/aws-sdk-go-v2/tree/master/service/dynamodb/dynamodbiface), instead of dependency on specific DynamoDB instance.
``` go
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
``` go
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
``` go
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
``` go
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](https://github.com/groovili/go-dynamock-v2/blob/master/LICENSE)