forked from coinbase/chainsformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapp.go
175 lines (149 loc) · 3.88 KB
/
testapp.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
package testapp
import (
"testing"
"github.com/coinbase/chainstorage/protos/coinbase/c3/common"
"github.com/coinbase/chainstorage/sdk/services"
"github.com/uber-go/tally/v4"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"go.uber.org/zap"
"github.com/coinbase/chainsformer/internal/config"
"github.com/coinbase/chainsformer/internal/utils/fxparams"
"github.com/coinbase/chainsformer/internal/utils/testutil"
"github.com/coinbase/chainsformer/internal/utils/tracer"
)
type (
TestApp interface {
Close()
Logger() *zap.Logger
Config() *config.Config
}
TestFn func(t *testing.T, cfg *config.Config)
testAppImpl struct {
app *fxtest.App
logger *zap.Logger
config *config.Config
}
localOnlyOption struct {
fx.Option
}
)
var (
TestConfigs = []string{
"bitcoin-mainnet",
"ethereum-mainnet",
"ethereum-goerli",
"polygon-mainnet",
"bsc-mainnet",
"arbitrum-mainnet",
"optimism-mainnet",
}
)
func New(t testing.TB, opts ...fx.Option) TestApp {
manager := services.NewMockSystemManager()
var cfg *config.Config
opts = append(
opts,
config.Module,
fxparams.Module,
tracer.Module,
fx.NopLogger,
fx.Provide(func() testing.TB { return t }),
fx.Provide(func() *zap.Logger { return manager.Logger() }),
fx.Provide(func() tally.Scope { return tally.NoopScope }),
fx.Provide(func() services.SystemManager { return manager }),
fx.Populate(&cfg),
)
app := fxtest.New(t, opts...)
app.RequireStart()
return &testAppImpl{
app: app,
logger: manager.Logger(),
config: cfg,
}
}
// WithIntegration runs the test only if $TEST_TYPE is integration.
func WithIntegration() fx.Option {
return &localOnlyOption{
Option: fx.Invoke(func(tb testing.TB, cfg *config.Config, logger *zap.Logger) {
if !cfg.IsIntegrationTest() {
logger.Warn("skipping integration test", zap.String("test", tb.Name()))
tb.Skip()
}
}),
}
}
// WithFunctional runs the test only if $TEST_TYPE is functional.
func WithFunctional() fx.Option {
return &localOnlyOption{
Option: fx.Invoke(func(tb testing.TB, cfg *config.Config, logger *zap.Logger) {
if !cfg.IsFunctionalTest() {
logger.Warn("skipping functional test", zap.String("test", tb.Name()))
tb.Skip()
}
}),
}
}
// WithConfig overrides the default config.
func WithConfig(cfg *config.Config) fx.Option {
return config.WithCustomConfig(cfg)
}
// WithBlockchainNetwork loads the config according to the specified blockchain and network.
func WithBlockchainNetwork(blockchain common.Blockchain, network common.Network) fx.Option {
cfg, err := config.New(
config.WithBlockchain(blockchain),
config.WithNetwork(network),
)
if err != nil {
panic(err)
}
return WithConfig(cfg)
}
func (a *testAppImpl) Close() {
a.app.RequireStop()
}
func (a *testAppImpl) Logger() *zap.Logger {
return a.logger
}
func (a *testAppImpl) Config() *config.Config {
return a.config
}
var envsToTest = []config.Env{
config.EnvLocal,
config.EnvDevelopment,
config.EnvProduction,
}
func TestAllEnvs(t *testing.T, fn TestFn) {
for _, env := range envsToTest {
t.Run(string(env), func(t *testing.T) {
require := testutil.Require(t)
cfg, err := config.New(config.WithEnvironment(env))
require.NoError(err)
require.Equal(env, cfg.Env())
fn(t, cfg)
})
}
}
func TestAllConfigs(t *testing.T, fn TestFn) {
for _, configName := range TestConfigs {
t.Run(configName, func(t *testing.T) {
for _, env := range envsToTest {
t.Run(string(env), func(t *testing.T) {
require := testutil.Require(t)
blockchain, network, err := config.ParseConfigName(configName)
require.NoError(err)
cfg, err := config.New(
config.WithEnvironment(env),
config.WithBlockchain(blockchain),
config.WithNetwork(network),
)
require.NoError(err)
require.Equal(env, cfg.Env())
require.Equal(blockchain, cfg.Blockchain())
require.Equal(network, cfg.Network())
fn(t, cfg)
})
}
})
}
}