-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstore_factory_test.go
More file actions
82 lines (68 loc) · 1.96 KB
/
Copy pathstore_factory_test.go
File metadata and controls
82 lines (68 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//go:build !sqlite
package floxy
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
// useSQLite is false when sqlite build tag is not set (default: use PostgreSQL)
var useSQLite = false
// setupTestStore creates a PostgreSQL store using testcontainers
func setupTestStore(t *testing.T) (Store, TxManager, func()) {
t.Helper()
ctx := context.Background()
// Start PostgreSQL container
postgresContainer, err := postgres.Run(ctx,
"postgres:17-alpine",
postgres.WithDatabase("floxy"),
postgres.WithUsername("user"),
postgres.WithPassword("password"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(30*time.Second)),
)
if err != nil {
t.Fatalf("Failed to start postgres container: %v", err)
}
// Get connection string
connStr, err := postgresContainer.ConnectionString(ctx, "sslmode=disable")
if err != nil {
_ = postgresContainer.Terminate(ctx)
t.Fatalf("Failed to get connection string: %v", err)
}
// Wait a bit for PostgreSQL to be fully ready
time.Sleep(2 * time.Second)
// Create connection pool with retry
var pool *pgxpool.Pool
for i := 0; i < 5; i++ {
pool, err = pgxpool.New(ctx, connStr)
if err == nil {
break
}
if i < 4 {
time.Sleep(time.Duration(i+1) * time.Second)
}
}
if err != nil {
_ = postgresContainer.Terminate(ctx)
t.Fatalf("Failed to create connection pool: %v", err)
}
// Run migrations
if err := RunMigrations(ctx, pool); err != nil {
pool.Close()
_ = postgresContainer.Terminate(ctx)
t.Fatalf("Failed to run migrations: %v", err)
}
store := NewStore(pool)
txManager := NewTxManager(pool)
cleanup := func() {
pool.Close()
_ = postgresContainer.Terminate(context.Background())
}
return store, txManager, cleanup
}