Skip to content

Commit

Permalink
feat(get): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwenn committed Aug 11, 2021
1 parent 6033e87 commit d865ad2
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/plugin/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ func (r *resourceGenerator) generateParentMethods(f *protogen.GeneratedFile) {
func (r *resourceGenerator) collectTestCases() []testCase {
return []testCase{
r.createTestCase(),
r.getTestCase(),
}
}
119 changes: 119 additions & 0 deletions internal/plugin/testcase_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package plugin

import (
"strconv"

"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/protobuf/compiler/protogen"
)

func (r *resourceGenerator) getTestCase() testCase {
getMethod, ok := r.standardMethod(aipreflect.MethodTypeGet)
if !ok {
return disabledTestCase()
}
createMethod, ok := r.standardMethod(aipreflect.MethodTypeCreate)
if !ok {
return disabledTestCase()
}

return newTestCase("Get", func(f *protogen.GeneratedFile) {
testingT := f.QualifiedGoIdent(protogen.GoIdent{GoName: "T", GoImportPath: "testing"})
assertEqual := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "Equal",
GoImportPath: "gotest.tools/v3/assert",
})
assertDeepEqual := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "DeepEqual",
GoImportPath: "gotest.tools/v3/assert",
})
assertNilError := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "NilError",
GoImportPath: "gotest.tools/v3/assert",
})
protocmpTransform := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "Transform",
GoImportPath: "google.golang.org/protobuf/testing/protocmp",
})
statusCode := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "Code",
GoImportPath: "google.golang.org/grpc/status",
})
codesInvalidArgument := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "InvalidArgument",
GoImportPath: "google.golang.org/grpc/codes",
})
codesNotFound := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "NotFound",
GoImportPath: "google.golang.org/grpc/codes",
})

f.P("// Standard methods: Get")
f.P("// https://google.aip.dev/131")

if hasParent(r.resource) {
f.P()
f.P("parent := fx.nextParent(t, false)")
}
methodCreate{
resource: r.resource,
method: createMethod,
parent: "parent",
}.Generate(f, "created00", "err", ":=")
f.P(assertNilError, "(t, err)")

f.P()
f.P("// Method should fail with InvalidArgument if no name is provided.")
f.P("t.Run(\"missing name\", func(t *", testingT, ") {")
f.P("fx.maybeSkip(t)")
methodGet{
resource: r.resource,
method: getMethod,
name: strconv.Quote(""),
}.Generate(f, "_", "err", ":=")
f.P(assertEqual, "(t, ", codesInvalidArgument, ",", statusCode, "(err), err)")
f.P("})")

f.P()
f.P("// Method should fail with InvalidArgument is provided name is not valid.")
f.P("t.Run(\"invalid name\", func(t *", testingT, ") {")
f.P("fx.maybeSkip(t)")
methodGet{
resource: r.resource,
method: getMethod,
name: strconv.Quote("invalid resource name"),
}.Generate(f, "_", "err", ":=")
f.P(assertEqual, "(t, ", codesInvalidArgument, ",", statusCode, "(err), err)")
f.P("})")

f.P()
f.P("// Resource should be returned without errors if it exists.")
f.P("t.Run(\"exists\", func(t *", testingT, ") {")
f.P("fx.maybeSkip(t)")
methodGet{
resource: r.resource,
method: getMethod,
name: "created00.Name",
}.Generate(f, "msg", "err", ":=")
f.P(assertNilError, "(t, err)")
f.P(assertDeepEqual, "(t, msg, created00, ", protocmpTransform, "())")
f.P("})")

f.P()
f.P("// Method should fail with NotFound if the resource does not exist.")
f.P("t.Run(\"not found\", func(t *", testingT, ") {")
f.P("fx.maybeSkip(t)")
methodGet{
resource: r.resource,
method: getMethod,
// appending to the resource name ensures it is valid
name: "created00.Name + \"notfound\"",
}.Generate(f, "_", "err", ":=")
f.P(assertEqual, "(t, ", codesNotFound, ",", statusCode, "(err), err)")
f.P("})")

// TODO: add test for supplying wildcard as name

f.P("_ = ", codesNotFound)
})
}
99 changes: 99 additions & 0 deletions proto/gen/einride/example/freight/v1/testing/freight_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d865ad2

Please sign in to comment.