-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
104 lines (82 loc) · 2.24 KB
/
app_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
package medialocker
import (
"bytes"
"context"
"io"
"os"
"testing"
)
type TestingApp struct {
App
in bytes.Buffer
out bytes.Buffer
err bytes.Buffer
logs bytes.Buffer
}
func NewTestAppCtx() AppContext {
ctx := context.Background()
ctx = context.WithValue(ctx, FS_CTX_KEY, &testFileSystem)
ctx = context.WithValue(ctx, LOG_CTX_KEY, NewDefaultLogger())
return AppContext{Context: ctx}
}
func TestAppContextConfig(t *testing.T) {
subject := NewTestAppCtx()
if c := subject.NewContainer(AppContextConfig); c == nil {
t.Error("NewContainer(AppCOntextConfig) return nil, expected *Container.")
}
}
func (ab *AppBuilder) TestBuild() (*TestingApp, *App, []error) {
testApp := &TestingApp{App: *ab.app}
ab.app.Fs = GetTestFileSystem()
ab.app.In = io.Reader(&testApp.in)
ab.app.Out = io.Writer(&testApp.out)
ab.app.Err = io.Writer(&testApp.err)
ab.app.Log = NewTestLogger(os.Stderr)
ab.app.Config.MemDB = true
ab.app.Config.LogSQL = true
ab.app.Registry = *NewRegistry(ab.app.Log, ab.app.Config)
return testApp, ab.app, ab.errors
}
func TestAppConfig(t *testing.T) {
EnableTestFileSystem()
config, errors := BuildConfig(DefaultConfiguration())
for _, err := range errors {
t.Errorf("Unexpected error returned from BuildConfig! %s", err)
}
if config == EmptyConfig {
t.Errorf("Expected not EmptyConfig! %+v", EmptyConfig)
}
}
func TestAppBuilder(t *testing.T) {
testApp, app, errs := NewAppBuilder().TestBuild()
for _, err := range errs {
t.Errorf("Unexpected error for AppBuilder: %s", err)
}
if testApp == nil {
t.Errorf("Expected TestApp to not be nil, got nil!")
}
if app == nil {
t.Errorf("Expected app to not be nil, got nil!")
}
app.Shutdown()
}
func TestAppRegistry(t *testing.T) {
_, app, errs := NewAppBuilder().TestBuild()
for _, err := range errs {
t.Errorf("Unexpected error for AppBuilder: %s", err)
}
if app.Registry.dbFactory == nil {
t.Error("Got nil back for Registry.dbFactory, expected it to be present.")
}
db, err := app.Registry.dbFactory()
if err != nil {
t.Errorf("Expected error to be nil, got: %s", err)
}
if db == nil {
t.Error("Expected db to be present, instead got nil!")
}
if err := db.Ping(); err != nil {
t.Errorf("Unable to open DB, got err: %s", err)
}
app.Shutdown()
}