|
1 | | -# poutine |
| 1 | +# poutine |
| 2 | + |
| 3 | +[](https://golang.org/) |
| 4 | +[](LICENSE) |
| 5 | +[](https://goreportcard.com/report/github.com/calumari/poutine) |
| 6 | + |
| 7 | +A Go library for database integration testing using JSON fixtures and snapshot assertions. |
| 8 | + |
| 9 | +## Packages |
| 10 | + |
| 11 | +* **`poutine`** – Core library providing a database-agnostic testing interface. |
| 12 | +* **`database/mongodb`** – MongoDB driver implementation. |
| 13 | +* **`testine`** – Utilities for loading fixtures, capturing snapshots, and cleaning up. |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +Database integration tests can be tricky - like trying to eat poutine with a spoon. This library helps you **put in** your test data cleanly: |
| 18 | + |
| 19 | +* Version-controlled JSON fixtures for consistent test data |
| 20 | +* Snapshot assertions to compare current database state with expected data |
| 21 | +* Handling of generated values like ObjectIDs and timestamps |
| 22 | +* Clean teardown so you don't leave a mess behind |
| 23 | +* Automatic cleanup of test data |
| 24 | +* Support for multiple backends through a driver interface |
| 25 | + |
| 26 | +The goal is to reduce boilerplate and make test code easier to read and maintain, without adding extra complexity. |
| 27 | + |
| 28 | +## Install |
| 29 | + |
| 30 | +```bash |
| 31 | +go get github.com/calumari/poutine |
| 32 | +# Optional MongoDB driver |
| 33 | +go get github.com/calumari/poutine/database/mongodb |
| 34 | +``` |
| 35 | + |
| 36 | +## Quick Start (MongoDB) |
| 37 | + |
| 38 | +```go |
| 39 | +package mytest |
| 40 | + |
| 41 | +import ( |
| 42 | + "testing" |
| 43 | + |
| 44 | + "github.com/calumari/poutine" |
| 45 | + "github.com/calumari/poutine/database/mongodb" |
| 46 | + "github.com/calumari/poutine/testine" |
| 47 | +) |
| 48 | + |
| 49 | +func Test_UserFlow(t *testing.T) { |
| 50 | + // Acquire a *mongo.Database instance |
| 51 | + pt := poutine.New(mongodb.NewDriver(db)) |
| 52 | + ti, err := testine.New(pt) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("failed to create test helper: %v", err) |
| 55 | + } |
| 56 | + ti.Cleanup(t) // register teardown |
| 57 | + |
| 58 | + // Seed DB from a JSON fixture |
| 59 | + snap := ti.Seed(t, ti.LoadJSON(t, "testdata/seed.json")) |
| 60 | + |
| 61 | + // ... run code that modifies the database ... |
| 62 | + snap.Assert(t) // check expected state |
| 63 | + |
| 64 | + // Or compare against a specific expected state |
| 65 | + ti.Assert(t, ti.LoadJSON(t, "testdata/after.json")) |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## JSON Fixture Format |
| 70 | + |
| 71 | +JSON fixtures are objects with collection (or table) names as keys and arrays of documents as values: |
| 72 | + |
| 73 | +```jsonc |
| 74 | +{ |
| 75 | + "users": [ |
| 76 | + { "_id": { "$oid": true}, "email": "[email protected]"}, |
| 77 | + { "_id": { "$oid": "507f1f77bcf86cd799439011"}, "email": "[email protected]"} |
| 78 | + ], |
| 79 | + "pets": [ |
| 80 | + { "name": "Fido", "ownerEmail": "[email protected]"} |
| 81 | + ] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +* `{"$oid": true}` – matches any ObjectID (wildcard) |
| 86 | +* `{"$oid": "hex_string"}` – matches a specific ObjectID |
| 87 | + |
| 88 | +## Using `testine` |
| 89 | + |
| 90 | +`testine.T` wraps a `Poutine` instance and provides helper methods: |
| 91 | + |
| 92 | +* `Seed(t, doc) *Snapshot` – seed DB and capture snapshot |
| 93 | +* `Assert(t, expectedDoc)` – compare current DB state to expected |
| 94 | +* `Snapshot.Assert(t)` – compare current state to previously captured snapshot |
| 95 | +* `Cleanup(t)` – register teardown |
| 96 | +* `LoadJSON(t, path|glob|dir)` – load JSON from file, directory, or glob; supports caching with `testine.WithDocumentCache()` |
| 97 | + |
| 98 | +## Custom Drivers |
| 99 | + |
| 100 | +Implement the `database.Driver` interface to support new databases: |
| 101 | + |
| 102 | +```go |
| 103 | +type Driver interface { |
| 104 | + Seed(ctx context.Context, root jwalk.Document) (jwalk.Document, error) |
| 105 | + Snapshot(ctx context.Context) (jwalk.Document, error) |
| 106 | + Teardown(ctx context.Context) error |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +See [`database/mongodb/driver.go`](database/mongodb/driver.go) for a reference implementation. |
0 commit comments