/
test_generator.go
373 lines (345 loc) Β· 12.4 KB
/
test_generator.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package genapp
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"github.com/goadesign/goa/design"
"github.com/goadesign/goa/goagen/codegen"
)
func makeTestDir(g *Generator, apiName string) (outDir string, err error) {
outDir = filepath.Join(g.outDir, "test")
if err = os.RemoveAll(outDir); err != nil {
return
}
if err = os.MkdirAll(outDir, 0755); err != nil {
return
}
g.genfiles = append(g.genfiles, outDir)
return
}
// TestMethod structure
type TestMethod struct {
Name string
Comment string
ResourceName string
ActionName string
ControllerName string
ContextVarName string
ContextType string
RouteVerb string
FullPath string
Status int
ReturnType *ObjectType
Params []*ObjectType
QueryParams []*ObjectType
Payload *ObjectType
}
// ObjectType structure
type ObjectType struct {
Label string
Name string
Type string
Pointer string
Validatable bool
}
func (g *Generator) generateResourceTest(api *design.APIDefinition) error {
if len(api.Resources) == 0 {
return nil
}
funcs := template.FuncMap{"isSlice": isSlice}
testTmpl := template.Must(template.New("test").Funcs(funcs).Parse(testTmpl))
outDir, err := makeTestDir(g, api.Name)
if err != nil {
return err
}
appPkg, err := codegen.PackagePath(g.outDir)
if err != nil {
return err
}
imports := []*codegen.ImportSpec{
codegen.SimpleImport("bytes"),
codegen.SimpleImport("fmt"),
codegen.SimpleImport("io"),
codegen.SimpleImport("log"),
codegen.SimpleImport("net/http"),
codegen.SimpleImport("net/http/httptest"),
codegen.SimpleImport("net/url"),
codegen.SimpleImport("strconv"),
codegen.SimpleImport("strings"),
codegen.SimpleImport("testing"),
codegen.SimpleImport("time"),
codegen.SimpleImport(appPkg),
codegen.SimpleImport("github.com/goadesign/goa"),
codegen.SimpleImport("github.com/goadesign/goa/goatest"),
codegen.SimpleImport("golang.org/x/net/context"),
}
return api.IterateResources(func(res *design.ResourceDefinition) error {
filename := filepath.Join(outDir, codegen.SnakeCase(res.Name)+"_testing.go")
file, err := codegen.SourceFileFor(filename)
if err != nil {
return err
}
if err := file.WriteHeader("", "test", imports); err != nil {
return err
}
var methods []*TestMethod
if err := res.IterateActions(func(action *design.ActionDefinition) error {
if err := action.IterateResponses(func(response *design.ResponseDefinition) error {
if response.Status == 101 { // SwitchingProtocols, Don't currently handle WebSocket endpoints
return nil
}
for routeIndex, route := range action.Routes {
mediaType := design.Design.MediaTypeWithIdentifier(response.MediaType)
if mediaType == nil {
methods = append(methods, g.createTestMethod(res, action, response, route, routeIndex, nil, nil))
} else {
if err := mediaType.IterateViews(func(view *design.ViewDefinition) error {
methods = append(methods, g.createTestMethod(res, action, response, route, routeIndex, mediaType, view))
return nil
}); err != nil {
return err
}
}
}
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return err
}
g.genfiles = append(g.genfiles, filename)
err = testTmpl.Execute(file, methods)
if err != nil {
panic(err)
}
return file.FormatCode()
})
}
func (g *Generator) createTestMethod(resource *design.ResourceDefinition, action *design.ActionDefinition,
response *design.ResponseDefinition, route *design.RouteDefinition, routeIndex int,
mediaType *design.MediaTypeDefinition, view *design.ViewDefinition) *TestMethod {
var (
actionName, ctrlName, varName string
routeQualifier, viewQualifier, respQualifier string
comment string
returnType *ObjectType
payload *ObjectType
)
actionName = codegen.Goify(action.Name, true)
ctrlName = codegen.Goify(resource.Name, true)
varName = codegen.Goify(action.Name, false)
routeQualifier = suffixRoute(action.Routes, routeIndex)
if view != nil && view.Name != "default" {
viewQualifier = codegen.Goify(view.Name, true)
}
respQualifier = codegen.Goify(response.Name, true)
hasReturnValue := view != nil && mediaType != nil
if hasReturnValue {
p, _, err := mediaType.Project(view.Name)
if err != nil {
panic(err) // bug
}
tmp := codegen.GoTypeName(p, nil, 0, false)
if !p.IsError() {
tmp = fmt.Sprintf("%s.%s", g.target, tmp)
}
validate := codegen.RecursiveChecker(p.AttributeDefinition, false, false, false, "payload", "raw", 1, true)
returnType = &ObjectType{}
returnType.Type = tmp
if p.IsObject() && !p.IsError() {
returnType.Pointer = "*"
}
returnType.Validatable = validate != ""
}
comment = "runs the method " + actionName + " of the given controller with the given parameters"
if action.Payload != nil {
comment += " and payload"
}
comment += ".\n// It returns the response writer so it's possible to inspect the response headers"
if hasReturnValue {
comment += " and the media type struct written to the response"
}
comment += "."
if action.Payload != nil {
payload = &ObjectType{}
payload.Name = "payload"
payload.Type = fmt.Sprintf("%s.%s", g.target, codegen.Goify(action.Payload.TypeName, true))
if !action.Payload.IsPrimitive() && !action.Payload.IsArray() && !action.Payload.IsHash() {
payload.Pointer = "*"
}
validate := codegen.RecursiveChecker(action.Payload.AttributeDefinition, false, false, false, "payload", "raw", 1, false)
if validate != "" {
payload.Validatable = true
}
}
return &TestMethod{
Name: fmt.Sprintf("%s%s%s%s%s", actionName, ctrlName, respQualifier, routeQualifier, viewQualifier),
ActionName: actionName,
ResourceName: ctrlName,
Comment: comment,
Params: pathParams(action, route),
QueryParams: queryParams(action),
Payload: payload,
ReturnType: returnType,
ControllerName: fmt.Sprintf("%s.%sController", g.target, ctrlName),
ContextVarName: fmt.Sprintf("%sCtx", varName),
ContextType: fmt.Sprintf("%s.New%s%sContext", g.target, actionName, ctrlName),
RouteVerb: route.Verb,
Status: response.Status,
FullPath: goPathFormat(route.FullPath()),
}
}
// pathParams returns the path params for the given action and route.
func pathParams(action *design.ActionDefinition, route *design.RouteDefinition) []*ObjectType {
return paramFromNames(action, route.Params())
}
// queryParams returns the query string params for the given action.
func queryParams(action *design.ActionDefinition) []*ObjectType {
var qparams []string
if qps := action.QueryParams; qps != nil {
for pname := range qps.Type.ToObject() {
qparams = append(qparams, pname)
}
}
sort.Strings(qparams)
return paramFromNames(action, qparams)
}
func paramFromNames(action *design.ActionDefinition, names []string) (params []*ObjectType) {
for _, paramName := range names {
for name, att := range action.Params.Type.ToObject() {
if name == paramName {
param := &ObjectType{}
param.Label = name
param.Name = codegen.Goify(name, false)
param.Type = codegen.GoTypeRef(att.Type, nil, 0, false)
if att.Type.IsPrimitive() && action.Params.IsPrimitivePointer(name) {
param.Pointer = "*"
}
params = append(params, param)
}
}
}
return
}
func goPathFormat(path string) string {
return design.WildcardRegex.ReplaceAllLiteralString(path, "/%v")
}
func suffixRoute(routes []*design.RouteDefinition, currIndex int) string {
if len(routes) > 1 && currIndex > 0 {
return strconv.Itoa(currIndex)
}
return ""
}
func isSlice(typeName string) bool {
return strings.HasPrefix(typeName, "[]")
}
var convertParamTmpl = `{{ if eq .Type "string" }} sliceVal := []string{ {{ if .Pointer }}*{{ end }}{{ .Name }}}{{/*
*/}}{{ else if eq .Type "int" }} sliceVal := []string{strconv.Itoa({{ if .Pointer }}*{{ end }}{{ .Name }})}{{/*
*/}}{{ else if eq .Type "[]string" }} sliceVal := {{ .Name }}{{/*
*/}}{{ else if (isSlice .Type) }} sliceVal := make([]string, len({{ .Name }}))
for i, v := range {{ .Name }} {
sliceVal[i] = fmt.Sprintf("%v", v)
}{{/*
*/}}{{ else if eq .Type "time.Time" }} sliceVal := []string{ {{ if .Pointer }}*{{ end }}{{ .Name }}.Format(time.RFC3339)}{{/*
*/}}{{ else }} sliceVal := []string{fmt.Sprintf("%v", {{ if .Pointer }}*{{ end }}{{ .Name }})}{{ end }}`
var testTmpl = `{{ define "convertParam" }}` + convertParamTmpl + `{{ end }}` + `
{{ range $test := . }}
// {{ $test.Name }} {{ $test.Comment }}
// If ctx is nil then context.Background() is used.
// If service is nil then a default service is created.
func {{ $test.Name }}(t *testing.T, ctx context.Context, service *goa.Service, ctrl {{ $test.ControllerName}}{{/*
*/}}{{ range $param := $test.Params }}, {{ $param.Name }} {{ $param.Pointer }}{{ $param.Type }}{{ end }}{{/*
*/}}{{ range $param := $test.QueryParams }}, {{ $param.Name }} {{ $param.Pointer }}{{ $param.Type }}{{ end }}{{/*
*/}}{{ if $test.Payload }}, {{ $test.Payload.Name }} {{ $test.Payload.Pointer }}{{ $test.Payload.Type }}{{ end }}){{/*
*/}} (http.ResponseWriter{{ if $test.ReturnType }}, {{ $test.ReturnType.Pointer }}{{ $test.ReturnType.Type }}{{ end }}) {
// Setup service
var (
logBuf bytes.Buffer
resp interface{}
respSetter goatest.ResponseSetterFunc = func(r interface{}) { resp = r }
)
if service == nil {
service = goatest.Service(&logBuf, respSetter)
} else {
logger := log.New(&logBuf, "", log.Ltime)
service.WithLogger(goa.NewLogger(logger))
newEncoder := func(io.Writer) goa.Encoder { return respSetter }
service.Encoder = goa.NewHTTPEncoder() // Make sure the code ends up using this decoder
service.Encoder.Register(newEncoder, "*/*")
}
{{ if $test.Payload }}{{ if $test.Payload.Validatable }}
// Validate payload
err := {{ $test.Payload.Name }}.Validate()
if err != nil {
e, ok := err.(goa.ServiceError)
if !ok {
panic(err) // bug
}
if e.ResponseStatus() != {{ $test.Status }} {
t.Errorf("unexpected payload validation error: %+v", e)
}
{{ if $test.ReturnType }}return nil, {{ if eq $test.Status 400 }}e{{ else }}nil{{ end }}{{ else }}return nil{{ end }}
}
{{ end }}{{ end }}
// Setup request context
rw := httptest.NewRecorder()
{{ if $test.QueryParams}} query := url.Values{}
{{ range $param := $test.QueryParams }}{{ if $param.Pointer }} if {{ $param.Name }} != nil {{ end }}{
{{ template "convertParam" $param }}
query[{{ printf "%q" $param.Label }}] = sliceVal
}
{{ end }}{{ end }} u := &url.URL{
Path: fmt.Sprintf({{ printf "%q" $test.FullPath }}{{ range $param := $test.Params }}, {{ $param.Name }}{{ end }}),
{{ if $test.QueryParams }} RawQuery: query.Encode(),
{{ end }} }
req, err := http.NewRequest("{{ $test.RouteVerb }}", u.String(), nil)
if err != nil {
panic("invalid test " + err.Error()) // bug
}
prms := url.Values{}
{{ range $param := $test.Params }} prms["{{ $param.Label }}"] = []string{fmt.Sprintf("%v",{{ $param.Name}})}
{{ end }}{{ range $param := $test.QueryParams }}{{ if $param.Pointer }} if {{ $param.Name }} != nil {{ end }} {
{{ template "convertParam" $param }}
prms[{{ printf "%q" $param.Label }}] = sliceVal
}
{{ end }} if ctx == nil {
ctx = context.Background()
}
goaCtx := goa.NewContext(goa.WithAction(ctx, "{{ $test.ResourceName }}Test"), rw, req, prms)
{{ $test.ContextVarName }}, err := {{ $test.ContextType }}(goaCtx, service)
if err != nil {
panic("invalid test data " + err.Error()) // bug
}
{{ if $test.Payload }}{{ $test.ContextVarName }}.Payload = {{ $test.Payload.Name }}{{ end }}
// Perform action
err = ctrl.{{ $test.ActionName}}({{ $test.ContextVarName }})
// Validate response
if err != nil {
t.Fatalf("controller returned %s, logs:\n%s", err, logBuf.String())
}
if rw.Code != {{ $test.Status }} {
t.Errorf("invalid response status code: got %+v, expected {{ $test.Status }}", rw.Code)
}
{{ if $test.ReturnType }} var mt {{ $test.ReturnType.Pointer }}{{ $test.ReturnType.Type }}
if resp != nil {
var ok bool
mt, ok = resp.({{ $test.ReturnType.Pointer }}{{ $test.ReturnType.Type }})
if !ok {
t.Errorf("invalid response media: got %+v, expected instance of {{ $test.ReturnType.Type }}", resp)
}
{{ if $test.ReturnType.Validatable }} err = mt.Validate()
if err != nil {
t.Errorf("invalid response media type: %s", err)
}
{{ end }} }
{{ end }}
// Return results
return rw{{ if $test.ReturnType }}, mt{{ end }}
}
{{ end }}`