mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-19 20:57:50 +02:00
add AddRows function to allow adding multiple rows
This commit is contained in:
parent
35c0d3c10b
commit
2df2d8867c
10
rows.go
10
rows.go
@ -188,6 +188,16 @@ func (r *Rows) AddRow(values ...driver.Value) *Rows {
|
|||||||
return r
|
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.
|
// FromCSVString build rows from csv string.
|
||||||
// return the same instance to perform subsequent actions.
|
// return the same instance to perform subsequent actions.
|
||||||
// Note that the number of values must match the number
|
// Note that the number of values must match the number
|
||||||
|
83
rows_test.go
83
rows_test.go
@ -3,6 +3,7 @@ package sqlmock
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -670,3 +671,85 @@ func queryRowBytesNotInvalidatedByClose(t *testing.T, rows *Rows, scan func(*sql
|
|||||||
t.Fatal(err)
|
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 ExampleMultiRows() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user