Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 02-Jan-2020 | - | ||||
README.md | H A D | 02-Jan-2020 | 1.5 KiB | 53 | 45 | |
unitTest.go | H A D | 02-Jan-2020 | 1.1 KiB | 42 | 29 | |
unitTest_test.go | H A D | 02-Jan-2020 | 1.8 KiB | 60 | 46 |
README.md
1# Example 2You can instantiate `*dynamodb.DynamoDB` and pass that as a parameter to all 3methods connecting to DynamoDB, or as `unitTest` demonstrates, create your own 4`type` and pass it along as a field. 5 6## Test-compatible DynamoDB field 7If you use `*dynamodb.DynamoDB` as a field, you will be unable to unit test it, 8as documented in #88. Cast it instead as `dynamodbiface.DynamoDBAPI`: 9 10```go 11type ItemGetter struct { 12 DynamoDB dynamodbiface.DynamoDBAPI 13} 14``` 15 16## Querying actual DynamoDB 17You'll need an `*aws.Config` and `*session.Session` for these to work correctly: 18 19```go 20// Setup 21var getter = new(ItemGetter) 22var config *aws.Config = &aws.Config{Region: aws.String("us-west-2"),} 23var sess *session.Session = session.NewSession(config) 24var svc *dynamodb.DynamoDB = dynamodb.New() 25getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc) 26// Finally 27getter.DynamoDB.GetItem(/* ... */) 28``` 29 30## Querying in tests 31Construct a `fakeDynamoDB` and add the necessary methods for each of those 32structs (custom ones for `ItemGetter` and [whatever methods you're using for 33DynamoDB](https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/dynamodbiface/interface.go)), 34and you're good to go! 35 36```go 37type fakeDynamoDB struct { 38 dynamodbiface.DynamoDBAPI 39} 40var getter = new(ItemGetter) 41getter.DynamoDB = &fakeDynamoDB{} 42// And to run it (assuming you've mocked fakeDynamoDB.GetItem) 43getter.DynamoDB.GetItem(/* ... */) 44``` 45 46## Output 47``` 48$ go test -tags example -cover 49PASS 50coverage: 100.0% of statements 51ok _/Users/shatil/workspace/aws-sdk-go/example/service/dynamodb/unitTest 0.008s 52``` 53