Skip to content

Commit 64ed29d

Browse files
authored
Merge pull request #121 from pashagolub/120-expectedcopyfromstring-outputs-table-name-instead-of-columns-names
[-] fix `ExpectedCopyFrom.String()` output, resolves #120
2 parents 106a759 + 8485c59 commit 64ed29d

File tree

4 files changed

+74
-56
lines changed

4 files changed

+74
-56
lines changed

expectations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func (e *ExpectedCopyFrom) WillDelayFor(duration time.Duration) *ExpectedCopyFro
438438
func (e *ExpectedCopyFrom) String() string {
439439
msg := "ExpectedCopyFrom => expecting CopyFrom which:"
440440
msg += "\n - matches table name: '" + e.expectedTableName + "'"
441-
msg += fmt.Sprintf("\n - matches column names: '%+v'", e.expectedTableName)
441+
msg += fmt.Sprintf("\n - matches column names: '%+v'", e.expectedColumns)
442442

443443
if e.err != nil {
444444
msg += fmt.Sprintf("\n - should return error: %s", e.err)

expectations_test.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package pgxmock
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"testing"
9+
"time"
10+
11+
"github.com/jackc/pgx/v5"
812
)
913

1014
func ExampleExpectedExec() {
@@ -25,6 +29,45 @@ func TestUnmonitoredPing(t *testing.T) {
2529
}
2630
}
2731

32+
func TestUnexpectedPing(t *testing.T) {
33+
mock, _ := NewConn(MonitorPingsOption(true))
34+
err := mock.Ping(context.Background())
35+
if err == nil {
36+
t.Error("Ping should return error for unexpected call")
37+
}
38+
mock.ExpectExec("foo")
39+
err = mock.Ping(context.Background())
40+
if err == nil {
41+
t.Error("Ping should return error for unexpected call")
42+
}
43+
}
44+
45+
func TestUnexpectedPrepare(t *testing.T) {
46+
mock, _ := NewConn()
47+
_, err := mock.Prepare(context.Background(), "foo", "bar")
48+
if err == nil {
49+
t.Error("Prepare should return error for unexpected call")
50+
}
51+
mock.ExpectExec("foo")
52+
_, err = mock.Prepare(context.Background(), "foo", "bar")
53+
if err == nil {
54+
t.Error("Prepare should return error for unexpected call")
55+
}
56+
}
57+
58+
func TestUnexpectedCopyFrom(t *testing.T) {
59+
mock, _ := NewConn()
60+
_, err := mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
61+
if err == nil {
62+
t.Error("CopyFrom should return error for unexpected call")
63+
}
64+
mock.ExpectExec("foo")
65+
_, err = mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
66+
if err == nil {
67+
t.Error("CopyFrom should return error for unexpected call")
68+
}
69+
}
70+
2871
func TestBuildQuery(t *testing.T) {
2972
mock, _ := NewConn(MonitorPingsOption(true))
3073
query := `
@@ -41,7 +84,7 @@ func TestBuildQuery(t *testing.T) {
4184
4285
`
4386

44-
mock.ExpectPing()
87+
mock.ExpectPing().WillDelayFor(1 * time.Second).WillReturnError(errors.New("no ping please"))
4588
mock.ExpectQuery(query)
4689
mock.ExpectExec(query)
4790
mock.ExpectPrepare("foo", query)

pgxmock.go

+5-34
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ type pgxMockIface interface {
5050
// the *ExpectedExec allows to mock database response
5151
ExpectExec(expectedSQL string) *ExpectedExec
5252

53-
// ExpectBegin expects *sql.DB.Begin to be called.
53+
// ExpectBegin expects pgx.Conn.Begin to be called.
5454
// the *ExpectedBegin allows to mock database response
5555
ExpectBegin() *ExpectedBegin
5656

57-
// ExpectBeginTx expects expects BeginTxFunc() to be called with expectedSQL
57+
// ExpectBeginTx expects expects BeginTx() to be called with expectedSQL
5858
// query. The *ExpectedBegin allows to mock database response.
5959
ExpectBeginTx(txOptions pgx.TxOptions) *ExpectedBegin
6060

61-
// ExpectCommit expects *sql.Tx.Commit to be called.
61+
// ExpectCommit expects pgx.Tx.Commit to be called.
6262
// the *ExpectedCommit allows to mock database response
6363
ExpectCommit() *ExpectedCommit
6464

65-
// ExpectRollback expects *sql.Tx.Rollback to be called.
65+
// ExpectRollback expects pgx.Tx.Rollback to be called.
6666
// the *ExpectedRollback allows to mock database response
6767
ExpectRollback() *ExpectedRollback
6868

69-
// ExpectPing expected *sql.DB.Ping to be called.
69+
// ExpectPing expected pgx.Conn.Ping to be called.
7070
// the *ExpectedPing allows to mock database response
7171
//
7272
// Ping support only exists in the SQL library in Go 1.8 and above.
@@ -421,35 +421,6 @@ func (c *pgxmock) LargeObjects() pgx.LargeObjects {
421421
return pgx.LargeObjects{}
422422
}
423423

424-
func (c *pgxmock) BeginFunc(ctx context.Context, f func(pgx.Tx) error) (err error) {
425-
return c.BeginTxFunc(ctx, pgx.TxOptions{}, f)
426-
}
427-
428-
func (c *pgxmock) BeginTxFunc(ctx context.Context, txOptions pgx.TxOptions, f func(pgx.Tx) error) (err error) {
429-
var tx pgx.Tx
430-
tx, err = c.BeginTx(ctx, txOptions)
431-
if err != nil {
432-
return err
433-
}
434-
defer func() {
435-
if err == nil {
436-
return
437-
}
438-
rollbackErr := tx.Rollback(ctx)
439-
if !(rollbackErr == nil || errors.Is(rollbackErr, pgx.ErrTxClosed)) {
440-
err = rollbackErr
441-
}
442-
}()
443-
444-
fErr := f(tx)
445-
if fErr != nil {
446-
_ = tx.Rollback(ctx) // ignore rollback error as there is already an error to return
447-
return fErr
448-
}
449-
450-
return tx.Commit(ctx)
451-
}
452-
453424
func (c *pgxmock) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
454425
ex, err := c.begin(txOptions)
455426
if ex != nil {

pgxmock_test.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestMockCopyFrom(t *testing.T) {
116116
defer mock.Close(context.Background())
117117

118118
mock.ExpectCopyFrom(`"fooschema"."baztable"`, []string{"col1"}).
119-
WillReturnResult(2)
119+
WillReturnResult(2).WillDelayFor(1 * time.Second)
120120

121121
_, err = mock.CopyFrom(context.Background(), pgx.Identifier{"error", "error"}, []string{"error"}, nil)
122122
if err == nil {
@@ -135,6 +135,14 @@ func TestMockCopyFrom(t *testing.T) {
135135
t.Errorf("expected RowsAffected to be 2, but got %d instead", rows)
136136
}
137137

138+
mock.ExpectCopyFrom(`"fooschema"."baztable"`, []string{"col1"}).
139+
WillReturnError(errors.New("error is here"))
140+
141+
_, err = mock.CopyFrom(context.Background(), pgx.Identifier{"fooschema", "baztable"}, []string{"col1"}, nil)
142+
if err == nil {
143+
t.Error("error is expected while executing CopyFrom")
144+
}
145+
138146
if err := mock.ExpectationsWereMet(); err != nil {
139147
t.Errorf("there were unfulfilled expectations: %s", err)
140148
}
@@ -229,24 +237,6 @@ func TestTransactionExpectations(t *testing.T) {
229237
t.Errorf("an error '%s' was not expected when committing a transaction", err)
230238
}
231239

232-
// // beginTxFunc and commit
233-
// mock.ExpectBeginTx(pgx.TxOptions{})
234-
// mock.ExpectCommit()
235-
236-
// err = mock.BeginFunc(context.Background(), func(tx pgx.Tx) error { return nil })
237-
// if err != nil {
238-
// t.Errorf("an error '%s' was not expected when beginning a transaction", err)
239-
// }
240-
241-
// // beginTxFunc and rollback
242-
// mock.ExpectBeginTx(pgx.TxOptions{})
243-
// mock.ExpectRollback()
244-
245-
// err = mock.BeginFunc(context.Background(), func(tx pgx.Tx) error { return errors.New("smth wrong") })
246-
// if err == nil {
247-
// t.Error("an error was expected whithin a transaction, but got none")
248-
// }
249-
250240
// begin and rollback
251241
mock.ExpectBegin()
252242
mock.ExpectRollback()
@@ -282,7 +272,9 @@ func TestPrepareExpectations(t *testing.T) {
282272
}
283273
defer mock.Close(context.Background())
284274

285-
mock.ExpectPrepare("foo", "SELECT (.+) FROM articles WHERE id = ?")
275+
mock.ExpectPrepare("foo", "SELECT (.+) FROM articles WHERE id = ?").
276+
WillDelayFor(1 * time.Second).
277+
WillReturnCloseError(errors.New("invaders must die"))
286278

287279
stmt, err := mock.Prepare(context.Background(), "foo", "SELECT (.+) FROM articles WHERE id = $1")
288280
if err != nil {
@@ -1250,3 +1242,15 @@ func TestPgConn(t *testing.T) {
12501242

12511243
_ = mock.PgConn()
12521244
}
1245+
1246+
func TestNewRowsWithColumnDefinition(t *testing.T) {
1247+
mock, err := NewConn()
1248+
if err != nil {
1249+
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
1250+
}
1251+
defer mock.Close(context.Background())
1252+
r := mock.NewRowsWithColumnDefinition(*mock.NewColumn("foo"))
1253+
if len(r.defs) != 1 {
1254+
t.Error("NewRows failed")
1255+
}
1256+
}

0 commit comments

Comments
 (0)