github.com/googleapis/api-linter@v1.65.2/rules/aip0231/plural_method_name_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 aip0231
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestPluralMethodResourceName(t *testing.T) {
    24  	// Set up the testing permutations.
    25  	tests := []struct {
    26  		testName       string
    27  		MethodName     string
    28  		CollectionName string
    29  		problems       testutils.Problems
    30  	}{
    31  		{
    32  			testName:       "ValidBatchGetBooks",
    33  			MethodName:     "BatchGetBooks",
    34  			CollectionName: "books",
    35  			problems:       testutils.Problems{},
    36  		},
    37  		{
    38  			testName:       "ValidBatchGetMen",
    39  			MethodName:     "BatchGetMen",
    40  			CollectionName: "men",
    41  			problems:       testutils.Problems{},
    42  		},
    43  		{
    44  			testName:       "InvalidSingularBus",
    45  			MethodName:     "BatchGetBus",
    46  			CollectionName: "buses",
    47  			problems:       testutils.Problems{{Message: "Buses"}},
    48  		},
    49  		{
    50  			testName:       "Invalid-SingularCorpPerson",
    51  			MethodName:     "BatchGetCorpPerson",
    52  			CollectionName: "corpPerson",
    53  			problems:       testutils.Problems{{Message: "CorpPeople"}},
    54  		},
    55  		{
    56  			testName:       "Invalid-Irrelevant",
    57  			MethodName:     "GetBook",
    58  			CollectionName: "books",
    59  			problems:       testutils.Problems{},
    60  		},
    61  	}
    62  
    63  	// Run each test individually.
    64  	for _, test := range tests {
    65  		t.Run(test.testName, func(t *testing.T) {
    66  			file := testutils.ParseProto3Tmpl(t, `
    67  				import "google/api/annotations.proto";
    68  
    69  				service Test {
    70  					rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {
    71  						option (google.api.http) = {
    72  							get: "/v1/{parent=publishers/*}/{{.CollectionName}}:batchGet"
    73  						};
    74  					}
    75  				}
    76  
    77  				message {{.MethodName}}Request {}
    78  
    79  				message {{.MethodName}}Response {}
    80  			`, test)
    81  
    82  			m := file.GetServices()[0].GetMethods()[0]
    83  
    84  			problems := pluralMethodResourceName.Lint(file)
    85  			if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
    86  				t.Errorf(diff)
    87  			}
    88  		})
    89  	}
    90  }