Skip to content

Commit e2676f6

Browse files
committed
tests: simplify database migrations
Open ./migrations fs.FS as a directory tree and just once.
1 parent 0a3b23f commit e2676f6

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

internal/postgres/postgres_test.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"flag"
6+
"io/fs"
67
"log"
78
"log/slog"
89
"os"
@@ -17,11 +18,19 @@ import (
1718

1819
var force = flag.Bool("force", false, "Force cleaning the database before starting")
1920

21+
// migrations for testing the database.
22+
var migrations fs.FS
23+
2024
func TestMain(m *testing.M) {
2125
if os.Getenv("INTEGRATION_TESTDB") != "true" {
2226
log.Printf("Skipping tests that require database connection")
2327
return
2428
}
29+
migRoot, err := os.OpenRoot("../../migrations")
30+
if err != nil {
31+
log.Fatalf("cannot open DB migrations: %v", err)
32+
}
33+
migrations = migRoot.FS()
2534
os.Exit(m.Run())
2635
}
2736

@@ -30,7 +39,7 @@ func TestTransactionContext(t *testing.T) {
3039

3140
migration := sqltest.New(t, sqltest.Options{
3241
Force: *force,
33-
Files: os.DirFS("../../migrations"),
42+
Files: migrations,
3443
})
3544
pool := migration.Setup(t.Context(), "")
3645
db := NewDB(pool, slog.Default())
@@ -51,7 +60,7 @@ func TestTransactionContextCanceled(t *testing.T) {
5160

5261
migration := sqltest.New(t, sqltest.Options{
5362
Force: *force,
54-
Files: os.DirFS("../../migrations"),
63+
Files: migrations,
5564
})
5665
pool := migration.Setup(t.Context(), "")
5766
db := NewDB(pool, slog.Default())
@@ -86,7 +95,7 @@ func TestWithAcquire(t *testing.T) {
8695
t.Parallel()
8796
migration := sqltest.New(t, sqltest.Options{
8897
Force: *force,
89-
Files: os.DirFS("../../migrations"),
98+
Files: migrations,
9099
})
91100
pool := migration.Setup(t.Context(), "")
92101
db := NewDB(pool, slog.Default())
@@ -116,7 +125,7 @@ func TestWithAcquireClosedPool(t *testing.T) {
116125
// Opt out of automatic tearing down migration as we want to close the connection pool before t.Cleanup() is called.
117126
SkipTeardown: true,
118127

119-
Files: os.DirFS("../../migrations"),
128+
Files: migrations,
120129
})
121130
pool := migration.Setup(t.Context(), "")
122131
db := NewDB(pool, slog.Default())
@@ -130,7 +139,7 @@ func TestCreateProduct(t *testing.T) {
130139
t.Parallel()
131140
migration := sqltest.New(t, sqltest.Options{
132141
Force: *force,
133-
Files: os.DirFS("../../migrations"),
142+
Files: migrations,
134143
})
135144
pool := migration.Setup(t.Context(), "")
136145
db := NewDB(pool, slog.Default())
@@ -278,7 +287,7 @@ func TestUpdateProduct(t *testing.T) {
278287
t.Parallel()
279288
migration := sqltest.New(t, sqltest.Options{
280289
Force: *force,
281-
Files: os.DirFS("../../migrations"),
290+
Files: migrations,
282291
})
283292
pool := migration.Setup(t.Context(), "")
284293
db := NewDB(pool, slog.Default())
@@ -469,7 +478,7 @@ func TestGetProduct(t *testing.T) {
469478
t.Parallel()
470479
migration := sqltest.New(t, sqltest.Options{
471480
Force: *force,
472-
Files: os.DirFS("../../migrations"),
481+
Files: migrations,
473482
})
474483
pool := migration.Setup(t.Context(), "")
475484
db := NewDB(pool, slog.Default())
@@ -552,7 +561,7 @@ func TestSearchProducts(t *testing.T) {
552561
t.Parallel()
553562
migration := sqltest.New(t, sqltest.Options{
554563
Force: *force,
555-
Files: os.DirFS("../../migrations"),
564+
Files: migrations,
556565
})
557566
pool := migration.Setup(t.Context(), "")
558567
db := NewDB(pool, slog.Default())
@@ -771,7 +780,7 @@ func TestDeleteProduct(t *testing.T) {
771780
t.Parallel()
772781
migration := sqltest.New(t, sqltest.Options{
773782
Force: *force,
774-
Files: os.DirFS("../../migrations"),
783+
Files: migrations,
775784
})
776785
pool := migration.Setup(t.Context(), "")
777786
db := NewDB(pool, slog.Default())
@@ -872,7 +881,7 @@ func TestCreateProductReview(t *testing.T) {
872881
t.Parallel()
873882
migration := sqltest.New(t, sqltest.Options{
874883
Force: *force,
875-
Files: os.DirFS("../../migrations"),
884+
Files: migrations,
876885
})
877886
pool := migration.Setup(t.Context(), "")
878887
db := NewDB(pool, slog.Default())
@@ -1048,7 +1057,7 @@ func TestUpdateProductReview(t *testing.T) {
10481057
t.Parallel()
10491058
migration := sqltest.New(t, sqltest.Options{
10501059
Force: *force,
1051-
Files: os.DirFS("../../migrations"),
1060+
Files: migrations,
10521061
})
10531062
pool := migration.Setup(t.Context(), "")
10541063
db := NewDB(pool, slog.Default())
@@ -1306,7 +1315,7 @@ func TestGetProductReview(t *testing.T) {
13061315
t.Parallel()
13071316
migration := sqltest.New(t, sqltest.Options{
13081317
Force: *force,
1309-
Files: os.DirFS("../../migrations"),
1318+
Files: migrations,
13101319
})
13111320
pool := migration.Setup(t.Context(), "")
13121321
db := NewDB(pool, slog.Default())
@@ -1430,7 +1439,7 @@ func TestGetProductReviews(t *testing.T) {
14301439
t.Parallel()
14311440
migration := sqltest.New(t, sqltest.Options{
14321441
Force: *force,
1433-
Files: os.DirFS("../../migrations"),
1442+
Files: migrations,
14341443
})
14351444
pool := migration.Setup(t.Context(), "")
14361445
db := NewDB(pool, slog.Default())
@@ -1634,7 +1643,7 @@ func TestDeleteProductReview(t *testing.T) {
16341643
t.Parallel()
16351644
migration := sqltest.New(t, sqltest.Options{
16361645
Force: *force,
1637-
Files: os.DirFS("../../migrations"),
1646+
Files: migrations,
16381647
})
16391648
pool := migration.Setup(t.Context(), "")
16401649
db := NewDB(pool, slog.Default())

0 commit comments

Comments
 (0)