github.com/googleapis/api-linter@v1.65.2/rules/aip0203/resource_name_identifier_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 aip0203 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestResourceNameIdentifier(t *testing.T) { 24 testCases := []struct { 25 name string 26 Field, NameField string 27 problems testutils.Problems 28 }{ 29 { 30 name: "Valid", 31 Field: "string name = 1 [(google.api.field_behavior) = IDENTIFIER];", 32 problems: nil, 33 }, 34 { 35 name: "ValidNameField", 36 Field: "string resource_name = 1 [(google.api.field_behavior) = IDENTIFIER];", 37 NameField: "resource_name", 38 problems: nil, 39 }, 40 { 41 name: "SkipMissingNameField", 42 Field: "string other = 1;", 43 problems: nil, 44 }, 45 { 46 name: "InvalidNoFieldBehavior", 47 Field: "string name = 1;", 48 problems: testutils.Problems{{Message: "field_behavior IDENTIFIER"}}, 49 }, 50 { 51 name: "InvalidMissingIdentifier", 52 Field: "string name = 1 [(google.api.field_behavior) = REQUIRED];", 53 problems: testutils.Problems{{Message: "field_behavior IDENTIFIER"}}, 54 }, 55 } 56 57 for _, test := range testCases { 58 t.Run(test.name, func(t *testing.T) { 59 file := testutils.ParseProto3Tmpl(t, ` 60 import "google/api/field_behavior.proto"; 61 import "google/api/resource.proto"; 62 message Book { 63 option (google.api.resource) = { 64 type: "library.googleapis.com/Book" 65 pattern: "books/{book}" 66 name_field: "{{.NameField}}" 67 }; 68 69 {{.Field}} 70 }`, test) 71 f := file.GetMessageTypes()[0].GetFields()[0] 72 problems := resourceNameIdentifier.Lint(file) 73 if diff := test.problems.SetDescriptor(f).Diff(problems); diff != "" { 74 t.Error(diff) 75 } 76 }) 77 } 78 }