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

     1  // Copyright 2022 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 aip0122
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestEmbeddedResource(t *testing.T) {
    24  	for _, test := range []struct {
    25  		name           string
    26  		FieldA, FieldB string
    27  		problems       testutils.Problems
    28  	}{
    29  		{"Valid", "", "", nil},
    30  		{
    31  			"ValidReferences",
    32  			`string library = 2 [(google.api.resource_reference).type = "library.googleapis.com/Library"];`,
    33  			`string librarian = 3 [(google.api.resource_reference).type = "library.googleapis.com/Librarian"];`,
    34  			nil,
    35  		},
    36  		{
    37  			"InvalidEmbeddedResources",
    38  			"Library library = 2;",
    39  			"Librarian librarian = 3;",
    40  			testutils.Problems{
    41  				{
    42  					Message:    "not by embedding",
    43  					Suggestion: `string library = 2 [(google.api.resource_reference).type = "library.googleapis.com/Library"];`,
    44  				},
    45  				{
    46  					Message:    "not by embedding",
    47  					Suggestion: `string librarian = 3 [(google.api.resource_reference).type = "library.googleapis.com/Librarian"];`,
    48  				},
    49  			},
    50  		},
    51  		{
    52  			"InvalidRepeatedEmbeddedResource",
    53  			"repeated Library library = 2;",
    54  			"",
    55  			testutils.Problems{{
    56  				Message:    "not by embedding",
    57  				Suggestion: `repeated string library = 2 [(google.api.resource_reference).type = "library.googleapis.com/Library"];`,
    58  			}},
    59  		},
    60  	} {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			f := testutils.ParseProto3Tmpl(t, `
    63  			import "google/api/resource.proto";
    64  			message Book {
    65  				option (google.api.resource) = {
    66  					type: "library.googleapis.com/Book"
    67  					pattern: "publishers/{publisher}/books/{book}"
    68  				};
    69  				string name = 1;
    70  
    71  				{{.FieldA}}
    72  
    73  				{{.FieldB}}
    74  			}
    75  
    76  			message Library {
    77  				option (google.api.resource) = {
    78  					type: "library.googleapis.com/Library"
    79  					pattern: "libraries/{library}"
    80  				};
    81  				string name = 1;
    82  			}
    83  
    84  			message Librarian {
    85  				option (google.api.resource) = {
    86  					type: "library.googleapis.com/Librarian"
    87  					pattern: "libraries/{library}/librarians/{librarian}"
    88  				};
    89  				string name = 1;
    90  			}
    91  		`, test)
    92  			m := f.FindMessage("Book")
    93  
    94  			want := test.problems
    95  			if len(want) > 0 {
    96  				want[0].Descriptor = m.FindFieldByName("library")
    97  			}
    98  			if len(want) > 1 {
    99  				want[1].Descriptor = m.FindFieldByName("librarian")
   100  			}
   101  			if diff := want.Diff(embeddedResource.Lint(f)); diff != "" {
   102  				t.Errorf(diff)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestEmbeddedResource_Revisions(t *testing.T) {
   109  	for _, test := range []struct {
   110  		name         string
   111  		SnapshotType string
   112  		problems     testutils.Problems
   113  	}{
   114  		{"Valid", "Book", nil},
   115  		{
   116  			"InvalidEmbeddedResource",
   117  			"Library",
   118  			testutils.Problems{{
   119  				Message:    "not by embedding",
   120  				Suggestion: `string snapshot = 2 [(google.api.resource_reference).type = "library.googleapis.com/Library"];`,
   121  			}},
   122  		},
   123  	} {
   124  		t.Run(test.name, func(t *testing.T) {
   125  			f := testutils.ParseProto3Tmpl(t, `
   126  			import "google/api/resource.proto";
   127  			message Book {
   128  				option (google.api.resource) = {
   129  					type: "library.googleapis.com/Book"
   130  					pattern: "publishers/{publisher}/books/{book}"
   131  				};
   132  				string name = 1;
   133  			}
   134  
   135  			message BookRevision {
   136  				option (google.api.resource) = {
   137  					type: "library.googleapis.com/BookRevision"
   138  					pattern: "publishers/{publisher}/books/{book}/revisions/{revision}"
   139  				};
   140  				string name = 1;
   141  
   142  				{{.SnapshotType}} snapshot = 2;
   143  			}
   144  
   145  			message Library {
   146  				option (google.api.resource) = {
   147  					type: "library.googleapis.com/Library"
   148  					pattern: "libraries/{library}"
   149  				};
   150  				string name = 1;
   151  			}
   152  		`, test)
   153  			field := f.FindMessage("BookRevision").FindFieldByName("snapshot")
   154  			if diff := test.problems.SetDescriptor(field).Diff(embeddedResource.Lint(f)); diff != "" {
   155  				t.Errorf(diff)
   156  			}
   157  		})
   158  	}
   159  }