github.com/googleapis/api-linter@v1.65.2/rules/aip0235/request_parent_field_test.go (about) 1 // Copyright 2020 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 aip0235 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 "github.com/jhump/protoreflect/desc" 22 ) 23 24 func TestParentField(t *testing.T) { 25 for _, test := range []struct { 26 name string 27 Package string 28 RPC string 29 Field string 30 Pattern string 31 problems testutils.Problems 32 }{ 33 {"Valid", "", "BatchDeleteBooks", "string parent = 1;", "publishers/{p}/books/{b}", nil}, 34 {"Missing", "", "BatchDeleteBooks", "", "publishers/{p}/books/{b}", testutils.Problems{{Message: "no `parent`"}}}, 35 {"InvalidType", "", "BatchDeleteBooks", "int32 parent = 1;", "publishers/{p}/books/{b}", testutils.Problems{{Suggestion: "string"}}}, 36 {"IrrelevantRPCName", "", "EnumerateBooks", "", "publishers/{p}/books/{b}", nil}, 37 {"IrrelevantNoParent", "", "BatchDeleteBooks", "", "books/{b}", nil}, 38 39 {"PackageValid", "package foo;", "BatchDeleteBooks", "string parent = 1;", "publishers/{p}/books/{b}", nil}, 40 {"PackageMissing", "package foo;", "BatchDeleteBooks", "", "publishers/{p}/books/{b}", testutils.Problems{{Message: "no `parent`"}}}, 41 {"PackageInvalidType", "package foo;", "BatchDeleteBooks", "int32 parent = 1;", "publishers/{p}/books/{b}", testutils.Problems{{Suggestion: "string"}}}, 42 {"PackageIrrelevantRPCName", "package foo;", "EnumerateBooks", "", "publishers/{p}/books/{b}", nil}, 43 {"PackageIrrelevantNoParent", "package foo;", "BatchDeleteBooks", "", "books/{b}", nil}, 44 } { 45 t.Run(test.name, func(t *testing.T) { 46 f := testutils.ParseProto3Tmpl(t, ` 47 {{.Package}} 48 import "google/api/resource.proto"; 49 import "google/protobuf/empty.proto"; 50 51 service Library { 52 rpc {{.RPC}}({{.RPC}}Request) returns (google.protobuf.Empty); 53 } 54 55 message {{.RPC}}Request { 56 {{.Field}} 57 } 58 59 message Book { 60 option (google.api.resource) = { 61 pattern: "{{.Pattern}}"; 62 }; 63 } 64 `, test) 65 var d desc.Descriptor = f.GetMessageTypes()[0] 66 if test.name == "InvalidType" || test.name == "PackageInvalidType" { 67 d = f.GetMessageTypes()[0].GetFields()[0] 68 } 69 if diff := test.problems.SetDescriptor(d).Diff(requestParentField.Lint(f)); diff != "" { 70 t.Errorf(diff) 71 } 72 }) 73 } 74 }