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

     1  package aip0132
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/googleapis/api-linter/rules/internal/testutils"
     7  )
     8  
     9  func TestRequestParentRequired(t *testing.T) {
    10  	tests := []struct {
    11  		name        string
    12  		MessageName string
    13  		FieldName   string
    14  		problems    testutils.Problems
    15  	}{
    16  		{"Valid", "ListBooksRequest", "parent", nil},
    17  		{"InvalidName", "ListBooksRequest", "publisher", testutils.Problems{{Message: "no `parent` field"}}},
    18  		{"Irrelevant", "EnumerateBooksRequest", "id", nil},
    19  		{"IrrelevantAIP162", "ListBookRevisionsRequest", "name", nil},
    20  	}
    21  
    22  	for _, test := range tests {
    23  		t.Run(test.name, func(t *testing.T) {
    24  			f := testutils.ParseProto3Tmpl(t, `
    25  				message {{.MessageName}} {
    26  					string {{.FieldName}} = 1;
    27  				}
    28  			`, test)
    29  			problems := requestParentRequired.Lint(f)
    30  			if diff := test.problems.SetDescriptor(f.GetMessageTypes()[0]).Diff(problems); diff != "" {
    31  				t.Errorf(diff)
    32  			}
    33  		})
    34  	}
    35  
    36  	// Test the "top-level exception", which is more involved
    37  	// than the other tests and therefore handled separately.
    38  	for _, test := range []struct {
    39  		testName string
    40  		Package  string
    41  	}{
    42  		{"ValidTopLevel", ""},
    43  		{"ValidTopLevelWithPackage", "package foo;"},
    44  	} {
    45  		t.Run(test.testName, func(t *testing.T) {
    46  			f := testutils.ParseProto3Tmpl(t, `
    47  				{{.Package}}
    48  				import "google/api/resource.proto";
    49  				message ListBooksRequest {}
    50  				message ListBooksResponse {
    51  					repeated Book books = 1;
    52  				}
    53  				message Book {
    54  					option (google.api.resource) = {
    55  						pattern: "books/{book}"
    56  					};
    57  					string name = 1;
    58  				}
    59  			`, test)
    60  			problems := requestParentRequired.Lint(f)
    61  			if diff := (testutils.Problems{}).Diff(problems); diff != "" {
    62  				t.Errorf(diff)
    63  			}
    64  		})
    65  	}
    66  }