-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathbase_test.go
99 lines (81 loc) · 1.95 KB
/
base_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
package core
import (
"testing"
"github.com/artalkjs/artalk/v2/internal/config"
"github.com/stretchr/testify/assert"
)
type MockService struct{}
func (s *MockService) Init() error { return nil }
func (s *MockService) Dispose() error { return nil }
func TestNewApp(t *testing.T) {
conf := &config.Config{
// Initialize config fields for testing.
}
app := NewApp(conf)
assert.NotNil(t, app)
assert.Equal(t, conf, app.conf)
assert.NotNil(t, app.service)
}
func TestAppBootstrap(t *testing.T) {
conf := &config.Config{
Cache: config.CacheConf{
Enabled: true,
Type: config.CacheTypeBuiltin,
},
DB: config.DBConf{
Type: config.TypeSQLite,
Dsn: "file::memory:?cache=shared",
},
}
// create app instance
app := NewApp(conf)
defer app.ResetBootstrapState()
// bootstrap
if err := app.Bootstrap(); err != nil {
t.Fatal(err)
}
assert.Equal(t, conf, app.conf)
assert.NotNil(t, app.dao)
assert.NotNil(t, app.cache)
assert.NotNil(t, app.service)
}
func TestAppInjectAndService(t *testing.T) {
app := &App{
service: &map[string]Service{},
}
// inject
mockService := &MockService{}
AppInject[*MockService](app, mockService)
// get
gotService, err := AppService[*MockService](app)
if assert.NoError(t, err) {
assert.NotNil(t, gotService)
assert.Equal(t, mockService, gotService)
}
// err test
t.Run("AccessNilApp", func(t *testing.T) {
var app *App
_, err := AppService[*MockService](app)
assert.Error(t, err)
})
t.Run("AccessNilService", func(t *testing.T) {
app := &App{
service: &map[string]Service{},
}
_, err := AppService[*MockService](app)
assert.Error(t, err)
})
t.Run("AccessNilServicesMap", func(t *testing.T) {
app := &App{
service: nil,
}
_, err := AppService[*MockService](app)
assert.Error(t, err)
})
}
func TestApp_OnTerminate(t *testing.T) {
// Prepare a mock app instance with necessary fields
app := NewApp(&config.Config{})
hook := app.OnTerminate()
assert.NotNil(t, hook)
}