-
Notifications
You must be signed in to change notification settings - Fork 2
/
null_string_test.go
48 lines (44 loc) · 1.22 KB
/
null_string_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
package gosql_test
import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/twharmon/gosql"
)
func TestNullStringValid(t *testing.T) {
db, mock, err := getMockDB()
check(t, err)
type T struct {
ID int `idx:"primary"`
Name gosql.NullString
}
control := T{
ID: 5,
Name: gosql.NullString{Valid: true, String: "foo"},
}
rows := sqlmock.NewRows([]string{"id", "name"})
rows.AddRow(control.ID, control.Name)
mock.ExpectQuery(`^select \* from t where id = \? limit 1$`).WithArgs(control.ID).WillReturnRows(rows)
var test T
check(t, db.Select("*").Where("id = ?", control.ID).Get(&test))
check(t, mock.ExpectationsWereMet())
equals(t, control, test)
}
func TestNullStringNotValid(t *testing.T) {
db, mock, err := getMockDB()
check(t, err)
type T struct {
ID int `idx:"primary"`
Name gosql.NullString
}
control := T{
ID: 5,
Name: gosql.NullString{Valid: false},
}
rows := sqlmock.NewRows([]string{"id", "name"})
rows.AddRow(control.ID, control.Name)
mock.ExpectQuery(`^select \* from t where id = \? limit 1$`).WithArgs(control.ID).WillReturnRows(rows)
var test T
check(t, db.Select("*").Where("id = ?", control.ID).Get(&test))
check(t, mock.ExpectationsWereMet())
equals(t, control, test)
}