-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry_test.go
162 lines (136 loc) · 3.81 KB
/
registry_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package registry
/**
* Mostly helper utilities
*/
import (
"encoding/json"
"fmt"
"net/http/httptest"
"path/filepath"
"reflect"
"runtime"
"testing"
"github.com/stretchr/testify/mock"
)
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
// nequals fails the test if exp is equal to act.
func nequals(tb testing.TB, exp, act interface{}) {
if reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:expected unequal:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
func headerHas(tb testing.TB, w *httptest.ResponseRecorder, key string, value string) {
equals(tb, 1, len(w.HeaderMap[key]))
equals(tb, value, w.HeaderMap[key][0])
}
func contentTypeWasJSON(tb testing.TB, w *httptest.ResponseRecorder) {
headerHas(tb, w, "Content-Type", "application/json; charset=UTF-8")
}
func matchedAPIError(tb testing.TB, expected APIErrorResponse, w *httptest.ResponseRecorder) {
// Content type better be JSON
contentTypeWasJSON(tb, w)
// ...and the content better be a standardized error in JSON too!
var apiError APIErrorResponse
err := json.NewDecoder(w.Body).Decode(&apiError)
ok(tb, err)
equals(tb, expected.Message, apiError.Message)
}
/**
Autogenerated with mockery -name=store
There's a few changes though. Buyer beware.
**/
type mockStore struct {
mock.Mock
registerCall map[string]RegisterReceiver
}
type RegisterReceiver struct {
Input Template
OutputTemplate Template
OutputError error
Actual Template
}
func (_m *mockStore) GetTemplate(name string) (Template, error) {
ret := _m.Called(name)
var r0 Template
if rf, ok := ret.Get(0).(func(string) Template); ok {
r0 = rf(name)
} else {
r0 = ret.Get(0).(Template)
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
func (_m *mockStore) RegisterTemplate(tmpl Template) (Template, error) {
receiver := _m.registerCall[tmpl.Name]
receiver.Actual = tmpl
return receiver.OutputTemplate, receiver.OutputError
}
func (_m *mockStore) ListTemplates() ([]Template, error) {
ret := _m.Called()
var r0 []Template
if rf, ok := ret.Get(0).(func() []Template); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]Template)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
func (_m *mockStore) UpdateTemplate(tmpl Template) (Template, error) {
receiver := _m.registerCall[tmpl.Name]
receiver.Actual = tmpl
return receiver.OutputTemplate, receiver.OutputError
}
func (_m *mockStore) DeleteTemplate(name string) (Template, error) {
ret := _m.Called(name)
var r0 Template
if rf, ok := ret.Get(0).(func(string) Template); ok {
r0 = rf(name)
} else {
r0 = ret.Get(0).(Template)
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}