-
Notifications
You must be signed in to change notification settings - Fork 23
/
app_test.go
87 lines (78 loc) · 1.99 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
package gobay
import (
"errors"
"testing"
"github.com/hashicorp/go-multierror"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type testExtension struct {
mock.Mock
}
func (e *testExtension) Init(app *Application) error {
args := e.Called("Init")
return args.Error(0)
}
func (e *testExtension) Close() error {
args := e.Called("Close")
return args.Error(0)
}
func (e *testExtension) Application() *Application {
return nil
}
func (e *testExtension) Object() interface{} {
return nil
}
func TestCreateApp(t *testing.T) {
assert := assert.New(t)
testExt := new(testExtension)
exts := map[Key]Extension{
"test": testExt,
}
// config file not found
call := testExt.On("Init", mock.Anything)
call.Return(nil)
app, err := CreateApp(".", "testing", exts)
assert.Nil(app)
assert.NotNil(err)
testExt.AssertNotCalled(t, "Init")
// success
app, err = CreateApp("./testdata", "testing", exts)
assert.NotNil(app)
assert.Nil(err)
assert.NotNil(app.Get("test"))
assert.Nil(app.Get("cache"))
testExt.AssertNumberOfCalls(t, "Init", 1)
// call extension.Init failed
initErr := errors.New("init failed")
call.Return(initErr)
app, err = CreateApp("./testdata", "testing", exts)
assert.Nil(app)
assert.Equal(multierror.Append(errors.New("test"), initErr), err)
}
func TestApplicationClose(t *testing.T) {
assert := assert.New(t)
testExt := new(testExtension)
exts := map[Key]Extension{
"test": testExt,
}
testExt.On("Init", mock.Anything).Return(nil)
app, _ := CreateApp("./testdata", "testing", exts)
// call extension.Close failed
closeErr := errors.New("close failed")
call := testExt.On("Close", mock.Anything)
call.Return(closeErr)
err := app.Close()
assert.Equal(err, closeErr)
testExt.AssertNumberOfCalls(t, "Close", 1)
// success
call.Return(nil)
err = app.Close()
assert.Nil(err)
testExt.AssertNumberOfCalls(t, "Close", 2)
// close again
err = app.Close()
assert.Nil(err)
testExt.AssertNumberOfCalls(t, "Close", 2)
}