-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
73 lines (55 loc) · 1.4 KB
/
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
// SPDX-License-Identifier: MIT
// Package test 提供测试功能函数
package test
import (
"os"
"github.com/issue9/assert/v3"
"github.com/issue9/orm/v5"
"github.com/issue9/orm/v5/dialect"
"github.com/issue9/unique"
"github.com/issue9/web/server/servertest"
"gopkg.in/yaml.v3"
"github.com/issue9/cmfx"
_ "github.com/mattn/go-sqlite3"
)
type Suite struct {
*servertest.Tester
a *assert.Assertion
db *orm.DB
dsn string
closed bool
}
func NewSuite(a *assert.Assertion) *Suite {
dsn := unique.Number().String() + "_test.db"
db, err := orm.NewDB(dsn, dialect.Sqlite3("sqlite3"))
a.NotError(err).NotNil(db)
server := servertest.NewTester(a, nil)
err = server.Server().Files().Serializer().Add(yaml.Marshal, yaml.Unmarshal, ".yaml", ".yml")
a.NotError(err)
s := &Suite{
Tester: server,
a: a,
db: db,
dsn: dsn,
}
cmfx.AddProblems(server.Server().Problems())
s.a.TB().Cleanup(func() {
s.Close()
})
return s
}
func (s *Suite) DB() *orm.DB { return s.db }
func (s *Suite) Assertion() *assert.Assertion { return s.a }
// Close 关闭服务
//
// 如果未手动调用,则在 testing.TB.Cleanup 中自动关闭。
// 部分功能需要手动调用此方法,比如要主动关闭服务才能退出的路由测试。
func (s *Suite) Close() {
if s.closed {
return
}
s.closed = true
s.Tester.Close(0)
s.a.NotError(s.db.Close())
s.a.NotError(os.RemoveAll(s.dsn))
}