github.com/googleapis/api-linter@v1.65.2/rules/aip0235/request_names_field_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 aip0235
    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 TestNamesField(t *testing.T) {
    25  	// Set up the testing permutations.
    26  	tests := []struct {
    27  		testName    string
    28  		src         string
    29  		problems    testutils.Problems
    30  		problemDesc func(m *desc.MessageDescriptor) desc.Descriptor
    31  	}{
    32  		{
    33  			testName: "Valid-Names",
    34  			src: `
    35  				message BatchDeleteBooksRequest {
    36  					repeated string names = 1;
    37  				}
    38  			`,
    39  			problems: testutils.Problems{},
    40  		},
    41  		{
    42  			testName: "Valid-StandardDeleteReq",
    43  			src: `
    44  				message BatchDeleteBooksRequest {
    45  					repeated DeleteBookRequest requests = 1;
    46  				}
    47  
    48  				message DeleteBookRequest {}
    49  			`,
    50  			problems: testutils.Problems{},
    51  		},
    52  		{
    53  			testName: "Invalid-MissingNamesField",
    54  			src: `
    55  				message BatchDeleteBooksRequest {
    56  					string parent = 1;
    57  				}
    58  			`,
    59  			problems: testutils.Problems{{Message: `Message "BatchDeleteBooksRequest" has no "names" field`}},
    60  		},
    61  		{
    62  			testName: "Invalid-KeepingNamesFieldOnly",
    63  			src: `
    64  				message BatchDeleteBooksRequest {
    65  					repeated string names = 1;
    66  					repeated DeleteBookRequest requests = 2;
    67  				}
    68  
    69  				message DeleteBookRequest {}
    70  			`,
    71  			problems: testutils.Problems{{Message: `Message "BatchDeleteBooksRequest" should delete "requests" field, only keep the "names" field`}},
    72  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
    73  				return m.FindFieldByName("requests")
    74  			},
    75  		},
    76  		{
    77  			testName: "Invalid-NamesFieldIsNotRepeated",
    78  			src: `
    79  				message BatchDeleteBooksRequest {
    80  					string names = 1;
    81  				}`,
    82  			problems: testutils.Problems{{Suggestion: "repeated string"}},
    83  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
    84  				return m.FindFieldByName("names")
    85  			},
    86  		},
    87  		{
    88  			testName: "Invalid-NamesFieldWrongType",
    89  			src: `
    90  				message BatchDeleteBooksRequest {
    91  					repeated int32 names = 1;
    92  				}
    93  			`,
    94  			problems: testutils.Problems{{Suggestion: "string"}},
    95  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
    96  				return m.FindFieldByName("names")
    97  			},
    98  		},
    99  		{
   100  			testName: "Invalid-DeleteReqFieldIsNotRepeated",
   101  			src: `
   102  				message BatchDeleteBooksRequest {
   103  					DeleteBookRequest requests = 1;
   104  				}
   105  
   106  				message DeleteBookRequest {}
   107  			`,
   108  			problems: testutils.Problems{{Message: `The "requests" field should be repeated`}},
   109  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
   110  				return m.FindFieldByName("requests")
   111  			},
   112  		},
   113  		{
   114  			testName: "Invalid-DeleteReqFieldWrongType",
   115  			src: `
   116  				message BatchDeleteBooksRequest {
   117  					repeated string requests = 1;
   118  				}
   119  			`,
   120  			problems: testutils.Problems{{Message: `The "requests" field on Batch Delete Request should be a "DeleteBookRequest" type`}},
   121  			problemDesc: func(m *desc.MessageDescriptor) desc.Descriptor {
   122  				return m.FindFieldByName("requests")
   123  			},
   124  		},
   125  		{
   126  			testName: "Irrelevant-UnmatchedMessageName",
   127  			src:      `message DeleteBooksRequest {}`,
   128  			problems: testutils.Problems{},
   129  		},
   130  	}
   131  
   132  	// Run each test individually.
   133  	for _, test := range tests {
   134  		t.Run(test.testName, func(t *testing.T) {
   135  			file := testutils.ParseProto3String(t, test.src)
   136  
   137  			m := file.GetMessageTypes()[0]
   138  
   139  			// Determine the descriptor that a failing test will attach to.
   140  			var problemDesc desc.Descriptor = m
   141  			if test.problemDesc != nil {
   142  				problemDesc = test.problemDesc(m)
   143  			}
   144  
   145  			problems := requestNamesField.Lint(file)
   146  			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
   147  				t.Errorf(diff)
   148  			}
   149  		})
   150  	}
   151  }