diff --git a/go.mod b/go.mod index eaf8a5a..6f58b70 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,5 @@ module github.com/DATA-DOG/go-sqlmock + +go 1.15 + +require github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a21f637 --- /dev/null +++ b/go.sum @@ -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= diff --git a/rows.go b/rows.go index ccc5f0c..941544b 100644 --- a/rows.go +++ b/rows.go @@ -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 diff --git a/rows_test.go b/rows_test.go index 15cdbee..ef17521 100644 --- a/rows_test.go +++ b/rows_test.go @@ -3,6 +3,7 @@ package sqlmock import ( "bytes" "database/sql" + "database/sql/driver" "fmt" "testing" ) @@ -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 +}