Skip to content

Commit b91299b

Browse files
committed
testing: use sqltest.Quick function and remove -force test flag
1 parent 865c8ea commit b91299b

3 files changed

Lines changed: 15 additions & 84 deletions

File tree

internal/inventory/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func serviceWithPostgres(t *testing.T) *inventory.Service {
5454
// if the INTEGRATION_TESTDB environment variable is set to true.
5555
if os.Getenv("INTEGRATION_TESTDB") == "true" {
5656
migration := sqltest.New(t, sqltest.Options{
57-
Force: *force,
57+
Force: true,
5858
TemporaryDatabasePrefix: "test_inventory_pkg", // Avoid a clash between database names of packages on parallel execution.
5959
Files: os.DirFS("../../migrations"),
6060
})

internal/inventory/inventory_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package inventory_test
33
import (
44
"context"
55
"errors"
6-
"flag"
76
"log"
87
"os"
98
"testing"
@@ -15,8 +14,6 @@ import (
1514
"go.uber.org/mock/gomock"
1615
)
1716

18-
var force = flag.Bool("force", false, "Force cleaning the database before starting")
19-
2017
func TestMain(m *testing.M) {
2118
if os.Getenv("INTEGRATION_TESTDB") != "true" {
2219
log.Printf("Skipping tests that require database connection")

internal/postgres/postgres_test.go

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package postgres
22

33
import (
44
"context"
5-
"flag"
65
"io/fs"
76
"log"
87
"log/slog"
@@ -16,8 +15,6 @@ import (
1615
"github.com/henvic/pgxtutorial/internal/inventory"
1716
)
1817

19-
var force = flag.Bool("force", false, "Force cleaning the database before starting")
20-
2118
// migrations for testing the database.
2219
var migrations fs.FS
2320

@@ -37,12 +34,7 @@ func TestMain(m *testing.M) {
3734
func TestTransactionContext(t *testing.T) {
3835
t.Parallel()
3936

40-
migration := sqltest.New(t, sqltest.Options{
41-
Force: *force,
42-
Files: migrations,
43-
})
44-
pool := migration.Setup(t.Context(), "")
45-
db := NewDB(pool, slog.Default())
37+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
4638

4739
ctx, err := db.TransactionContext(t.Context())
4840
if err != nil {
@@ -58,12 +50,7 @@ func TestTransactionContext(t *testing.T) {
5850
func TestTransactionContextCanceled(t *testing.T) {
5951
t.Parallel()
6052

61-
migration := sqltest.New(t, sqltest.Options{
62-
Force: *force,
63-
Files: migrations,
64-
})
65-
pool := migration.Setup(t.Context(), "")
66-
db := NewDB(pool, slog.Default())
53+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
6754

6855
canceledCtx, immediateCancel := context.WithCancel(t.Context())
6956
immediateCancel()
@@ -93,12 +80,7 @@ func TestRollbackNoTransaction(t *testing.T) {
9380

9481
func TestWithAcquire(t *testing.T) {
9582
t.Parallel()
96-
migration := sqltest.New(t, sqltest.Options{
97-
Force: *force,
98-
Files: migrations,
99-
})
100-
pool := migration.Setup(t.Context(), "")
101-
db := NewDB(pool, slog.Default())
83+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
10284

10385
// Reuse the same connection for executing SQL commands.
10486
dbCtx, err := db.WithAcquire(t.Context())
@@ -120,7 +102,7 @@ func TestWithAcquire(t *testing.T) {
120102
func TestWithAcquireClosedPool(t *testing.T) {
121103
t.Parallel()
122104
migration := sqltest.New(t, sqltest.Options{
123-
Force: *force,
105+
Force: true,
124106

125107
// Opt out of automatic tearing down migration as we want to close the connection pool before t.Cleanup() is called.
126108
SkipTeardown: true,
@@ -137,12 +119,7 @@ func TestWithAcquireClosedPool(t *testing.T) {
137119

138120
func TestCreateProduct(t *testing.T) {
139121
t.Parallel()
140-
migration := sqltest.New(t, sqltest.Options{
141-
Force: *force,
142-
Files: migrations,
143-
})
144-
pool := migration.Setup(t.Context(), "")
145-
db := NewDB(pool, slog.Default())
122+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
146123

147124
type args struct {
148125
ctx context.Context
@@ -285,12 +262,7 @@ func TestCreateProduct(t *testing.T) {
285262

286263
func TestUpdateProduct(t *testing.T) {
287264
t.Parallel()
288-
migration := sqltest.New(t, sqltest.Options{
289-
Force: *force,
290-
Files: migrations,
291-
})
292-
pool := migration.Setup(t.Context(), "")
293-
db := NewDB(pool, slog.Default())
265+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
294266

295267
// Add some products that will be modified next:
296268
createProducts(t, db, []inventory.CreateProductParams{
@@ -476,12 +448,7 @@ func TestUpdateProduct(t *testing.T) {
476448

477449
func TestGetProduct(t *testing.T) {
478450
t.Parallel()
479-
migration := sqltest.New(t, sqltest.Options{
480-
Force: *force,
481-
Files: migrations,
482-
})
483-
pool := migration.Setup(t.Context(), "")
484-
db := NewDB(pool, slog.Default())
451+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
485452

486453
createProducts(t, db, []inventory.CreateProductParams{
487454
{
@@ -559,12 +526,7 @@ func TestGetProduct(t *testing.T) {
559526

560527
func TestSearchProducts(t *testing.T) {
561528
t.Parallel()
562-
migration := sqltest.New(t, sqltest.Options{
563-
Force: *force,
564-
Files: migrations,
565-
})
566-
pool := migration.Setup(t.Context(), "")
567-
db := NewDB(pool, slog.Default())
529+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
568530

569531
// On this test, reuse the same connection for executing SQL commands
570532
// to check acquiring and releasing a connection passed via context is working as expected.
@@ -778,11 +740,7 @@ func TestSearchProducts(t *testing.T) {
778740

779741
func TestDeleteProduct(t *testing.T) {
780742
t.Parallel()
781-
migration := sqltest.New(t, sqltest.Options{
782-
Force: *force,
783-
Files: migrations,
784-
})
785-
pool := migration.Setup(t.Context(), "")
743+
pool := sqltest.Quick(t, migrations)
786744
db := NewDB(pool, slog.Default())
787745

788746
createProducts(t, db, []inventory.CreateProductParams{
@@ -879,12 +837,7 @@ func TestDeleteProduct(t *testing.T) {
879837

880838
func TestCreateProductReview(t *testing.T) {
881839
t.Parallel()
882-
migration := sqltest.New(t, sqltest.Options{
883-
Force: *force,
884-
Files: migrations,
885-
})
886-
pool := migration.Setup(t.Context(), "")
887-
db := NewDB(pool, slog.Default())
840+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
888841

889842
createProducts(t, db, []inventory.CreateProductParams{
890843
{
@@ -1055,12 +1008,7 @@ func TestCreateProductReview(t *testing.T) {
10551008

10561009
func TestUpdateProductReview(t *testing.T) {
10571010
t.Parallel()
1058-
migration := sqltest.New(t, sqltest.Options{
1059-
Force: *force,
1060-
Files: migrations,
1061-
})
1062-
pool := migration.Setup(t.Context(), "")
1063-
db := NewDB(pool, slog.Default())
1011+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
10641012

10651013
// Add some products that will be modified next:
10661014
createProducts(t, db, []inventory.CreateProductParams{
@@ -1313,12 +1261,7 @@ func TestUpdateProductReview(t *testing.T) {
13131261

13141262
func TestGetProductReview(t *testing.T) {
13151263
t.Parallel()
1316-
migration := sqltest.New(t, sqltest.Options{
1317-
Force: *force,
1318-
Files: migrations,
1319-
})
1320-
pool := migration.Setup(t.Context(), "")
1321-
db := NewDB(pool, slog.Default())
1264+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
13221265

13231266
createProducts(t, db, []inventory.CreateProductParams{
13241267
{
@@ -1437,12 +1380,7 @@ func TestGetProductReview(t *testing.T) {
14371380

14381381
func TestGetProductReviews(t *testing.T) {
14391382
t.Parallel()
1440-
migration := sqltest.New(t, sqltest.Options{
1441-
Force: *force,
1442-
Files: migrations,
1443-
})
1444-
pool := migration.Setup(t.Context(), "")
1445-
db := NewDB(pool, slog.Default())
1383+
db := NewDB(sqltest.Quick(t, migrations), slog.Default())
14461384

14471385
createProducts(t, db, []inventory.CreateProductParams{
14481386
{
@@ -1641,11 +1579,7 @@ func TestGetProductReviews(t *testing.T) {
16411579

16421580
func TestDeleteProductReview(t *testing.T) {
16431581
t.Parallel()
1644-
migration := sqltest.New(t, sqltest.Options{
1645-
Force: *force,
1646-
Files: migrations,
1647-
})
1648-
pool := migration.Setup(t.Context(), "")
1582+
pool := sqltest.Quick(t, migrations)
16491583
db := NewDB(pool, slog.Default())
16501584

16511585
createProducts(t, db, []inventory.CreateProductParams{

0 commit comments

Comments
 (0)