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

     1  // Copyright 2023 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  )
    22  
    23  func TestForceField(t *testing.T) {
    24  	tests := []struct {
    25  		name      string
    26  		Reference string
    27  		BoolField string
    28  		problems  testutils.Problems
    29  	}{
    30  		{"ValidWithChildren", `.type = "library.googleapis.com/Publisher"`, "force", nil},
    31  		{"ValidWithoutChildren", `.type = "library.googleapis.com/Book"`, "other", nil},
    32  		{"SkipIncorrectChildTypeReference", `.child_type = "library.googleapis.com/Publisher"`, "other", nil},
    33  		{"InvalidMissingForce", `.type = "library.googleapis.com/Publisher"`, "other", testutils.Problems{{Message: "bool force"}}},
    34  	}
    35  
    36  	for _, test := range tests {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			f := testutils.ParseProto3Tmpl(t, `
    39  				import "google/api/resource.proto";
    40  
    41  				message Book {
    42  					option (google.api.resource) = {
    43  						type: "library.googleapis.com/Book"
    44  						pattern: "publishers/{publisher}/books/{book}"
    45  					};
    46  	
    47  					string name = 1;
    48  				}
    49  
    50  				message Publisher {
    51  					option (google.api.resource) = {
    52  						type: "library.googleapis.com/Publisher"
    53  						pattern: "publishers/{publisher}"
    54  					};
    55  	
    56  					string name = 1;
    57  				}
    58  
    59  				message DeleteResourceRequest {
    60  					string name = 1 [(google.api.resource_reference){{.Reference}}];
    61  
    62  					bool {{.BoolField}} = 2;
    63  				}
    64  			`, test)
    65  			msg := f.FindMessage("DeleteResourceRequest")
    66  			problems := forceField.Lint(f)
    67  			if diff := test.problems.SetDescriptor(msg).Diff(problems); diff != "" {
    68  				t.Errorf("Problems did not match: %v", diff)
    69  			}
    70  		})
    71  	}
    72  }