Switch to using the dep tool and update all the dependencies

This commit is contained in:
Nick Craig-Wood 2017-05-11 15:39:54 +01:00
parent 5135ff73cb
commit 98c2d2c41b
5321 changed files with 4483201 additions and 5922 deletions

View file

@ -0,0 +1,52 @@
# Example
You can instantiate `*dynamodb.DynamoDB` and pass that as a parameter to all
methods connecting to DynamoDB, or as `unitTest` demonstrates, create your own
`type` and pass it along as a field.
## Test-compatible DynamoDB field
If you use `*dynamodb.DynamoDB` as a field, you will be unable to unit test it,
as documented in #88. Cast it instead as `dynamodbiface.DynamoDBAPI`:
```go
type ItemGetter struct {
DynamoDB dynamodbiface.DynamoDBAPI
}
```
## Querying actual DynamoDB
You'll need an `*aws.Config` and `*session.Session` for these to work correctly:
```go
// Setup
var getter = new(ItemGetter)
var config *aws.Config = &aws.Config{Region: aws.String("us-west-2"),}
var sess *session.Session = session.NewSession(config)
var svc *dynamodb.DynamoDB = dynamodb.New()
getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc)
// Finally
getter.DynamoDB.GetItem(/* ... */)
```
## Querying in tests
Construct a `fakeDynamoDB` and add the necessary methods for each of those
structs (custom ones for `ItemGetter` and [whatever methods you're using for
DynamoDB](https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/dynamodbiface/interface.go)),
and you're good to go!
```go
type fakeDynamoDB struct {
dynamodbiface.DynamoDBAPI
}
var getter = new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{}
// And to run it (assuming you've mocked fakeDynamoDB.GetItem)
getter.DynamoDB.GetItem(/* ... */)
```
## Output
```
$ go test -tags example -cover
PASS
coverage: 100.0% of statements
ok _/Users/shatil/workspace/aws-sdk-go/example/service/dynamodb/unitTest 0.008s
```

View file

@ -0,0 +1,41 @@
// +build example
// Package unitTest demonstrates how to unit test, without needing to pass a
// connector to every function, code that uses DynamoDB.
package unitTest
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// ItemGetter can be assigned a DynamoDB connector like:
// svc := dynamodb.DynamoDB(sess)
// getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc)
type ItemGetter struct {
DynamoDB dynamodbiface.DynamoDBAPI
}
// Get a value from a DynamoDB table containing entries like:
// {"id": "my primary key", "value": "valuable value"}
func (ig *ItemGetter) Get(id string) (value string) {
var input = &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String(id),
},
},
TableName: aws.String("my_table"),
AttributesToGet: []*string{
aws.String("value"),
},
}
if output, err := ig.DynamoDB.GetItem(input); err == nil {
if _, ok := output.Item["value"]; ok {
dynamodbattribute.Unmarshal(output.Item["value"], &value)
}
}
return
}

View file

@ -0,0 +1,59 @@
// +build example
// Unit tests for package unitTest.
package unitTest
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// A fakeDynamoDB instance. During testing, instatiate ItemGetter, then simply
// assign an instance of fakeDynamoDB to it.
type fakeDynamoDB struct {
dynamodbiface.DynamoDBAPI
payload map[string]string // Store expected return values
err error
}
// Mock GetItem such that the output returned carries values identical to input.
func (fd *fakeDynamoDB) GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
output := new(dynamodb.GetItemOutput)
output.Item = make(map[string]*dynamodb.AttributeValue)
for key, value := range fd.payload {
output.Item[key] = &dynamodb.AttributeValue{
S: aws.String(value),
}
}
return output, fd.err
}
func TestItemGetterGet(t *testing.T) {
expectedKey := "expected key"
expectedValue := "expected value"
getter := new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{
payload: map[string]string{"id": expectedKey, "value": expectedValue},
}
if actualValue := getter.Get(expectedKey); actualValue != expectedValue {
t.Errorf("Expected %q but got %q", expectedValue, actualValue)
}
}
// When DynamoDB.GetItem returns a non-nil error, expect an empty string.
func TestItemGetterGetFail(t *testing.T) {
expectedKey := "expected key"
expectedValue := "expected value"
getter := new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{
payload: map[string]string{"id": expectedKey, "value": expectedValue},
err: errors.New("any error"),
}
if actualValue := getter.Get(expectedKey); len(actualValue) > 0 {
t.Errorf("Expected %q but got %q", expectedValue, actualValue)
}
}