Skip to content

Create multi row Rows instance in one line #135

@stoewer

Description

@stoewer

When using sqlmock I find myself often in situations where I want to create a sqlmock.Rows with multiple rows in a just single line of code. In larger tests my test data is often organized like this:

var (
    testColumns []string
    testValues [][]driver.Values
)

In the actual test the data is usually used like this:

testRows := sqlmock.NewRows(testColumns)
for _, values := range testValues {
    testRows.AddRow(values...)
}
mock.ExpectQuery(query).
    WillReturnRows(testRows)

Although this works perfectly fine, it is still a bit of an overhead that decreases readability in larger tests where multiple queries are mocked. I therefore suggest to add a method AddRows(rows ...[]driver.Value) to the sqlmock.Rows type. The method could be use like this:

mock.ExpectQuery(query).
    WillReturnRows(sqlmock.NewRows(testColumns).AddRows(testValues...))

If this feature was accepted, I'd be more than happy to make a PR that adds this little helper method.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions