Skip to content

Commit

Permalink
feat: implement Delete endpoint tests
Browse files Browse the repository at this point in the history
Implements basic Delete endpoint tests, identical to the Get endpoint
tests.
  • Loading branch information
radhus committed Sep 27, 2022
1 parent fc9a6ca commit bae5597
Show file tree
Hide file tree
Showing 28 changed files with 2,103 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,14 @@ Suites
| one by one | Searching resource one by one should eventually return all resources. | Generated only if all are true: <ul><li>resource has a parent</li><li>has Search method</li><li>resource has a parent</li></ul> |
| deleted | Method should not return deleted resources. | Generated only if all are true: <ul><li>resource has a parent</li><li>has Search method</li><li>has Delete method</li><li>resource has a parent</li></ul> |

### Delete

| Name | Description | Only if |
|----------------|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| missing name | Method should fail with InvalidArgument if no name is provided. | Generated only if all are true: <ul><li>has Delete method</li></ul> |
| invalid name | Method should fail with InvalidArgument if the provided name is not valid. | Generated only if all are true: <ul><li>has Delete method</li></ul> |
| exists | Resource should be deleted without errors if it exists. | Generated only if all are true: <ul><li>has Delete method</li></ul> |
| not found | Method should fail with NotFound if the resource does not exist. | Generated only if all are true: <ul><li>has Delete method</li></ul> |
| only wildcards | Method should fail with InvalidArgument if the provided name only contains wildcards ('-') | Generated only if all are true: <ul><li>has Delete method</li></ul> |

<!-- END suites -->
18 changes: 18 additions & 0 deletions internal/aiptest/deletion/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package deletion

import (
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
)

// Suite of Delete tests.
//nolint: gochecknoglobals
var Suite = suite.Suite{
Name: "Delete",
Tests: []suite.Test{
missingName,
invalidName,
exists,
notFound,
wildcardName,
},
}
39 changes: 39 additions & 0 deletions internal/aiptest/deletion/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package deletion

import (
"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint: gochecknoglobals
var exists = suite.Test{
Name: "exists",
Doc: []string{
"Resource should be deleted without errors if it exists.",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeDelete),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)

if util.HasParent(scope.Resource) {
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
f.P("created := fx.create(t, parent)")
} else {
f.P("created := fx.create(t)")
}
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
Name: "created.Name",
}.Generate(f, "_", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
return nil
},
}
35 changes: 35 additions & 0 deletions internal/aiptest/deletion/name_invalid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package deletion

import (
"strconv"

"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint: gochecknoglobals
var invalidName = suite.Test{
Name: "invalid name",
Doc: []string{
"Method should fail with InvalidArgument if the provided name is not valid.",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeDelete),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
Name: strconv.Quote("invalid resource name"),
}.Generate(f, "_", "err", ":=")
f.P(ident.AssertEqual, "(t, ", ident.Codes(codes.InvalidArgument), ",", ident.StatusCode, "(err), err)")
return nil
},
}
35 changes: 35 additions & 0 deletions internal/aiptest/deletion/name_missing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package deletion

import (
"strconv"

"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint: gochecknoglobals
var missingName = suite.Test{
Name: "missing name",
Doc: []string{
"Method should fail with InvalidArgument if no name is provided.",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeDelete),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
Name: strconv.Quote(""),
}.Generate(f, "_", "err", ":=")
f.P(ident.AssertEqual, "(t, ", ident.Codes(codes.InvalidArgument), ",", ident.StatusCode, "(err), err)")
return nil
},
}
41 changes: 41 additions & 0 deletions internal/aiptest/deletion/not_found.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package deletion

import (
"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint: gochecknoglobals
var notFound = suite.Test{
Name: "not found",
Doc: []string{
"Method should fail with NotFound if the resource does not exist.",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeDelete),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)

if util.HasParent(scope.Resource) {
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
f.P("created := fx.create(t, parent)")
} else {
f.P("created := fx.create(t)")
}
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
// appending to the resource name ensures it is valid
Name: "created.Name + \"notfound\"",
}.Generate(f, "_", "err", ":=")
f.P(ident.AssertEqual, "(t, ", ident.Codes(codes.NotFound), ",", ident.StatusCode, "(err), err)")
return nil
},
}
35 changes: 35 additions & 0 deletions internal/aiptest/deletion/wildcard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package deletion

import (
"strconv"

"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint: gochecknoglobals
var wildcardName = suite.Test{
Name: "only wildcards",
Doc: []string{
"Method should fail with InvalidArgument if the provided name only contains wildcards ('-')",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeDelete),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
Name: strconv.Quote(util.WildcardResourceName(scope.Resource)),
}.Generate(f, "_", "err", ":=")
f.P(ident.AssertEqual, "(t, ", ident.Codes(codes.InvalidArgument), ",", ident.StatusCode, "(err), err)")
return nil
},
}
2 changes: 2 additions & 0 deletions internal/aiptest/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aiptest
import (
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/batchget"
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/create"
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/deletion"
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/get"
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/list"
"github.com/einride/protoc-gen-go-aip-test/internal/aiptest/search"
Expand All @@ -19,4 +20,5 @@ var Suites = []suite.Suite{
update.Suite,
list.Suite,
search.Suite,
deletion.Suite,
}
106 changes: 106 additions & 0 deletions proto/gen/einride/example/freight/v1/freight_service_aiptest.pb.go

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

Loading

0 comments on commit bae5597

Please sign in to comment.