pashagolub / pgxmock

pgx mock driver for golang to test database interactions
Other
386 stars 49 forks source link

When you close row, the test will panic #190

Closed MeelyNe closed 8 months ago

MeelyNe commented 8 months ago

Describe the bug pgxmock.Rows returns panic if business code closes it

To Reproduce


// type of replicatePool is pgxpool.Pool{}

rows, err := i.replicaPool.Query(ctx, query, id)
if err != nil {
    return nil, fmt.Errorf("error on query: %w", err)
}
defer rows.Close() // here fails

on test:

rows := pgxmock.NewRows([]string{"id"}).
        AddRow(1).
        AddRow(2)

rows.CloseError(nil)

replicaPool.EXPECT().Query(ctx, gomock.Any(), id).Return(rows.Kind(), nil)

Expected behavior not panic )

Version github.com/jackc/pgx/v5 v5.5.2 github.com/pashagolub/pgxmock/v3 v3.3.0

Console Output

[PANICKED] Test Panicked In [It] at: /usr/local/go/src/runtime/panic.go:261 @ 02/14/24 16:39:15.544

runtime error: invalid memory address or nil pointer dereference

Full Stack Trace github.com/pashagolub/pgxmock/v3.(*rowSets).Close(0xc000387d28?) /home/username/GolandProjects/myProject/vendor/github.com/pashagolub/pgxmock/v3/rows.go:53 +0x4

pashagolub commented 8 months ago

This is not pgxmock code. What is EXPECT()? What is gomock anyway?

MeelyNe commented 8 months ago

This is not pgxmock code. What is EXPECT()? What is gomock anyway?

I use github.com/uber-go/mock, cuz we use wrapper around default pgxpool.Pool as DB . But I was thinking of using use your library just for mock rows )

pashagolub commented 8 months ago

No, sorry. That doesn't work that way )

MeelyNe commented 8 months ago

OK :( I'm sorry, can you recommend an alternative? (

pashagolub commented 8 months ago

Well, you should use uber-go/mock for everything. Usually mock libs are packed with all tools needed