-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper_test.go
360 lines (324 loc) · 7.9 KB
/
helper_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2020 The GMC Author. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// More information at https://github.com/snail007/gmc
package gmc
import (
"bytes"
"fmt"
gcore "github.com/snail007/gmc/core"
gconfig "github.com/snail007/gmc/module/config"
gdb "github.com/snail007/gmc/module/db"
gcaptcha "github.com/snail007/gmc/util/captcha"
gmap "github.com/snail007/gmc/util/map"
gtest "github.com/snail007/gmc/util/testing"
"github.com/stretchr/testify/assert"
"os"
"sync"
"testing"
)
var (
dbOnce = sync.Once{}
mysqlCfg = bytes.NewReader([]byte(`
[database]
default="mysql"
[[database.mysql]]
enable=true
id="default"
host="127.0.0.1"
port="3306"
username="user"
password="user"
database="test"
prefix=""
prefix_sql_holder="__PREFIX__"
charset="utf8"
collate="utf8_general_ci"
maxidle=30
maxconns=200
timeout=15000
readtimeout=15000
writetimeout=15000
maxlifetimeseconds=1800`))
sqlite3Cfg = bytes.NewReader([]byte(`
[database]
default="sqlite3"
[[database.sqlite3]]
enable=true
id="default"
database="test.db"
password=""
prefix=""
prefix_sql_holder="__PREFIX__"
# sync mode: 0:OFF, 1:NORMAL, 2:FULL, 3:EXTRA
syncmode=0
# open mode: ro,rw,rwc,memory
openmode="rw"
# cache mode: shared,private
cachemode="shared"`))
cacheRedisCfg = bytes.NewReader([]byte(`
[cache]
default="redis"
[[cache.redis]]
debug=true
enable=true
id="default"
address="127.0.0.1:6379"
prefix=""
password=""
timeout=10
dbnum=0
maxidle=10
maxactive=30
idletimeout=300
maxconnlifetime=3600
wait=false`))
cacheFileCfg = bytes.NewReader([]byte(`
[cache]
default="file"
[[cache.file]]
enable=true
id="default"
dir="{tmp}"
cleanupinterval=30`))
cacheMemCfg = bytes.NewReader([]byte(`
[cache]
default="memory"
[[cache.memory]]
enable=true
id="default"
cleanupinterval=30`))
i18nCfg = bytes.NewReader([]byte(`
[i18n]
enable=true
dir="i18n"
default="zh-cn"`))
)
func TestCacheAssistant_Cache(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(cacheMemCfg)
err := Cache.Init(cfg)
assert.NoError(err)
assert.Implements((*gcore.Cache)(nil), Cache.Memory())
assert.Implements((*gcore.Cache)(nil), Cache.Cache())
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestCacheAssistant_File(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(cacheFileCfg)
Cache.Init(cfg)
assert.Implements((*gcore.Cache)(nil), Cache.File())
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestCacheAssistant_Redis(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(cacheRedisCfg)
Cache.Init(cfg)
assert.Implements((*gcore.Cache)(nil), Cache.Redis())
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestDBAssistant_DB(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(mysqlCfg)
DB.Init(cfg)
assert.Implements((*gcore.Database)(nil), DB.DB())
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestDBAssistant_InitFromFile(t *testing.T) {
DB.InitFromFile("module/app/app.toml")
assert.Implements(t, (*gcore.Database)(nil), DB.DB())
}
func TestDBAssistant_MySQL(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(mysqlCfg)
DB.Init(cfg)
assert.Implements((*gcore.Database)(nil), DB.MySQL())
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestDBAssistant_SQLite3(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(sqlite3Cfg)
DB.Init(cfg)
assert.Implements((*gcore.Database)(nil), DB.SQLite3())
defer os.Remove("test.db")
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestDBAssistant_Table(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(mysqlCfg)
DB.Init(cfg)
assert.IsType(&gdb.Model{}, DB.Table("test"))
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestErrorAssistant_New(t *testing.T) {
e := fmt.Errorf("abc")
assert.Equal(t, "abc", Err.New(e).Error())
}
func TestErrorAssistant_Recover(t *testing.T) {
s := ""
g := sync.WaitGroup{}
g.Add(1)
a := 1
b := 0
go func() {
defer Err.Recover(func(e interface{}) {
s = fmt.Sprintf("%v", e)
g.Done()
})
_ = a / b
}()
g.Wait()
assert.Contains(t, s, "zero")
}
func TestErrorAssistant_RecoverString(t *testing.T) {
g := sync.WaitGroup{}
g.Add(1)
a := 1
b := 0
go func() {
defer Err.Recover("abc", true)
defer g.Done()
_ = a / b
}()
g.Wait()
}
func TestErrorAssistant_RecoverObj(t *testing.T) {
g := sync.WaitGroup{}
g.Add(1)
a := 1
b := 0
go func() {
defer Err.Recover(fmt.Errorf(""), true)
defer g.Done()
_ = a / b
}()
g.Wait()
}
func TestErrorAssistant_Stack(t *testing.T) {
e := fmt.Errorf("abc")
assert.Contains(t, Err.Stack(e), "helper_test.go")
assert.Contains(t, Err.Stack(e), "abc")
}
func TestErrorAssistant_Wrap(t *testing.T) {
e := fmt.Errorf("abc")
assert.Contains(t, Err.Wrap(e).Error(), "abc")
}
func TestI18nAssistant_Init(t *testing.T) {
assert := assert.New(t)
if gtest.RunProcess(t, func() {
cfg := gconfig.New()
cfg.SetConfigType("toml")
cfg.ReadConfig(i18nCfg)
i, err := I18n.Init(cfg)
ctx := New.Ctx()
ctx.SetConfig(cfg)
assert.NoError(err)
assert.Implements((*gcore.I18n)(nil), i)
assert.Implements((*gcore.I18n)(nil), New.Tr("", ctx))
}) {
return
}
_, _, err := gtest.NewProcess(t).Wait()
assert.Nil(err)
}
func TestNewAssistant_APIServer(t *testing.T) {
s, err := New.APIServer(New.Ctx(), ":")
assert.NoError(t, err)
assert.Implements(t, (*gcore.APIServer)(nil), s)
}
func TestNewAssistant_APIServerDefault(t *testing.T) {
s, err := New.APIServerDefault(New.Ctx())
assert.NoError(t, err)
assert.Implements(t, (*gcore.APIServer)(nil), s)
}
func TestNewAssistant_App(t *testing.T) {
assert.Implements(t, (*gcore.App)(nil), New.App())
}
func TestNewAssistant_AppDefault(t *testing.T) {
assert.Implements(t, (*gcore.App)(nil), New.AppDefault())
}
func TestNewAssistant_Captcha(t *testing.T) {
assert.IsType(t, &gcaptcha.Captcha{}, New.Captcha())
}
func TestNewAssistant_CaptchaDefault(t *testing.T) {
assert.IsType(t, &gcaptcha.Captcha{}, New.CaptchaDefault())
}
func TestNewAssistant_Config(t *testing.T) {
assert.Implements(t, (*gcore.Config)(nil), New.Config())
}
func TestNewAssistant_ConfigFile(t *testing.T) {
s, err := New.ConfigFile("module/app/app.toml")
assert.NoError(t, err)
assert.Implements(t, (*gcore.Config)(nil), s)
}
func TestNewAssistant_Ctx(t *testing.T) {
assert.Implements(t, (*gcore.Ctx)(nil), New.Ctx())
}
func TestNewAssistant_HTTPServer(t *testing.T) {
assert.Implements(t, (*gcore.HTTPServer)(nil), New.HTTPServer(New.Ctx()))
}
func TestNewAssistant_Logger(t *testing.T) {
assert.Implements(t, (*gcore.Logger)(nil), New.Logger(New.Ctx(), ""))
}
func TestNewAssistant_Map(t *testing.T) {
assert.IsType(t, &gmap.Map{}, New.Map())
}
func TestNewAssistant_Router(t *testing.T) {
assert.Implements(t, (*gcore.HTTPRouter)(nil), New.Router(New.Ctx()))
}
func TestNewErrorAssistant(t *testing.T) {
assert.IsType(t, &ErrorAssistant{}, NewErrorAssistant())
}
func Test_initHelper(t *testing.T) {
initHelper()
assert.NotNil(t, Err.p)
}