Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Multi Row Support #263

Merged
merged 4 commits into from
Oct 30, 2021
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
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module github.com/DATA-DOG/go-sqlmock

go 1.15

require github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46 h1:veS9QfglfvqAw2e+eeNT/SbGySq8ajECXJ9e4fPoLhY=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
10 changes: 10 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ func (r *Rows) AddRow(values ...driver.Value) *Rows {
return r
}

// AddRows adds multiple rows composed from database driver.Value slice and
// returns the same instance to perform subsequent actions.
func (r *Rows) AddRows(values ...[]driver.Value) *Rows {
for _, value := range values {
r.AddRow(value...)
}

return r
}

// FromCSVString build rows from csv string.
// return the same instance to perform subsequent actions.
// Note that the number of values must match the number
Expand Down
83 changes: 83 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlmock
import (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
"testing"
)
Expand Down Expand Up @@ -670,3 +671,85 @@ func queryRowBytesNotInvalidatedByClose(t *testing.T, rows *Rows, scan func(*sql
t.Fatal(err)
}
}

func TestAddRows(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

values := [][]driver.Value{
{
1, "John",
},
{
2, "Jane",
},
{
3, "Peter",
},
{
4, "Emily",
},
}

rows := NewRows([]string{"id", "name"}).AddRows(values...)
mock.ExpectQuery("SELECT").WillReturnRows(rows).RowsWillBeClosed()

rs, _ := db.Query("SELECT")
defer rs.Close()

for rs.Next() {
var id int
var name string
rs.Scan(&id, &name)
fmt.Println("scanned id:", id, "and name:", name)
}

if rs.Err() != nil {
fmt.Println("got rows error:", rs.Err())
}
// Output: scanned id: 1 and title: John
// scanned id: 2 and title: Jane
// scanned id: 3 and title: Peter
// scanned id: 4 and title: Emily
}

func ExampleRows_AddRows() {
db, mock, err := New()
if err != nil {
fmt.Println("failed to open sqlmock database:", err)
}
defer db.Close()

values := [][]driver.Value{
{
1, "one",
},
{
2, "two",
},
}

rows := NewRows([]string{"id", "title"}).AddRows(values...)

mock.ExpectQuery("SELECT").WillReturnRows(rows)

rs, _ := db.Query("SELECT")
defer rs.Close()

for rs.Next() {
var id int
var title string
rs.Scan(&id, &title)
fmt.Println("scanned id:", id, "and title:", title)
}

if rs.Err() != nil {
fmt.Println("got rows error:", rs.Err())
}
// Output: scanned id: 1 and title: one
// scanned id: 2 and title: two
}