github.com/googleapis/api-linter@v1.65.2/rules/aip0135/request_unknown_fields_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 aip0135
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc/builder"
    22  )
    23  
    24  func TestUnknownFields(t *testing.T) {
    25  	// Set up the testing permutations.
    26  	tests := []struct {
    27  		testName    string
    28  		messageName string
    29  		fieldName   string
    30  		fieldType   *builder.FieldType
    31  		problems    testutils.Problems
    32  	}{
    33  		{"Force", "DeleteBookRequest", "force", builder.FieldTypeBool(), testutils.Problems{}},
    34  		{"Etag", "DeleteBookRequest", "etag", builder.FieldTypeString(), testutils.Problems{}},
    35  		{"AllowMissing", "DeleteBookRequest", "allow_missing", builder.FieldTypeBool(), testutils.Problems{}},
    36  		{"RequestId", "DeleteBookRequest", "request_id", builder.FieldTypeString(), testutils.Problems{}},
    37  		{"ValidateOnly", "DeleteBookRequest", "validate_only", builder.FieldTypeBool(), testutils.Problems{}},
    38  		{"Invalid", "DeleteBookRequest", "application_id", builder.FieldTypeString(), testutils.Problems{{
    39  			Message: "Unexpected field",
    40  		}}},
    41  		{"Irrelevant", "RemoveBookRequest", "application_id", builder.FieldTypeString(), testutils.Problems{}},
    42  	}
    43  
    44  	// Run each test individually.
    45  	for _, test := range tests {
    46  		t.Run(test.testName, func(t *testing.T) {
    47  			// Create an appropriate message descriptor.
    48  			message, err := builder.NewMessage(test.messageName).AddField(
    49  				builder.NewField("name", builder.FieldTypeString()),
    50  			).AddField(
    51  				builder.NewField(test.fieldName, test.fieldType),
    52  			).Build()
    53  			if err != nil {
    54  				t.Fatalf("Could not build DeleteBookRequest message.")
    55  			}
    56  
    57  			// Run the lint rule, and establish that it returns the correct problems.
    58  			wantProblems := test.problems.SetDescriptor(message.FindFieldByName(test.fieldName))
    59  			gotProblems := unknownFields.Lint(message.GetFile())
    60  			if diff := wantProblems.Diff(gotProblems); diff != "" {
    61  				t.Errorf(diff)
    62  			}
    63  		})
    64  	}
    65  }