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

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