github.com/googleapis/api-linter@v1.65.2/rules/aip4232/required_fields_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 aip4232 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestRequiredFields(t *testing.T) { 24 tests := []struct { 25 testName string 26 Signature string 27 FieldBehavior string 28 problems testutils.Problems 29 }{ 30 {"Valid", "name,paperback_only,shelf", "REQUIRED", nil}, 31 {"ValidNested", "name,paperback_only,shelf.name", "REQUIRED", nil}, 32 {"ValidNotRequired", "paperback_only", "OPTIONAL", nil}, 33 {"Invalid", "paperback_only", "REQUIRED", testutils.Problems{{Message: "missing at least one"}}}, 34 } 35 36 for _, test := range tests { 37 t.Run(test.testName, func(t *testing.T) { 38 f := testutils.ParseProto3Tmpl(t, ` 39 import "google/api/client.proto"; 40 import "google/api/field_behavior.proto"; 41 service Library { 42 rpc ArchiveBook(ArchiveBookRequest) returns (ArchiveBookResponse) { 43 option (google.api.method_signature) = "{{.Signature}}"; 44 } 45 } 46 message ArchiveBookRequest { 47 string name = 1 [(google.api.field_behavior) = {{.FieldBehavior}}]; 48 49 bool paperback_only = 2; 50 51 Shelf shelf = 3 [(google.api.field_behavior) = {{.FieldBehavior}}]; 52 } 53 message ArchiveBookResponse {} 54 message Shelf { 55 string name = 1; 56 } 57 `, test) 58 method := f.GetServices()[0].GetMethods()[0] 59 if diff := test.problems.SetDescriptor(method).Diff(requiredFields.Lint(f)); diff != "" { 60 t.Error(diff) 61 } 62 }) 63 } 64 }