Skip to content

Commit

Permalink
feat(update): add test for preserving create_time when "*" update mask
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwenn committed Feb 11, 2022
1 parent 670f465 commit 61d7a8a
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Suites
| invalid name | Method should fail with InvalidArgument if provided name is not valid. | Generated only if all are true: <ul><li>has Update method</li></ul> |
| update time | Field update_time should be updated when the resource is updated. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li><li>Update method does not return long-running operation</li><li>has field 'update_time'</li></ul> |
| persisted | The updated resource should be persisted and reachable with Get. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li><li>Update method does not return long-running operation</li><li>has Get method</li></ul> |
| preserve create_time | The field create_time should be preserved when a '*'-update mask is used. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li><li>Update method does not return long-running operation</li><li>has field 'create_time'</li><li>resource has any required fields</li></ul> |
| not found | Method should fail with NotFound if the resource does not exist. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li></ul> |
| invalid update mask | The method should fail with InvalidArgument if the update_mask is invalid. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li><li>Update method has update_mask</li></ul> |
| required fields | Method should fail with InvalidArgument if any required field is missing when called with '*' update_mask. | Generated only if all are true: <ul><li>has Create method</li><li>Create method does not return long-running operation</li><li>has Update method</li><li>resource has any required fields</li></ul> |
Expand Down
52 changes: 52 additions & 0 deletions internal/aiptest/update/create_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package update

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/protobuf/compiler/protogen"
)

// nolint: gochecknoglobals
var preserveCreateTime = suite.Test{
Name: "preserve create_time",
Doc: []string{
"The field create_time should be preserved when a '*'-update mask is used.",
},

OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeCreate),
onlyif.MethodNotLRO(aipreflect.MethodTypeCreate),
onlyif.HasMethod(aipreflect.MethodTypeUpdate),
onlyif.MethodNotLRO(aipreflect.MethodTypeUpdate),
onlyif.HasField("create_time"),
onlyif.HasRequiredFields,
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
createMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeCreate)
if util.HasParent(scope.Resource) {
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
}
util.MethodCreate{
Resource: scope.Resource,
Method: createMethod,
Parent: "parent",
}.Generate(f, "created", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
f.P("originalCreateTime := created.CreateTime")
updateMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeUpdate)
util.MethodUpdate{
Resource: scope.Resource,
Method: updateMethod,
Msg: "created",
UpdateMask: []string{strconv.Quote("*")},
}.Generate(f, "updated", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
f.P(ident.AssertDeepEqual, "(t, originalCreateTime, updated.CreateTime,", ident.ProtocmpTransform, "())")
return nil
},
}
1 change: 1 addition & 0 deletions internal/aiptest/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Suite = suite.Suite{
invalidName,
updateTime,
persisted,
preserveCreateTime,
},
TestGroups: []suite.TestGroup{
withResourceGroup,
Expand Down
42 changes: 42 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.

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

0 comments on commit 61d7a8a

Please sign in to comment.