github.com/googleapis/api-linter@v1.65.2/rules/aip0134/request_resource_required_test.go (about)

     1  package aip0134
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/googleapis/api-linter/rules/internal/testutils"
     7  )
     8  
     9  func TestRequestResourceFieldRequired(t *testing.T) {
    10  	// Set up the testing permutations.
    11  	tests := []struct {
    12  		name              string
    13  		MessageName       string
    14  		ResourceName      string
    15  		ResourceFieldName string
    16  		problems          testutils.Problems
    17  	}{
    18  		{"Valid", "UpdateBookRequest", "Book", "book", nil},
    19  		{"ValidTwoWords", "UpdateBigBookRequest", "BigBook", "big_book", nil},
    20  		{"InvalidMismatch", "UpdateBookRequest", "Foo", "foo", testutils.Problems{{Message: "has no \"Book\""}}},
    21  	}
    22  
    23  	// Run each test individually.
    24  	for _, test := range tests {
    25  		t.Run(test.name, func(t *testing.T) {
    26  			file := testutils.ParseProto3Tmpl(t, `
    27  				import "google/protobuf/field_mask.proto";
    28  				message {{.MessageName}} {
    29  					{{.ResourceName}} {{.ResourceFieldName}} = 1;
    30  				}
    31  				message {{.ResourceName}} {}
    32  			`, test)
    33  			message := file.GetMessageTypes()[0]
    34  			problems := requestResourceRequired.Lint(file)
    35  			if diff := test.problems.SetDescriptor(message).Diff(problems); diff != "" {
    36  				t.Errorf(diff)
    37  			}
    38  		})
    39  	}
    40  }