Skip to content

Commit 46635c4

Browse files
committed
docs: update testcontainers guide examples to use CockroachDB
Replace all remaining SetupTestContainer/PostgresRepository references with testdb.SetupCockroachDB pattern throughout examples, best practices, troubleshooting, and migration sections.
1 parent c1f0079 commit 46635c4

1 file changed

Lines changed: 44 additions & 43 deletions

File tree

docs/guides/testcontainers-usage.md

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ deleted.
9393
1. **Use defer for cleanup**:
9494

9595
```go
96-
tc := testhelpers.SetupTestContainer(t)
97-
defer tc.Cleanup(t)
96+
db, cleanup := testdb.SetupCockroachDB(t, nil)
97+
defer cleanup()
9898
```
9999

100100
2. **Run tests in parallel** (when safe):
101101

102102
```go
103103
func TestConcurrentOperations(t *testing.T) {
104104
t.Parallel()
105-
tc := testhelpers.SetupTestContainer(t)
106-
defer tc.Cleanup(t)
105+
db, cleanup := testdb.SetupCockroachDB(t, nil)
106+
defer cleanup()
107107
}
108108
```
109109

@@ -126,15 +126,16 @@ deleted.
126126

127127
```go
128128
func TestCreate(t *testing.T) {
129-
tc := testhelpers.SetupTestContainer(t)
130-
defer tc.Cleanup(t)
129+
db, cleanup := testdb.SetupCockroachDB(t, nil)
130+
defer cleanup()
131131

132+
repo := repository.NewRepository(db)
132133
log := createTestLog(t, "ACC-001")
133-
err := tc.Repo.Create(context.Background(), log)
134+
err := repo.Create(context.Background(), log)
134135
require.NoError(t, err)
135136

136137
// Verify
137-
retrieved, err := tc.Repo.FindByID(context.Background(), log.LogID)
138+
retrieved, err := repo.FindByID(context.Background(), log.LogID)
138139
require.NoError(t, err)
139140
assert.Equal(t, log.LogID, retrieved.LogID)
140141
}
@@ -144,15 +145,16 @@ func TestCreate(t *testing.T) {
144145

145146
```go
146147
func TestCreateBatch(t *testing.T) {
147-
tc := testhelpers.SetupTestContainer(t)
148-
defer tc.Cleanup(t)
148+
db, cleanup := testdb.SetupCockroachDB(t, nil)
149+
defer cleanup()
149150

151+
repo := repository.NewRepository(db)
150152
logs := make([]*domain.FinancialPositionLog, 100)
151153
for i := 0; i < 100; i++ {
152154
logs[i] = createTestLog(t, fmt.Sprintf("ACC-%03d", i))
153155
}
154156

155-
err := tc.Repo.CreateBatch(context.Background(), logs)
157+
err := repo.CreateBatch(context.Background(), logs)
156158
require.NoError(t, err)
157159
}
158160
```
@@ -161,22 +163,21 @@ func TestCreateBatch(t *testing.T) {
161163

162164
```go
163165
func TestCustomQuery(t *testing.T) {
164-
tc := testhelpers.SetupTestContainer(t)
165-
defer tc.Cleanup(t)
166+
db, cleanup := testdb.SetupCockroachDB(t, nil)
167+
defer cleanup()
168+
169+
repo := repository.NewRepository(db)
166170

167171
// Insert test data
168172
log := createTestLog(t, "ACC-001")
169-
err := tc.Repo.Create(context.Background(), log)
173+
err := repo.Create(context.Background(), log)
170174
require.NoError(t, err)
171175

172-
// Run custom query
176+
// Run custom query via GORM's underlying connection
173177
var status string
174-
err = tc.Pool.QueryRow(context.Background(),
175-
`SELECT current_status
176-
FROM position_keeping.financial_position_logs
177-
WHERE log_id = $1`,
178-
log.LogID).Scan(&status)
179-
require.NoError(t, err)
178+
db.Raw(`SELECT current_status
179+
FROM position_keeping.financial_position_logs
180+
WHERE log_id = ?`, log.LogID).Scan(&status)
180181
assert.Equal(t, "PENDING", status)
181182
}
182183
```
@@ -186,13 +187,15 @@ func TestCustomQuery(t *testing.T) {
186187
```go
187188
func BenchmarkCreate(b *testing.B) {
188189
// Setup once for all iterations
189-
tc := testhelpers.SetupTestContainer(&testing.T{})
190-
defer tc.Cleanup(&testing.T{})
190+
t := &testing.T{}
191+
db, cleanup := testdb.SetupCockroachDB(t, nil)
192+
defer cleanup()
191193

194+
repo := repository.NewRepository(db)
192195
b.ResetTimer()
193196
for i := 0; i < b.N; i++ {
194-
log := createTestLog(&testing.T{}, fmt.Sprintf("ACC-%d", i))
195-
_ = tc.Repo.Create(context.Background(), log)
197+
log := createTestLog(t, fmt.Sprintf("ACC-%d", i))
198+
_ = repo.Create(context.Background(), log)
196199
}
197200
}
198201
```
@@ -225,11 +228,11 @@ docker system df
225228

226229
**Problem**: Too many open connections
227230

228-
**Solution**: Ensure `Cleanup()` is called with defer:
231+
**Solution**: Ensure `cleanup()` is called with defer:
229232

230233
```go
231-
tc := testhelpers.SetupTestContainer(t)
232-
defer tc.Cleanup(t) // CRITICAL - must use defer
234+
db, cleanup := testdb.SetupCockroachDB(t, nil)
235+
defer cleanup() // CRITICAL - must use defer
233236
```
234237

235238
### Slow Tests
@@ -244,25 +247,24 @@ defer tc.Cleanup(t) // CRITICAL - must use defer
244247

245248
## Migration from Inline Setup
246249

247-
If you have tests with inline testcontainer setup, migrate to this package:
250+
If you have tests with inline testcontainer setup, migrate to `testdb.SetupCockroachDB`:
248251

249252
### Before
250253

251254
```go
252255
func TestOldWay(t *testing.T) {
253256
ctx := context.Background()
254-
pgContainer, err := postgres.Run(ctx, "postgres:16-alpine", ...)
257+
container, err := cockroach.Run(ctx, "cockroachdb/cockroach:latest-v24.3", ...)
255258
require.NoError(t, err)
256-
defer pgContainer.Terminate(ctx)
259+
defer container.Terminate(ctx)
257260

258-
connStr, _ := pgContainer.ConnectionString(ctx, "sslmode=disable")
259-
pool, _ := pgxpool.New(ctx, connStr)
260-
defer pool.Close()
261+
connStr, _ := container.ConnectionString(ctx)
262+
db, _ := gorm.Open(postgres.Open(connStr), &gorm.Config{})
261263

262264
// Load schema manually...
263-
_, err = pool.Exec(ctx, "CREATE TABLE...")
265+
db.Exec("CREATE TABLE...")
264266

265-
repo := repository.NewPostgresRepository(pool)
267+
repo := repository.NewRepository(db)
266268
// ... test code ...
267269
}
268270
```
@@ -271,10 +273,11 @@ func TestOldWay(t *testing.T) {
271273

272274
```go
273275
func TestNewWay(t *testing.T) {
274-
tc := testhelpers.SetupTestContainer(t)
275-
defer tc.Cleanup(t)
276+
db, cleanup := testdb.SetupCockroachDB(t, nil)
277+
defer cleanup()
276278

277-
// ... test code using tc.Repo ...
279+
repo := repository.NewRepository(db)
280+
// ... test code ...
278281
}
279282
```
280283

@@ -287,9 +290,7 @@ func TestNewWay(t *testing.T) {
287290

288291
## See Also
289292

290-
- [postgres_repository_test.go][repo-test] - Example integration tests
291-
- [postgres_repository_bench_test.go][repo-bench] - Example benchmarks
293+
- [repository_test.go][repo-test] - Example integration tests
292294
- [testcontainers-go docs](https://golang.testcontainers.org/) - Official documentation
293295

294-
[repo-test]: ../../services/position-keeping/repository/postgres_repository_test.go
295-
[repo-bench]: ../../services/position-keeping/repository/postgres_repository_bench_test.go
296+
[repo-test]: ../../services/position-keeping/repository/repository_test.go

0 commit comments

Comments
 (0)