github.com/googleapis/api-linter@v1.65.2/rules/aip0158/request_page_size_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 aip0158
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  	"github.com/jhump/protoreflect/desc"
    23  	"github.com/jhump/protoreflect/desc/builder"
    24  )
    25  
    26  func TestRequestPaginationPageSize(t *testing.T) {
    27  	// Set up the testing permutations.
    28  	tests := []struct {
    29  		testName      string
    30  		messageName   string
    31  		messageFields []field
    32  		isOneof       bool
    33  		problems      testutils.Problems
    34  		problemDesc   func(m *desc.MessageDescriptor) desc.Descriptor
    35  	}{
    36  		{
    37  			"Valid",
    38  			"ListFooRequest",
    39  			[]field{{"page_size", builder.FieldTypeInt32()}, {"page_token", builder.FieldTypeString()}},
    40  			false,
    41  			testutils.Problems{},
    42  			nil,
    43  		},
    44  		{
    45  			"MissingField",
    46  			"ListFooRequest",
    47  			[]field{{"page_token", builder.FieldTypeString()}},
    48  			false,
    49  			testutils.Problems{{Message: "page_size"}},
    50  			nil,
    51  		},
    52  		{
    53  			"InvalidType",
    54  			"ListFooRequest",
    55  			[]field{{"page_size", builder.FieldTypeDouble()}},
    56  			false,
    57  			testutils.Problems{{Suggestion: "int32"}},
    58  			func(m *desc.MessageDescriptor) desc.Descriptor {
    59  				return m.FindFieldByName("page_size")
    60  			},
    61  		},
    62  		{
    63  			"IrrelevantMessage",
    64  			"ListFooPageToken",
    65  			[]field{{"page_token", builder.FieldTypeString()}},
    66  			false,
    67  			nil,
    68  			nil,
    69  		},
    70  		{
    71  			"InvalidIsOneof",
    72  			"ListFooRequest",
    73  			[]field{{"page_size", builder.FieldTypeInt32()}},
    74  			/* isOneof */ true,
    75  			testutils.Problems{{Message: "oneof"}},
    76  			func(m *desc.MessageDescriptor) desc.Descriptor {
    77  				return m.FindFieldByName("page_size")
    78  			},
    79  		},
    80  	}
    81  
    82  	// Run each test individually.
    83  	for _, test := range tests {
    84  		t.Run(test.testName, func(t *testing.T) {
    85  			// Create an appropriate message descriptor.
    86  			messageBuilder := builder.NewMessage(test.messageName)
    87  
    88  			for _, f := range test.messageFields {
    89  				fb := builder.NewField(f.fieldName, f.fieldType)
    90  				if test.isOneof {
    91  					messageBuilder.AddOneOf(builder.NewOneOf(fmt.Sprintf("%s_oneof", f.fieldName)).AddChoice(fb))
    92  				} else {
    93  					messageBuilder.AddField(fb)
    94  				}
    95  			}
    96  
    97  			message, err := messageBuilder.Build()
    98  			if err != nil {
    99  				t.Fatalf("Could not build %s message.", test.messageName)
   100  			}
   101  
   102  			// Determine the descriptor that a failing test will attach to.
   103  			var problemDesc desc.Descriptor = message
   104  			if test.problemDesc != nil {
   105  				problemDesc = test.problemDesc(message)
   106  			}
   107  
   108  			// Run the lint rule, and establish that it returns the correct problems.
   109  			problems := requestPaginationPageSize.Lint(message.GetFile())
   110  			if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" {
   111  				t.Error(diff)
   112  			}
   113  		})
   114  	}
   115  }