github.com/googleapis/api-linter@v1.65.2/rules/aip0148/field_behavior_test.go (about) 1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aip0148 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestFieldBehavior(t *testing.T) { 24 const messageOptsResource = `option (google.api.resource).type = "library.googleapis.com/Book";` 25 const fieldOptsOutputOnly = `[(google.api.field_behavior) = OUTPUT_ONLY]` 26 missingOutputOnly := testutils.Problems{{Message: "OUTPUT_ONLY"}} 27 28 for _, test := range []struct { 29 name string 30 MessageOpts string 31 Field string 32 FieldOpts string 33 problems testutils.Problems 34 }{ 35 {"ValidCreateTime", messageOptsResource, `google.protobuf.Timestamp create_time`, fieldOptsOutputOnly, nil}, 36 {"InvalidCreateTime", messageOptsResource, `google.protobuf.Timestamp create_time`, ``, missingOutputOnly}, 37 {"ValidDeleteTime", messageOptsResource, `google.protobuf.Timestamp delete_time`, fieldOptsOutputOnly, nil}, 38 {"InvalidDeleteTime", messageOptsResource, `google.protobuf.Timestamp delete_time`, ``, missingOutputOnly}, 39 {"ValidUid", messageOptsResource, `string uid`, fieldOptsOutputOnly, nil}, 40 {"InvalidUid", messageOptsResource, `string uid`, ``, missingOutputOnly}, 41 {"ValidUpdateTime", messageOptsResource, `google.protobuf.Timestamp update_time`, fieldOptsOutputOnly, nil}, 42 {"InvalidUpdateTime", messageOptsResource, `google.protobuf.Timestamp update_time`, ``, missingOutputOnly}, 43 {"IrrelevantNotResource", ``, `string uid`, ``, nil}, 44 {"IrrelevantField", messageOptsResource, `string name`, ``, nil}, 45 } { 46 t.Run(test.name, func(t *testing.T) { 47 file := testutils.ParseProto3Tmpl(t, ` 48 import "google/api/field_behavior.proto"; 49 import "google/api/resource.proto"; 50 import "google/protobuf/timestamp.proto"; 51 message Book { 52 {{.MessageOpts}} 53 {{.Field}} = 1 {{.FieldOpts}}; 54 } 55 `, test) 56 field := file.GetMessageTypes()[0].GetFields()[0] 57 if diff := test.problems.SetDescriptor(field).Diff(fieldBehavior.Lint(file)); diff != "" { 58 t.Error(diff) 59 } 60 }) 61 } 62 }