-
Notifications
You must be signed in to change notification settings - Fork 227
/
unit.go
126 lines (106 loc) · 3.28 KB
/
unit.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
package internal
import (
"net/http"
"net/url"
"reflect"
"github.com/8treenet/iris/v12/context"
"github.com/go-redis/redis"
)
var _ UnitTest = (*UnitTestImpl)(nil)
// UnitTest Unit testing tools.
type UnitTest interface {
FetchService(service interface{})
FetchRepository(repository interface{})
FetchFactory(factory interface{})
InstallDB(f func() (db interface{}))
InstallRedis(f func() (client redis.Cmdable))
InstallCustom(f func() interface{})
Run()
SetRequest(request *http.Request)
InjectBaseEntity(entity interface{})
}
// UnitTestImpl Unit testing tools.
type UnitTestImpl struct {
rt *worker
}
// NewUnitTest .
func NewUnitTest() UnitTest {
return new(UnitTestImpl)
}
// FetchService .
func (u *UnitTestImpl) FetchService(service interface{}) {
globalApp.FetchService(u.rt.IrisContext(), service)
}
// FetchRepository .
func (u *UnitTestImpl) FetchRepository(repository interface{}) {
instance := serviceElement{calls: []BeginRequest{}, workers: []reflect.Value{}}
value := reflect.ValueOf(repository).Elem()
ok := globalApp.rpool.diRepoFromValue(value, &instance)
if !ok {
globalApp.IrisApp.Logger().Fatalf("[Freedom] No dependency injection was found for the object,%v", value.Type().String())
}
if !value.CanSet() {
globalApp.IrisApp.Logger().Fatalf("[Freedom] This use repository object must be a capital variable, %v" + value.Type().String())
}
if br, ok := value.Interface().(BeginRequest); ok {
instance.calls = append(instance.calls, br)
}
globalApp.pool.beginRequest(u.rt, instance)
}
// FetchFactory .
func (u *UnitTestImpl) FetchFactory(factory interface{}) {
instance := serviceElement{calls: []BeginRequest{}, workers: []reflect.Value{}}
value := reflect.ValueOf(factory).Elem()
ok := globalApp.factoryPool.diFactoryFromValue(value, &instance)
if !ok {
globalApp.IrisApp.Logger().Fatalf("[Freedom] No dependency injection was found for the object,%v", value.Type().String())
}
if !value.CanSet() {
globalApp.IrisApp.Logger().Fatalf("[Freedom] This use repository object must be a capital variable, %v" + value.Type().String())
}
globalApp.pool.beginRequest(u.rt, instance)
}
// InstallDB .
func (u *UnitTestImpl) InstallDB(f func() (db interface{})) {
globalApp.InstallDB(f)
}
// InstallRedis .
func (u *UnitTestImpl) InstallRedis(f func() (client redis.Cmdable)) {
globalApp.InstallRedis(f)
}
// InstallCustom .
func (u *UnitTestImpl) InstallCustom(f func() interface{}) {
globalApp.InstallCustom(f)
}
// Run .
func (u *UnitTestImpl) Run() {
for index := 0; index < len(prepares); index++ {
prepares[index](globalApp)
}
u.rt = u.newRuntime()
logLevel := "debug"
globalApp.IrisApp.Logger().SetLevel(logLevel)
globalApp.installDB()
globalApp.other.booting()
globalApp.comPool.singleBooting(globalApp)
}
func (u *UnitTestImpl) newRuntime() *worker {
ctx := context.NewContext(globalApp.IrisApp)
request := new(http.Request)
request.URL = &url.URL{}
ctx.ResetRequest(request)
rt := newWorker(ctx)
ctx.Values().Set(WorkerKey, rt)
rt.bus = newBus(make(http.Header))
ctx.Values().Set(WorkerKey, rt)
return rt
}
// SetRequest .
func (u *UnitTestImpl) SetRequest(request *http.Request) {
u.rt.IrisContext().ResetRequest(request)
}
// InjectBaseEntity .
func (u *UnitTestImpl) InjectBaseEntity(entity interface{}) {
injectBaseEntity(u.rt, entity)
return
}