-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules_test.go
176 lines (143 loc) · 3.54 KB
/
modules_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
package workfx
import (
"database/sql"
"testing"
"github.com/freerware/work"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"go.uber.org/zap"
)
type dataMapper struct{}
func (dm dataMapper) Insert(e ...interface{}) error { return nil }
func (dm dataMapper) Update(e ...interface{}) error { return nil }
func (dm dataMapper) Delete(e ...interface{}) error { return nil }
type sqlDataMapper struct{}
func (dm sqlDataMapper) Insert(tx *sql.Tx, e ...interface{}) error { return nil }
func (dm sqlDataMapper) Update(tx *sql.Tx, e ...interface{}) error { return nil }
func (dm sqlDataMapper) Delete(tx *sql.Tx, e ...interface{}) error { return nil }
type entity struct{}
type sqlUniterParameters struct {
fx.In
Uniter work.Uniter `name:"sqlWorkUniter"`
}
type bestEffortUniterParameters struct {
fx.In
Uniter work.Uniter `name:"bestEffortWorkUniter"`
}
func TestSQLUnitModule(t *testing.T) {
//arrange.
type result struct {
fx.Out
Mappers map[work.TypeName]work.SQLDataMapper
DB *sql.DB `name:"rwDB"`
Logger *zap.Logger
Scope tally.Scope
}
unitDeps := func() result {
mappers := make(map[work.TypeName]work.SQLDataMapper)
dm := sqlDataMapper{}
t := work.TypeNameOf(entity{})
mappers[t] = &dm
var db *sql.DB
l := zap.NewExample()
s := tally.NewTestScope("test", map[string]string{})
return result{
Mappers: mappers,
DB: db,
Logger: l,
Scope: s,
}
}
var uniter *work.Uniter
//action.
fxtest.New(
t,
fx.Provide(unitDeps),
Modules.SQLUnit,
fx.Invoke(func(p sqlUniterParameters) {
uniter = &p.Uniter
}),
).RequireStart().RequireStop()
//assert.
require.NotNil(t, uniter)
}
func TestBestEffortUnit(t *testing.T) {
//arrange.
type result struct {
fx.Out
Mappers map[work.TypeName]work.DataMapper
Logger *zap.Logger
Scope tally.Scope
}
unitDeps := func() result {
mappers := make(map[work.TypeName]work.DataMapper)
dm := dataMapper{}
t := work.TypeNameOf(entity{})
mappers[t] = &dm
l := zap.NewExample()
s := tally.NewTestScope("test", map[string]string{})
return result{
Mappers: mappers,
Logger: l,
Scope: s,
}
}
var uniter *work.Uniter
//action.
fxtest.New(
t,
fx.Provide(unitDeps),
Modules.BestEffortUnit,
fx.Invoke(func(p bestEffortUniterParameters) {
uniter = &p.Uniter
}),
).RequireStart().RequireStop()
//assert.
require.NotNil(t, uniter)
}
func TestSQLUnitAndBestEffortUnit(t *testing.T) {
//arrange.
type sResult struct {
fx.Out
Mappers map[work.TypeName]work.SQLDataMapper
DB *sql.DB `name:"rwDB"`
Logger *zap.Logger
Scope tally.Scope
}
type bResult struct {
fx.Out
Mappers map[work.TypeName]work.DataMapper
}
unitDeps := func() (sResult, bResult) {
sqlMappers := make(map[work.TypeName]work.SQLDataMapper)
mappers := make(map[work.TypeName]work.DataMapper)
sdm := sqlDataMapper{}
dm := dataMapper{}
t := work.TypeNameOf(entity{})
sqlMappers[t] = &sdm
mappers[t] = &dm
var db *sql.DB
l := zap.NewExample()
s := tally.NewTestScope("test", map[string]string{})
return sResult{Mappers: sqlMappers, DB: db, Logger: l, Scope: s},
bResult{Mappers: mappers}
}
var sql *work.Uniter
var bestEffort *work.Uniter
//action.
fxtest.New(
t,
fx.Provide(unitDeps),
Modules.BestEffortUnit,
Modules.SQLUnit,
fx.Invoke(func(sp sqlUniterParameters, bep bestEffortUniterParameters) {
sql = &sp.Uniter
bestEffort = &bep.Uniter
}),
).RequireStart().RequireStop()
//assert.
require.NotNil(t, sql)
require.NotNil(t, bestEffort)
}