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

Modify: existing panic in AddRow to give a hint to the issue #326

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (r *Rows) RowError(row int, err error) *Rows {
// of columns
func (r *Rows) AddRow(values ...driver.Value) *Rows {
if len(values) != len(r.cols) {
panic("Expected number of values to match number of columns")
panic(fmt.Sprintf("Expected number of values to match number of columns: expected %d, actual %d", len(values), len(r.cols)))
}

row := make([]driver.Value, len(r.cols))
Expand Down
25 changes: 25 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,31 @@ func TestAddRows(t *testing.T) {
// scanned id: 4 and title: Emily
}

func TestAddRowExpectPanic(t *testing.T) {
t.Parallel()

const expectedPanic = "Expected number of values to match number of columns: expected 1, actual 2"
values := []driver.Value{
"John",
"Jane",
}

defer func() {
if r := recover(); r != nil {
if r != expectedPanic {
t.Fatalf("panic message did not match expected: expected '%s', actual '%s'", r, expectedPanic)
}

return
}
t.Fatalf("expected panic: %s", expectedPanic)
}()

rows := NewRows([]string{"id", "name"})
// Note missing spread "..."
rows.AddRow(values)
}

func ExampleRows_AddRows() {
db, mock, err := New()
if err != nil {
Expand Down