Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlmock panics when closing empty Rows object #337

Open
raxod502-plaid opened this issue May 7, 2024 · 0 comments
Open

sqlmock panics when closing empty Rows object #337

raxod502-plaid opened this issue May 7, 2024 · 0 comments

Comments

@raxod502-plaid
Copy link

Operating system and Go Version

macOS 14.4.1, Go 1.20

Issue

When using db.Query to perform a query that does not return any rows, sqlmock panics instead of performing a no-op as other database drivers do.

A workaround is to use db.Exec instead so that there is no rows object to close, but the sqlmock behavior is incorrect.

Reproduction steps

package main

import (
	"github.com/DATA-DOG/go-sqlmock"
)

func assert(err error) {
	if err != nil {
		panic(err.Error())
	}
}

func main() {
	db, mock, err := sqlmock.New()
	assert(err)
	mock.ExpectQuery("TRUNCATE table").WithoutArgs().WillReturnRows()
	rows, err := db.Query("TRUNCATE table")
	assert(err)
	err = rows.Close()
	assert(err)
}

Expected Result

No error (or, at worst, an error returned from rows.Close)

Actual Result

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/DATA-DOG/go-sqlmock.(*rowSets).Close(0x14000124040)
	/Users/rrosborough/.asdf/installs/golang/1.20/packages/pkg/mod/github.com/!d!a!t!a-!d!o!g/go-sqlmock@v1.5.2/rows.go:40 +0x64
database/sql.(*Rows).close.func1()
	/Users/rrosborough/.asdf/installs/golang/1.20/go/src/database/sql/sql.go:3287 +0x34
database/sql.withLock({0x100b84d50, 0x14000142000}, 0x14000149eb8)
	/Users/rrosborough/.asdf/installs/golang/1.20/go/src/database/sql/sql.go:3405 +0x7c
database/sql.(*Rows).close(0x14000134180, {0x0, 0x0})
	/Users/rrosborough/.asdf/installs/golang/1.20/go/src/database/sql/sql.go:3286 +0x130
database/sql.(*Rows).Close(0x140001360e0?)
	/Users/rrosborough/.asdf/installs/golang/1.20/go/src/database/sql/sql.go:3270 +0x24
main.main()
	/Users/rrosborough/temp/sqlmockexample/main.go:19 +0x12c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant