@@ -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
1616var (
1717 mockPool pgxmock.PgxPoolIface
1818 mockConn pgxmock.PgxConnIface
19+ ctx = context .Background ()
1920)
2021
2122func 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
3738func 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
5755func 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) {
9087func 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) {
115111func 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) {
142138func 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
184180func 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
206201func 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
228222func 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" ))
0 commit comments