Skip to content

Commit

Permalink
Merge remote-tracking branch 'hebo/typed_rows'
Browse files Browse the repository at this point in the history
* hebo/typed_rows:
  Allow for multiple rows by manually creating each row
  Create a single row from an interface list - #1
  • Loading branch information
l3pp4rd committed Feb 13, 2014
2 parents c33b881 + 517a41f commit 008d884
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package sqlmock

import (
"database/sql/driver"
"database/sql/driver"
)

// Result satisfies sql driver Result, which
// holds last insert id and rows affected
// by Exec queries
type result struct {
insertID int64
insertID int64
rowsAffected int64
}

Expand Down
21 changes: 21 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ func (r *rows) Next(dest []driver.Value) error {
return nil
}

func (r *rows) AddRow(values ...interface{}) {
if len(values) != len(r.cols) {
panic("Expected number of values to match number of columns")
}

row := make([]driver.Value, len(r.cols))
for i, v := range values {
row[i] = v
}

r.rows = append(r.rows, row)
}

// NewRows allows Rows to be created manually to use
// any of the types sql/driver.Value supports
func NewRows(columns []string) *rows {
rs := &rows{}
rs.cols = columns
return rs
}

// RowsFromCSVString creates Rows from CSV string
// to be used for mocked queries. Returns sql driver Rows interface
func RowsFromCSVString(columns []string, s string) driver.Rows {
Expand Down
52 changes: 52 additions & 0 deletions sqlmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"testing"
"time"
)

func TestMockQuery(t *testing.T) {
Expand Down Expand Up @@ -48,6 +49,57 @@ func TestMockQuery(t *testing.T) {
}
}

func TestMockQueryTypes(t *testing.T) {
db, err := sql.Open("mock", "")
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}

columns := []string{"id", "timestamp", "sold"}

timestamp := time.Now()
rs := NewRows(columns)
rs.AddRow(5, timestamp, true)

ExpectQuery("SELECT (.+) FROM sales WHERE id = ?").
WithArgs(5).
WillReturnRows(rs)

rows, err := db.Query("SELECT (.+) FROM sales WHERE id = ?", 5)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
}
defer rows.Close()
if !rows.Next() {
t.Error("it must have had one row as result, but got empty result set instead")
}

var id int
var time time.Time
var sold bool

err = rows.Scan(&id, &time, &sold)
if err != nil {
t.Errorf("error '%s' was not expected while trying to scan row", err)
}

if id != 5 {
t.Errorf("expected mocked id to be 5, but got %d instead", id)
}

if time != timestamp {
t.Errorf("expected mocked time to be %s, but got '%s' instead", timestamp, time)
}

if sold != true {
t.Errorf("expected mocked boolean to be true, but got %v instead", sold)
}

if err = db.Close(); err != nil {
t.Errorf("error '%s' was not expected while closing the database", err)
}
}

func TestTransactionExpectations(t *testing.T) {
db, err := sql.Open("mock", "")
if err != nil {
Expand Down

0 comments on commit 008d884

Please sign in to comment.