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

     1  // Copyright 2020 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 aip0233
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  func TestParentField(t *testing.T) {
    25  	for _, test := range []struct {
    26  		name     string
    27  		Package  string
    28  		RPC      string
    29  		Field    string
    30  		Pattern  string
    31  		problems testutils.Problems
    32  	}{
    33  		{"Valid", "", "BatchCreateBooks", "string parent = 1;", "publishers/{p}/books/{b}", nil},
    34  		{"Missing", "", "BatchCreateBooks", "", "publishers/{p}/books/{b}", testutils.Problems{{Message: "no `parent`"}}},
    35  		{"InvalidType", "", "BatchCreateBooks", "int32 parent = 1;", "publishers/{p}/books/{b}", testutils.Problems{{Suggestion: "string"}}},
    36  		{"IrrelevantRPCName", "", "EnumerateBooks", "", "publishers/{p}/books/{b}", nil},
    37  		{"IrrelevantNoParent", "", "BatchCreateBooks", "", "books/{b}", nil},
    38  
    39  		{"PackageValid", "package foo;", "BatchCreateBooks", "string parent = 1;", "publishers/{p}/books/{b}", nil},
    40  		{"PackageMissing", "package foo;", "BatchCreateBooks", "", "publishers/{p}/books/{b}", testutils.Problems{{Message: "no `parent`"}}},
    41  		{"PackageInvalidType", "package foo;", "BatchCreateBooks", "int32 parent = 1;", "publishers/{p}/books/{b}", testutils.Problems{{Suggestion: "string"}}},
    42  		{"PackageIrrelevantRPCName", "package foo;", "EnumerateBooks", "", "publishers/{p}/books/{b}", nil},
    43  		{"PackageIrrelevantNoParent", "package foo;", "BatchCreateBooks", "", "books/{b}", nil},
    44  	} {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			f := testutils.ParseProto3Tmpl(t, `
    47  				{{.Package}}
    48  				import "google/api/resource.proto";
    49  
    50  				service Library {
    51  					rpc {{.RPC}}({{.RPC}}Request) returns ({{.RPC}}Response);
    52  				}
    53  
    54  				message {{.RPC}}Request {
    55  					{{.Field}}
    56  					repeated string names = 2;
    57  				}
    58  
    59  				message {{.RPC}}Response {
    60  					repeated Book books = 1;
    61  				}
    62  
    63  				message Book {
    64  					option (google.api.resource) = {
    65  						pattern: "{{.Pattern}}";
    66  					};
    67  					string name = 1;
    68  				}
    69  			`, test)
    70  			var d desc.Descriptor = f.GetMessageTypes()[0]
    71  			if test.name == "InvalidType" || test.name == "PackageInvalidType" {
    72  				d = f.GetMessageTypes()[0].GetFields()[0]
    73  			}
    74  			if diff := test.problems.SetDescriptor(d).Diff(requestParentField.Lint(f)); diff != "" {
    75  				t.Errorf(diff)
    76  			}
    77  		})
    78  	}
    79  }