github.com/googleapis/api-linter@v1.65.2/rules/aip0132/request_parent_reference_test.go (about) 1 // Copyright 2019 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 aip0132 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestRequestParentReference(t *testing.T) { 24 t.Run("Present", func(t *testing.T) { 25 f := testutils.ParseProto3String(t, ` 26 import "google/api/resource.proto"; 27 message ListBooksRequest { 28 string parent = 1 [(google.api.resource_reference) = { 29 type: "library.googleapis.com/Publisher" 30 }]; 31 } 32 `) 33 if diff := (testutils.Problems{}).Diff(requestParentReference.Lint(f)); diff != "" { 34 t.Errorf(diff) 35 } 36 }) 37 t.Run("Absent", func(t *testing.T) { 38 for _, test := range []struct { 39 name string 40 FieldName string 41 problems testutils.Problems 42 }{ 43 {"Error", "parent", testutils.Problems{{Message: "google.api.resource_reference"}}}, 44 {"Irrelevant", "something_else", testutils.Problems{}}, 45 } { 46 t.Run(test.name, func(t *testing.T) { 47 f := testutils.ParseProto3Tmpl(t, ` 48 import "google/api/resource.proto"; 49 message ListBooksRequest { 50 string {{.FieldName}} = 1; 51 } 52 `, test) 53 field := f.GetMessageTypes()[0].GetFields()[0] 54 if diff := test.problems.SetDescriptor(field).Diff(requestParentReference.Lint(f)); diff != "" { 55 t.Errorf(diff) 56 } 57 }) 58 } 59 }) 60 }