forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows_go18_test.go
92 lines (71 loc) · 1.99 KB
/
rows_go18_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// +build go1.8
package sqlmock
import (
"fmt"
"testing"
)
func TestQueryMultiRows(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rs1 := NewRows([]string{"id", "title"}).AddRow(5, "hello world")
rs2 := NewRows([]string{"name"}).AddRow("gopher").AddRow("john").AddRow("jane").RowError(2, fmt.Errorf("error"))
mock.ExpectQuery("SELECT (.+) FROM articles WHERE id = \\?;SELECT name FROM users").
WithArgs(5).
WillReturnRows(rs1, rs2)
rows, err := db.Query("SELECT id, title FROM articles WHERE id = ?;SELECT name FROM users", 5)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
defer rows.Close()
if !rows.Next() {
t.Error("expected a row to be available in first result set")
}
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if id != 5 || name != "hello world" {
t.Errorf("unexpected row values id: %v name: %v", id, name)
}
if rows.Next() {
t.Error("was not expecting next row in first result set")
}
if !rows.NextResultSet() {
t.Error("had to have next result set")
}
if !rows.Next() {
t.Error("expected a row to be available in second result set")
}
err = rows.Scan(&name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if name != "gopher" {
t.Errorf("unexpected row name: %v", name)
}
if !rows.Next() {
t.Error("expected a row to be available in second result set")
}
err = rows.Scan(&name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if name != "john" {
t.Errorf("unexpected row name: %v", name)
}
if rows.Next() {
t.Error("expected next row to produce error")
}
if rows.Err() == nil {
t.Error("expected an error, but there was none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}