Skip to content

Commit 03aed87

Browse files
authored
[+] bump pashagolub/pgxmock to v3.0, closes #605 (#606)
[+] bump `pashagolub/pgxmock` to v3.0, closes #605
1 parent 0855f47 commit 03aed87

13 files changed

Lines changed: 57 additions & 80 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/jackc/pgx/v5 v5.4.3
88
github.com/jessevdk/go-flags v1.5.0
99
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc
10-
github.com/pashagolub/pgxmock/v2 v2.12.0
10+
github.com/pashagolub/pgxmock/v3 v3.0.0
1111
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
1212
github.com/sethvargo/go-retry v0.2.4
1313
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
154154
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
155155
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc h1:BU12v9x5hvONtYU2R2LnlkxmWSsjzco046NzJLcWMHg=
156156
github.com/ory/mail/v3 v3.0.1-0.20210418065910-7f033ddea8dc/go.mod h1:vAPEMm1zIQKGmM9hcZTSlOU/CDVCXHGOw6SFxPlSoHw=
157-
github.com/pashagolub/pgxmock/v2 v2.12.0 h1:IVRmQtVFNCoq7NOZ+PdfvB6fwnLJmEuWDhnc3yrDxBs=
158-
github.com/pashagolub/pgxmock/v2 v2.12.0/go.mod h1:D3YslkN/nJ4+umVqWmbwfSXugJIjPMChkGBG47OJpNw=
157+
github.com/pashagolub/pgxmock/v3 v3.0.0 h1:wJBQ9FkL9Q95jht0jR92jKF+uPY2YUXx2fDaFvB2fOg=
158+
github.com/pashagolub/pgxmock/v3 v3.0.0/go.mod h1:pCNliy92lIbLQL7m5GXlkMa5QtZgrZR2Ak55mmCbxqw=
159159
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
160160
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
161161
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -168,8 +168,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
168168
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
169169
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
170170
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
171-
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
172-
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
171+
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
172+
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
173173
github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9cJvm4SvQ=
174174
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
175175
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=

internal/api/api.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ func Init(opts config.RestAPIOpts, logger log.LoggerIface) *RestAPIServer {
3434
MaxHeaderBytes: 1 << 20,
3535
},
3636
}
37-
http.HandleFunc("/liveness", func(w http.ResponseWriter, r *http.Request) {
38-
w.WriteHeader(http.StatusOK) // i'm serving hence I'm alive
39-
})
37+
http.HandleFunc("/liveness", s.livenessHandler)
4038
http.HandleFunc("/readiness", s.readinessHandler)
4139
http.HandleFunc("/startchain", s.chainHandler)
4240
http.HandleFunc("/stopchain", s.chainHandler)
@@ -47,12 +45,16 @@ func Init(opts config.RestAPIOpts, logger log.LoggerIface) *RestAPIServer {
4745
return s
4846
}
4947

50-
func (Server *RestAPIServer) readinessHandler(w http.ResponseWriter, r *http.Request) {
48+
func (Server *RestAPIServer) livenessHandler(w http.ResponseWriter, _ *http.Request) {
49+
Server.l.Debug("Received /liveness REST API request")
50+
w.WriteHeader(http.StatusOK) // i'm serving hence I'm alive
51+
}
52+
53+
func (Server *RestAPIServer) readinessHandler(w http.ResponseWriter, _ *http.Request) {
5154
Server.l.Debug("Received /readiness REST API request")
5255
if Server.APIHandler == nil || !Server.APIHandler.IsReady() {
5356
w.WriteHeader(http.StatusServiceUnavailable)
5457
return
5558
}
5659
w.WriteHeader(http.StatusOK)
57-
r.Context()
5860
}

internal/api/api_test.go

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package api_test
1+
package api
22

33
import (
44
"context"
55
"errors"
6-
"io"
76
"net/http"
7+
"net/url"
88
"testing"
99

10-
"github.com/cybertec-postgresql/pg_timetable/internal/api"
1110
"github.com/cybertec-postgresql/pg_timetable/internal/config"
1211
"github.com/cybertec-postgresql/pg_timetable/internal/log"
1312
"github.com/stretchr/testify/assert"
@@ -31,47 +30,30 @@ func (r *apihandler) StopChain(context.Context, int) error {
3130
return nil
3231
}
3332

34-
var restsrv *api.RestAPIServer
33+
var restsrv = Init(config.RestAPIOpts{Port: 8080}, log.Init(config.LoggingOpts{LogLevel: "error"}))
3534

36-
func init() {
37-
restsrv = api.Init(config.RestAPIOpts{Port: 8080}, log.Init(config.LoggingOpts{LogLevel: "error"}))
38-
}
35+
const turl = "http://localhost:8080/"
3936

4037
func TestStatus(t *testing.T) {
41-
42-
r, err := http.Get("http://localhost:8080/liveness")
43-
assert.NoError(t, err)
44-
assert.Equal(t, http.StatusOK, r.StatusCode)
45-
46-
r, err = http.Get("http://localhost:8080/readiness")
47-
assert.NoError(t, err)
48-
assert.Equal(t, http.StatusServiceUnavailable, r.StatusCode)
38+
assert.HTTPSuccess(t, restsrv.livenessHandler, "GET", turl+"liveness", nil)
39+
assert.HTTPStatusCode(t, restsrv.readinessHandler, "GET", turl+"readiness", nil, http.StatusServiceUnavailable)
4940

5041
restsrv.APIHandler = &apihandler{}
51-
r, err = http.Get("http://localhost:8080/readiness")
52-
assert.NoError(t, err)
53-
assert.Equal(t, http.StatusOK, r.StatusCode)
42+
assert.HTTPSuccess(t, restsrv.readinessHandler, "GET", turl+"readiness", nil)
5443
}
5544

5645
func TestChainManager(t *testing.T) {
5746
restsrv.APIHandler = &apihandler{}
58-
r, err := http.Get("http://localhost:8080/startchain")
59-
assert.NoError(t, err)
60-
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
61-
b, _ := io.ReadAll(r.Body)
62-
assert.Contains(t, string(b), "invalid syntax")
63-
64-
r, err = http.Get("http://localhost:8080/startchain?id=1")
65-
assert.NoError(t, err)
66-
assert.Equal(t, http.StatusOK, r.StatusCode)
67-
68-
r, err = http.Get("http://localhost:8080/stopchain?id=1")
69-
assert.NoError(t, err)
70-
assert.Equal(t, http.StatusOK, r.StatusCode)
71-
72-
r, err = http.Get("http://localhost:8080/startchain?id=0")
73-
assert.NoError(t, err)
74-
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
75-
b, _ = io.ReadAll(r.Body)
76-
assert.Contains(t, string(b), "invalid chain id")
47+
assert.HTTPStatusCode(t, restsrv.chainHandler, "GET", turl+"startchain", nil, http.StatusBadRequest)
48+
assert.HTTPBodyContains(t, restsrv.chainHandler, "GET", turl+"startchain", nil, "invalid syntax")
49+
50+
assert.HTTPSuccess(t, restsrv.chainHandler, "GET", turl+"startchain",
51+
url.Values{"id": []string{"1"}})
52+
assert.HTTPSuccess(t, restsrv.chainHandler, "GET", turl+"stopchain",
53+
url.Values{"id": []string{"1"}})
54+
55+
assert.HTTPError(t, restsrv.chainHandler, "GET", turl+"startchain",
56+
url.Values{"id": []string{"0"}})
57+
assert.HTTPBodyContains(t, restsrv.chainHandler, "GET", turl+"startchain",
58+
url.Values{"id": []string{"0"}}, "invalid chain id")
7759
}

internal/migrator/migrator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/cybertec-postgresql/pg_timetable/internal/migrator"
1414
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
1515
pgx "github.com/jackc/pgx/v5"
16-
"github.com/pashagolub/pgxmock/v2"
16+
"github.com/pashagolub/pgxmock/v3"
1717
"github.com/stretchr/testify/assert"
1818
)
1919

internal/pgengine/access_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77

88
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
9-
"github.com/pashagolub/pgxmock/v2"
9+
"github.com/pashagolub/pgxmock/v3"
1010
"github.com/stretchr/testify/assert"
1111
)
1212

internal/pgengine/bootstrap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
1212
pgx "github.com/jackc/pgx/v5"
13-
"github.com/pashagolub/pgxmock/v2"
13+
"github.com/pashagolub/pgxmock/v3"
1414
"github.com/stretchr/testify/assert"
1515
)
1616

internal/pgengine/log_hook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/pashagolub/pgxmock/v2"
9+
"github.com/pashagolub/pgxmock/v3"
1010
"github.com/sirupsen/logrus"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

1414
func TestLogHook(t *testing.T) {
1515
var h LogHook
16-
mockPool, err := pgxmock.NewPool(pgxmock.MonitorPingsOption(true))
16+
mockPool, err := pgxmock.NewPool()
1717
assert.NoError(t, err)
1818
func() { //fake NewHook
1919
h = LogHook{ctx: context.Background(),

internal/pgengine/transaction_test.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import (
99
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
1010
"github.com/jackc/pgx/v5/pgconn"
1111
"github.com/jackc/pgx/v5/pgtype"
12-
"github.com/pashagolub/pgxmock/v2"
12+
"github.com/pashagolub/pgxmock/v3"
1313
"github.com/stretchr/testify/assert"
1414
)
1515

1616
var (
1717
mockPool pgxmock.PgxPoolIface
1818
mockConn pgxmock.PgxConnIface
19+
ctx = context.Background()
1920
)
2021

2122
func initmockdb(t *testing.T) {
2223
var err error
23-
mockPool, err = pgxmock.NewPool(pgxmock.MonitorPingsOption(true))
24+
mockPool, err = pgxmock.NewPool()
2425
assert.NoError(t, err)
2526
mockConn, err = pgxmock.NewConn()
2627
assert.NoError(t, err)
@@ -36,28 +37,24 @@ func TestIsIntervalChainListed(t *testing.T) {
3637

3738
func TestStartTransaction(t *testing.T) {
3839
initmockdb(t)
39-
defer mockPool.Close()
40-
ctx := context.Background()
41-
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
4240

41+
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
4342
mockPool.ExpectBegin().WillReturnError(errors.New("foo"))
44-
tx, txid, err := pge.StartTransaction(ctx)
45-
assert.Nil(t, tx)
43+
_, txid, err := pge.StartTransaction(ctx)
4644
assert.Zero(t, txid)
4745
assert.Error(t, err)
4846

4947
mockPool.ExpectBegin()
5048
mockPool.ExpectQuery("SELECT txid_current()").WillReturnRows(pgxmock.NewRows([]string{"txid"}).AddRow(int64(42)))
51-
tx, txid, err = pge.StartTransaction(ctx)
49+
tx, txid, err := pge.StartTransaction(ctx)
5250
assert.NotNil(t, tx)
53-
assert.Equal(t, int64(42), txid)
51+
assert.EqualValues(t, 42, txid)
5452
assert.NoError(t, err)
5553
}
5654

5755
func TestMustTransaction(t *testing.T) {
5856
initmockdb(t)
59-
defer mockPool.Close()
60-
ctx := context.Background()
57+
6158
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
6259

6360
mockPool.ExpectBegin()
@@ -90,7 +87,6 @@ func TestMustTransaction(t *testing.T) {
9087
func TestExecuteSQLTask(t *testing.T) {
9188
initmockdb(t)
9289
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
93-
ctx := context.Background()
9490

9591
t.Run("Check autonomous SQL task", func(t *testing.T) {
9692
_, err := pge.ExecuteSQLTask(ctx, nil, &pgengine.ChainTask{Autonomous: true}, []string{})
@@ -115,7 +111,7 @@ func TestExecuteSQLTask(t *testing.T) {
115111
func TestExecLocalSQLTask(t *testing.T) {
116112
initmockdb(t)
117113
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
118-
ctx := context.Background()
114+
119115
mockPool.ExpectExec("SET ROLE").WillReturnResult(pgconn.NewCommandTag("SET"))
120116
mockPool.ExpectExec("SAVEPOINT task").WillReturnResult(pgconn.NewCommandTag("SAVEPOINT"))
121117
mockPool.ExpectExec("SELECT set_config").
@@ -142,7 +138,7 @@ func TestExecLocalSQLTask(t *testing.T) {
142138
func TestExecStandaloneTask(t *testing.T) {
143139
initmockdb(t)
144140
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
145-
ctx := context.Background()
141+
146142
mockPool.ExpectExec("SET ROLE").WillReturnResult(pgconn.NewCommandTag("SET"))
147143
mockPool.ExpectExec("SELECT set_config").
148144
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg()).
@@ -183,9 +179,8 @@ func TestExpectedCloseError(t *testing.T) {
183179

184180
func TestExecuteSQLCommand(t *testing.T) {
185181
initmockdb(t)
186-
defer mockPool.Close()
182+
187183
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
188-
ctx := context.Background()
189184

190185
_, err := pge.ExecuteSQLCommand(ctx, mockPool, "", []string{})
191186
assert.Error(t, err)
@@ -205,9 +200,8 @@ func TestExecuteSQLCommand(t *testing.T) {
205200

206201
func TestGetChainElements(t *testing.T) {
207202
initmockdb(t)
208-
defer mockPool.Close()
203+
209204
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
210-
ctx := context.Background()
211205

212206
mockPool.ExpectQuery("SELECT").WithArgs(0).WillReturnError(errors.New("error"))
213207
assert.Error(t, pge.GetChainElements(ctx, &[]pgengine.ChainTask{}, 0))
@@ -227,8 +221,7 @@ func TestGetChainElements(t *testing.T) {
227221

228222
func TestSetRole(t *testing.T) {
229223
initmockdb(t)
230-
defer mockPool.Close()
231-
ctx := context.Background()
224+
232225
pge := pgengine.NewDB(mockPool, "pgengine_unit_test")
233226
mockPool.ExpectBegin()
234227
mockPool.ExpectExec("SET ROLE").WillReturnError(errors.New("error"))

internal/scheduler/chain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/jackc/pgx/v5"
1414
"github.com/jackc/pgx/v5/pgconn"
1515
"github.com/jackc/pgx/v5/pgtype"
16-
"github.com/pashagolub/pgxmock/v2"
16+
"github.com/pashagolub/pgxmock/v3"
1717
"github.com/stretchr/testify/assert"
1818
)
1919

@@ -26,7 +26,7 @@ func TestSchedulerExclusiveLocking(*testing.T) {
2626
}
2727

2828
func TestAsyncChains(t *testing.T) {
29-
mock, err := pgxmock.NewPool(pgxmock.MonitorPingsOption(true))
29+
mock, err := pgxmock.NewPool()
3030
assert.NoError(t, err)
3131
pge := pgengine.NewDB(mock, "scheduler_unit_test")
3232
sch := New(pge, log.Init(config.LoggingOpts{LogLevel: "error"}))

0 commit comments

Comments
 (0)