github.com/googleapis/api-linter@v1.65.2/rules/aip0234/request_requests_field_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 aip0234
    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 TestRequestRequestsField(t *testing.T) {
    25  	// Set up the testing permutations.
    26  	tests := []struct {
    27  		testName string
    28  		Field    string
    29  		problems testutils.Problems
    30  	}{
    31  		{
    32  			testName: "Valid",
    33  			Field:    "repeated UpdateBookRequest requests",
    34  			problems: testutils.Problems{},
    35  		},
    36  		{
    37  			testName: "Invalid-MissingRequestsField",
    38  			Field:    "string parent",
    39  			problems: testutils.Problems{{Message: `no "requests" field`}},
    40  		},
    41  		{
    42  			testName: "Invalid-RequestsFieldIsNotRepeated",
    43  			Field:    "UpdateBookRequest requests",
    44  			problems: testutils.Problems{{Message: "repeated"}},
    45  		},
    46  		{
    47  			testName: "Invalid-RequestsFieldWrongType",
    48  			Field:    "repeated int32 requests",
    49  			problems: testutils.Problems{{
    50  				Suggestion: "UpdateBookRequest",
    51  			}},
    52  		},
    53  		{
    54  			testName: "Invalid-RequestsNotRepeatedWrongType",
    55  			Field:    "int32 requests",
    56  			problems: testutils.Problems{
    57  				{Message: `The "requests" field should be repeated`},
    58  				{
    59  					Suggestion: "UpdateBookRequest",
    60  				},
    61  			},
    62  		},
    63  	}
    64  
    65  	// Run each test individually.
    66  	for _, test := range tests {
    67  		t.Run(test.testName, func(t *testing.T) {
    68  			file := testutils.ParseProto3Tmpl(t, `
    69  				message BatchUpdateBooksRequest {
    70  					{{.Field}} = 1;
    71  				}
    72  				message UpdateBookRequest {}
    73  				`, test)
    74  
    75  			// Determine the descriptor that a failing test will attach to.
    76  			var problemDesc desc.Descriptor
    77  			if requests := file.GetMessageTypes()[0].FindFieldByName("requests"); requests != nil {
    78  				problemDesc = requests
    79  			} else {
    80  				problemDesc = file.GetMessageTypes()[0]
    81  			}
    82  
    83  			problems := requestRequestsField.Lint(file)
    84  			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
    85  				t.Errorf(diff)
    86  			}
    87  		})
    88  	}
    89  }