@@ -2,7 +2,6 @@ package postgres
22
33import (
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.
2219var migrations fs.FS
2320
@@ -37,12 +34,7 @@ func TestMain(m *testing.M) {
3734func 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) {
5850func 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
9481func 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) {
120102func 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
138120func 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
286263func 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
477449func 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
560527func 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
779741func 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
880838func 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
10561009func 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
13141262func 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
14381381func 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
16421580func 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