Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test documentation #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -125,4 +148,4 @@ DYNAMO_TEST_REGION=us-west-2 go test github.com/guregu/dynamo/... -cover

### License

BSD
BSD