-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlkebab_test.go
228 lines (191 loc) · 4.56 KB
/
mysqlkebab_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package mysqlkebab
import (
"encoding/json"
"fmt"
"testing"
"time"
)
var db *DBLink
func TestConnect(t *testing.T) {
cs := ConnStringDirect("mysql://mysqlkebab:001001001001@localhost/mysqlkebab?sslmode=disable")
opts := Options(cs, 10, 10, 5, 5, 10, true)
if _db, err := NewConnected(opts); err != nil {
t.Fatal(err)
} else {
db = _db
}
}
func TestExec(t *testing.T) {
if _, err := db.Exec("DROP TABLE IF EXISTS kbd"); err != nil {
t.Fatal(err)
} else {
t.Log("table droped")
}
if _, err := db.Exec("CREATE TABLE kbd (id SERIAL NOT NULL PRIMARY KEY,txt VARCHAR(30), ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, dec DECIMAL, x INT, j JSONB)"); err != nil {
t.Fatal(err)
} else {
t.Log("table kbd created")
}
if i, err := db.Exec("INSERT INTO kbd (txt, dec, x) VALUES ('test one',1.5,8),('test two',2.6,90),('test three',3.1,120),('test four',4.6,311),('test five',5.10,501)"); err != nil {
t.Fatal(err)
} else {
if i != 5 {
t.Logf("%d records inserted\n", i)
}
}
}
func TestGetString(t *testing.T) {
if s, err := db.GetString("SELECT CURRENT_TIMESTAMP now"); err != nil {
t.Fatal(err)
} else {
t.Log(s)
}
}
func TestGetInt64(t *testing.T) {
if i, err := db.GetInt64("SELECT COUNT(*) FROM kbd WHERE x>=120"); err != nil {
t.Fatal(err)
} else {
if i != 3 {
t.Fatalf("expected 3, got %d", i)
}
}
}
func TestGetFloat64(t *testing.T) {
if f, err := db.GetFloat64("SELECT SUM(dec) FROM kbd"); err != nil {
t.Fatal(err)
} else {
if f != 16.9 {
t.Fatalf("expected 16.9, got %f", f)
}
}
}
func TestGetBool(t *testing.T) {
if b, err := db.GetBool("SELECT EXISTS ( SELECT*FROM kbd)"); err != nil {
t.Fatal(err)
} else {
if !b {
t.Fatalf("'true' expected, got false")
}
}
}
func TestDBLink_Insert(t *testing.T) {
pairs := map[string]interface{}{
"txt": "testing Insert()",
"dec": 88.8888888,
"x": time.Now().Unix(),
}
if err := db.Insert("kbd", pairs); err != nil {
t.Fatal(err)
}
}
func TestDBLink_InsertID(t *testing.T) {
pairs := map[string]interface{}{
"txt": "testing Insert()",
"dec": 99.9999999,
"x": time.Now().Unix(),
}
if id, err := db.InsertID("kbd", pairs); err != nil {
t.Fatal(err)
} else {
t.Logf("last inserted id: %d\n", id)
}
}
func TestDBLink_GetOne(t *testing.T) {
if r, err := db.GetOne("SELECT*FROM kbd WHERE true LIMIT 1"); err != nil {
t.Fatal(err)
} else {
if r.Ready() {
t.Log(r.JSON())
} else {
fmt.Println("empty row")
}
}
}
func TestDBLink_GetMany(t *testing.T) {
if rs, err := db.GetMany("SELECT*FROM kbd"); err != nil {
t.Fatal(err)
} else {
for rs.Next() {
row, _ := rs.Row()
fmt.Printf("id:%d\ttxt:%s\tdec:%f\tx:%d\n", row.Int64("id"), row.String("txt"), row.Float64("dec"), row.Int64("x"))
}
t.Logf(rs.JSON())
}
}
func TestDBLink_MustGetMany(t *testing.T) {
if _, err := db.MustGetMany("SELECT*FROM kbd WHERE id<0"); err == nil {
t.Fatal(err)
} else {
t.Log("Super ok!")
}
}
func TestDBLink_InsertJSONMap(t *testing.T) {
d, err0 := json.Marshal(map[string]interface{}{"xyz": 987, "b": true, "i": 987654321})
if err0 != nil {
t.Fatal(err0)
}
pairs := map[string]interface{}{
"txt": "TestDBLink_InsertJSONMap",
"dec": 0,
"x": 0,
"J": string(d),
}
if err := db.Insert("kbd", pairs); err != nil {
t.Fatal(err)
}
}
func TestDBLink_GetJSONMap(t *testing.T) {
if m, err := db.GetJSONMap("SELECT j FROM kbd WHERE txt='TestDBLink_InsertJSONMap' LIMIT 1"); err != nil {
t.Error(err)
} else {
t.Log(m)
}
}
type jstruct struct {
ABC int `json:"abc"`
B bool `json:"b"`
I int `json:"i"`
}
func TestDBLink_InsertJSONStruct(t *testing.T) {
x := jstruct{ABC: 123, B: true, I: 9}
d, err0 := json.Marshal(x)
if err0 != nil {
t.Fatal(err0)
}
pairs := map[string]interface{}{
"txt": "TestDBLink_InsertJSONStruct",
"dec": 0,
"x": 0,
"j": string(d),
}
if err := db.Insert("kbd", pairs); err != nil {
t.Fatal(err)
}
}
func TestDBLink_GetJSONStruct(t *testing.T) {
var j jstruct
if err := db.GetJSONStruct(&j, "SELECT j FROM kbd WHERE txt='TestDBLink_InsertJSONStruct' LIMIT 1"); err != nil {
t.Error(err)
} else {
t.Log(j)
}
}
func Test_RowJSONMap(t *testing.T) {
if rs, err := db.GetOne("SELECT j FROM kbd WHERE txt='TestDBLink_InsertJSONMap' LIMIT 1"); err != nil {
t.Error(err)
} else {
t.Log(rs.JSONMap("j"))
}
}
func Test_RowJSONStruct(t *testing.T) {
if rs, err := db.GetOne("SELECT j FROM kbd WHERE txt='TestDBLink_InsertJSONStruct' LIMIT 1"); err != nil {
t.Error(err)
} else {
var j jstruct
if err0 := rs.JSONStruct("j", &j); err0 != nil {
t.Error(err0)
return
}
t.Log(j)
}
}