github.com/googleapis/api-linter@v1.65.2/rules/aip0123/resource_singular_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  package aip0123
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/googleapis/api-linter/rules/internal/testutils"
    20  )
    21  
    22  func TestResourceSingular(t *testing.T) {
    23  	for _, test := range []struct {
    24  		name     string
    25  		Singular string
    26  		problems testutils.Problems
    27  	}{
    28  		{
    29  			"Valid",
    30  			`singular: "book"`,
    31  			nil,
    32  		},
    33  		{
    34  			"InvalidDoesntMatchType",
    35  			`singular: "shelf"`,
    36  			testutils.Problems{{
    37  				Message: "book",
    38  			}},
    39  		},
    40  		{
    41  			"InvalidMissing",
    42  			``,
    43  			testutils.Problems{{
    44  				Message: "Resources should declare singular",
    45  			}},
    46  		},
    47  	} {
    48  		t.Run(test.name, func(t *testing.T) {
    49  			f := testutils.ParseProto3Tmpl(t, `
    50  			import "google/api/resource.proto";
    51  			message Book {
    52  				option (google.api.resource) = {
    53  					type: "library.googleapis.com/Book"
    54  					pattern: "publishers/{publisher}/books/{book}"
    55  					{{.Singular}}
    56  				};
    57  				string name = 1;
    58  			}
    59  			`, test)
    60  			m := f.GetMessageTypes()[0]
    61  			if diff := test.problems.SetDescriptor(m).Diff(resourceSingular.Lint(f)); diff != "" {
    62  				t.Error(diff)
    63  			}
    64  		})
    65  	}
    66  }