-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_test.go
141 lines (114 loc) · 3.13 KB
/
plugin_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
package apidCRUD
import (
"testing"
"strings"
"net/http"
"net/http/httptest"
"github.com/apid/apid-core"
)
// ----- unit tests for initDB()
func Test_initDB(t *testing.T) {
cx := newTestContext(t)
x, err := initDB(dbName)
if !cx.assertErrorNil(err, "error ret") {
return
}
cx.assertTrue(x.handle != nil, "handle should not be nil")
}
// ----- unit tests for registerHandlers() and addPath()
type mockApiService struct {
hfmap map[string]http.HandlerFunc
}
func newMockApiService() *mockApiService {
fmap := make(map[string]http.HandlerFunc)
return &mockApiService{fmap}
}
func (service mockApiService) HandleFunc(path string,
hf http.HandlerFunc) apid.Route {
// record the handler that is being registered.
service.hfmap[path] = hf
return nil
}
func registerHandler_Checker(cx *testContext,
service *mockApiService,
tc callApiMethod_TC) {
path := basePath + tc.descStr
fp := service.hfmap[path]
if !cx.assertTrue(fp != nil, "handler should not be nil") {
return
}
r, _ := http.NewRequest(tc.verb, path, strings.NewReader(""))
w := httptest.NewRecorder()
// make the call
fp(w, r)
// check the recorded response
cx.assertEqual(tc.xcode, w.Code, "w.Code")
}
func Test_registerHandlers(t *testing.T) {
service := newMockApiService()
registerHandlers(service, fakeApiTable)
cx := newTestContext(t, "callApiMethod_Tab")
// check that the expected paths were in fact registered.
for _, desc := range callApiMethod_Tab {
registerHandler_Checker(cx, service, desc)
cx.bump()
}
}
// ----- unit tests for initPlugin()
type mockForModuler struct {
name string
}
func (fmi mockForModuler) ForModule(name string) apid.LogService {
return apid.Log()
}
func Test_realInitPlugin(t *testing.T) {
cx := newTestContext(t)
gsi := mockGetStringer{}
fmi := mockForModuler{}
hfi := newMockApiService()
_, err := realInitPlugin(gsi, fmi, *hfi)
cx.assertErrorNil(err, "returned error")
}
// ----- support for unit tests configuration
// global conf variables with values to be used during unit testing.
// used by initConfig().
var utConfData = map[string]string {
"apidCRUD_base_path": "/test",
"apidCRUD_max_recs": "7",
"apidCRUD_db_driver": "sqlite3",
"apidCRUD_db_name": "unit-test.db",
}
// ----- unit tests for confGet()
type confGet_TC struct {
name string
defval string
xval string
}
var confGet_Tab = []confGet_TC {
{"apidCRUD_db_name", "garbage", "unit-test.db"}, // this key is present
{"not-there", "no", "no"}, // this key is not present
}
// mockGetStringer is compatible with the interface expected by confGet().
type mockGetStringer struct {
data map[string]string
}
func (gs mockGetStringer) GetString(name string) string {
return gs.data[name]
}
func confGet_Checker(cx *testContext, gs getStringer, tc *confGet_TC) {
res := confGet(gs, tc.name, tc.defval)
cx.assertEqual(tc.xval, res, "result")
}
func Test_confGet(t *testing.T) {
cx := newTestContext(t, "confGet_Tab")
gs := mockGetStringer{utConfData}
for _, tc := range confGet_Tab {
confGet_Checker(cx, gs, &tc)
cx.bump()
}
}
func utInitConfig() {
// this just proves it can be called without crashing
gs := mockGetStringer{utConfData}
initConfig(gs)
}