Skip to content

Commit 61d7a8a

Browse files
committed
feat(update): add test for preserving create_time when "*" update mask
1 parent 670f465 commit 61d7a8a

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Suites
139139
| 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> |
140140
| 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> |
141141
| 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> |
142+
| 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> |
142143
| 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> |
143144
| 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> |
144145
| 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> |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package update
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/einride/protoc-gen-go-aip-test/internal/ident"
7+
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
8+
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
9+
"github.com/einride/protoc-gen-go-aip-test/internal/util"
10+
"go.einride.tech/aip/reflect/aipreflect"
11+
"google.golang.org/protobuf/compiler/protogen"
12+
)
13+
14+
// nolint: gochecknoglobals
15+
var preserveCreateTime = suite.Test{
16+
Name: "preserve create_time",
17+
Doc: []string{
18+
"The field create_time should be preserved when a '*'-update mask is used.",
19+
},
20+
21+
OnlyIf: suite.OnlyIfs(
22+
onlyif.HasMethod(aipreflect.MethodTypeCreate),
23+
onlyif.MethodNotLRO(aipreflect.MethodTypeCreate),
24+
onlyif.HasMethod(aipreflect.MethodTypeUpdate),
25+
onlyif.MethodNotLRO(aipreflect.MethodTypeUpdate),
26+
onlyif.HasField("create_time"),
27+
onlyif.HasRequiredFields,
28+
),
29+
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
30+
createMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeCreate)
31+
if util.HasParent(scope.Resource) {
32+
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
33+
}
34+
util.MethodCreate{
35+
Resource: scope.Resource,
36+
Method: createMethod,
37+
Parent: "parent",
38+
}.Generate(f, "created", "err", ":=")
39+
f.P(ident.AssertNilError, "(t, err)")
40+
f.P("originalCreateTime := created.CreateTime")
41+
updateMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeUpdate)
42+
util.MethodUpdate{
43+
Resource: scope.Resource,
44+
Method: updateMethod,
45+
Msg: "created",
46+
UpdateMask: []string{strconv.Quote("*")},
47+
}.Generate(f, "updated", "err", ":=")
48+
f.P(ident.AssertNilError, "(t, err)")
49+
f.P(ident.AssertDeepEqual, "(t, originalCreateTime, updated.CreateTime,", ident.ProtocmpTransform, "())")
50+
return nil
51+
},
52+
}

internal/aiptest/update/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Suite = suite.Suite{
1818
invalidName,
1919
updateTime,
2020
persisted,
21+
preserveCreateTime,
2122
},
2223
TestGroups: []suite.TestGroup{
2324
withResourceGroup,

proto/gen/einride/example/freight/v1/freight_service_aiptest.pb.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/gen/googleapis/cloud/aiplatform/v1/tensorboard_service_aiptest.pb.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)