github.com/googleapis/api-linter@v1.65.2/rules/aip0134/request_allow_missing_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 aip0134
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestAllowMissing(t *testing.T) {
    24  	const singletonPattern = `books/{book}/settings`
    25  	const nonSingletonPattern = `books/{book}`
    26  	problems := testutils.Problems{{Message: "include a singular `bool allow_missing`"}}
    27  	for _, test := range []struct {
    28  		name         string
    29  		Style        string
    30  		Pattern      string
    31  		AllowMissing string
    32  		problems     testutils.Problems
    33  	}{
    34  		{"IgnoredNotDF", "", nonSingletonPattern, "", nil},
    35  		{"ValidIncluded", "style: DECLARATIVE_FRIENDLY", nonSingletonPattern, "bool allow_missing = 2;", nil},
    36  		{"Invalid", "style: DECLARATIVE_FRIENDLY", nonSingletonPattern, "", problems},
    37  		{"InvalidWrongType", "style: DECLARATIVE_FRIENDLY", nonSingletonPattern, "string allow_missing = 2;", problems},
    38  		{"InvalidRepeated", "style: DECLARATIVE_FRIENDLY", nonSingletonPattern, "repeated bool allow_missing = 2;", problems},
    39  		{"IgnoredSingleton", "style: DECLARATIVE_FRIENDLY", singletonPattern, "", nil},
    40  	} {
    41  		t.Run(test.name, func(t *testing.T) {
    42  			f := testutils.ParseProto3Tmpl(t, `
    43  				import "google/api/resource.proto";
    44  
    45  				service Library {
    46  					rpc UpdateBook(UpdateBookRequest) returns (Book);
    47  				}
    48  
    49  				message UpdateBookRequest {
    50  					Book book = 1;
    51  					{{.AllowMissing}}
    52  				}
    53  
    54  				message Book {
    55  					option (google.api.resource) = {
    56  						type: "library.googleapis.com/Book"
    57  						pattern: "{{.Pattern}}"
    58  						{{.Style}}
    59  					};
    60  				}
    61  			`, test)
    62  			m := f.GetMessageTypes()[0]
    63  			if diff := test.problems.SetDescriptor(m).Diff(allowMissing.Lint(f)); diff != "" {
    64  				t.Error(diff)
    65  			}
    66  		})
    67  	}
    68  }