diff --git a/README.md b/README.md index ebf7691..e7a33ae 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,29 @@ var someBook book err := db.Table("Books").Get("ID", 555).One(dynamo.AWSEncoding(&someBook)) ``` +### Unit Testing Your Dynamo App +If you need to write unit tests around your application using the dynamo client, +you may do so by using [`dynamodbiface`](https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbiface/) from Amazon, and instantiating the client +using the function `dynamo.NewFromIface`: + +```go +// Create the session and database using the +// using AWS functions +s := session.Must(session.NewSession()) +i := dynamodb.New(s, aws.NewConfig().WithRegion("us-west-2")) +db := dynamo.NewFromIface(i) +``` + +You may then create mocks by implementing the following functions corresponding to +which dynamo operation you wish to test: + +| Operation | Function to Override in dynamodbiface | +| ----------- | ------------------------------------- | +| `Get` (one) | `GetItemWithContext` | +| `Get` (all) | `QueryWithContext` | +| `Scan` | `ScanWithContext` | +| `Put` | `PutItemWithContext` | + ### Integration tests By default, tests are run in offline mode. Create a table called `TestDB`, with a Number Parition Key called `UserID` and a String Sort Key called `Time`. Change the table name with the environment variable `DYNAMO_TEST_TABLE`. You must specify `DYNAMO_TEST_REGION`, setting it to the AWS region where your test table is. @@ -125,4 +148,4 @@ DYNAMO_TEST_REGION=us-west-2 go test github.com/guregu/dynamo/... -cover ### License -BSD \ No newline at end of file +BSD