-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes-all_integration-sql_test.go
146 lines (141 loc) · 3.66 KB
/
types-all_integration-sql_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package sqljson_test
import (
"testing"
"github.com/rhaseven7h/sqljson"
sqlmock "github.com/DATA-DOG/go-sqlmock"
. "github.com/smartystreets/goconvey/convey"
)
func TestSQLIntegration(t *testing.T) {
type modelSupplier struct {
ID int
ContactEmail sqljson.NullString
IsAdmin sqljson.NullBool
Followers sqljson.NullInt64
BankBalance sqljson.NullFloat64
}
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
Convey("Given a sql mock using a struct with sqljson.Null* - with valid values", t, func() {
mock.
ExpectQuery(`
SELECT
id,
contact_email,
is_admin,
followers,
bank_balance
FROM suppliers
`).
WillReturnRows(
sqlmock.
NewRows([]string{
"id",
"contact_email",
"is_admin",
"followers",
"bank_balance",
}).
AddRow(
10,
"gmedina@ooyala.com",
true,
100,
123.45,
),
)
Convey("When I query a row and scan it", func() {
supplier := &modelSupplier{}
row := db.QueryRow(`
SELECT
id,
contact_email,
is_admin,
followers,
bank_balance
FROM suppliers
`)
dbErr := row.Scan(
&(*supplier).ID,
&(*supplier).ContactEmail,
&(*supplier).IsAdmin,
&(*supplier).Followers,
&(*supplier).BankBalance,
)
mockErr := mock.ExpectationsWereMet()
Convey("Then I should get the row correctly", func() {
So(dbErr, ShouldBeNil)
So(mockErr, ShouldBeNil)
So(supplier.ID, ShouldEqual, 10)
So(supplier.ContactEmail.Valid, ShouldBeTrue)
So(supplier.ContactEmail.String, ShouldEqual, "gmedina@ooyala.com")
So(supplier.IsAdmin.Valid, ShouldBeTrue)
So(supplier.IsAdmin.Bool, ShouldBeTrue)
So(supplier.Followers.Valid, ShouldBeTrue)
So(supplier.Followers.Int64, ShouldEqual, 100)
So(supplier.BankBalance.Valid, ShouldBeTrue)
So(supplier.BankBalance.Float64, ShouldEqual, 123.45)
})
})
})
Convey("Given a sql mock using a struct with sqljson.Null* - with null values", t, func() {
mock.
ExpectQuery(`
SELECT
id,
contact_email,
is_admin,
followers,
bank_balance
FROM suppliers
`).
WillReturnRows(
sqlmock.
NewRows([]string{
"id",
"contact_email",
"is_admin",
"followers",
"bank_balance",
}).
AddRow(
10,
nil,
nil,
nil,
nil,
),
)
Convey("When I query a row and scan it", func() {
supplier := &modelSupplier{}
row := db.QueryRow(`
SELECT
id,
contact_email,
is_admin,
followers,
bank_balance
FROM suppliers
`)
dbErr := row.Scan(
&(*supplier).ID,
&(*supplier).ContactEmail,
&(*supplier).IsAdmin,
&(*supplier).Followers,
&(*supplier).BankBalance,
)
mockErr := mock.ExpectationsWereMet()
Convey("Then I should get the row correctly", func() {
So(dbErr, ShouldBeNil)
So(mockErr, ShouldBeNil)
So(supplier.ID, ShouldEqual, 10)
So(supplier.ContactEmail.Valid, ShouldBeFalse)
So(supplier.IsAdmin.Valid, ShouldBeFalse)
So(supplier.Followers.Valid, ShouldBeFalse)
So(supplier.BankBalance.Valid, ShouldBeFalse)
})
})
})
}