protoc-gen-go-aip-test

command module
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: MIT Imports: 2 Imported by: 0

README

protoc-gen-go-aip-test

Generate test suites for protobuf services implementing standard AIP methods.

The generated test suites are based on guidance for standard methods, and experience from implementing these methods in practice. See Suites for a list of the generated tests.

Experimental: This plugin is experimental, and breaking changes with regard to the generated tests suites should be expected.

Usage

Step 1: Declare a service with AIP standard methods
service FreightService {
  // Get a shipper.
  // See: https://google.aip.dev/131 (Standard methods: Get).
  rpc GetShipper(GetShipperRequest) returns (Shipper) {
    option (google.api.http) = {
      get: "/v1/{name=shippers/*}"
    };
    option (google.api.method_signature) = "name";
  }

  // ...
}
Step 2: Install the generator

Download a prebuilt binary from releases and put it in your PATH.

The generator can also be built from source using Go.

Step 3: Generate test suites

Include the plugin in protoc invocation

protoc
  --go-aip-test_out=[OUTPUT DIR] \
  --go-aip-test_opt=module=[OUTPUT MODULE] \
  [.proto files ...]

This can also be done via a buf generate template. See buf.gen.yaml for an example.

Step 4: Run tests

The generated test suites must be provided with some input data.

package example

func Test_FreightService(t *testing.T) {
	t.Skip("this is just an example, the service is not implemented.")
	// setup server before test
	server := examplefreightv1.UnimplementedFreightServiceServer{}
	// setup test suite
	suite := examplefreightv1.FreightServiceTestSuite{
		T:      t,
		Server: server,
	}

	// run tests for each resource in the service
	ctx := context.Background()
	suite.TestShipper(ctx, examplefreightv1.ShipperTestSuiteConfig{
		// Create should return a resource which is valid to create, i.e.
		// all required fields set.
		Create: func() *examplefreightv1.Shipper {
			return &examplefreightv1.Shipper{
				DisplayName:    "Example shipper",
				BillingAccount: "billingAccounts/12345",
			}
		},
		// Update should return a resource which is valid to update, i.e.
		// all required fields set.
		Update: func() *examplefreightv1.Shipper {
			return &examplefreightv1.Shipper{
				DisplayName:    "Updated example shipper",
				BillingAccount: "billingAccounts/54321",
			}
		},
	})
}
Skipping tests

There may be multiple reasons for an API to deviate from the guidance for standard methods (for examples see AIP-200). This plugin supports skipping individual or groups of tests using the Skip field generated for each test suite config.

Each test are compared, using strings.Contains, against a list of skipped test patterns. The full name of each test will follow the format [resource]/[method type]/[test_name].

Sample skips:

  • "Get/invalid_name" skips the "invalid name" test for Get standard method.
  • "Get" skips all tests for a Get standard method.

Suites

Create
Name Description Only if
missing parent Method should fail with InvalidArgument if no parent is provided. Generated only if all are true:
  • has Create method
  • resource has a parent
invalid parent Method should fail with InvalidArgument if provided parent is invalid. Generated only if all are true:
  • has Create method
  • resource has a parent
create time Field create_time should be populated when the resource is created. Generated only if all are true:
  • has Create method
  • Create method does not return long-running operation
  • has field 'create_time'
persisted The created resource should be persisted and reachable with Get. Generated only if all are true:
  • has Create method
  • Create method does not return long-running operation
  • has Get method
user settable id If method support user settable IDs, when set the resource should be returned with the provided ID. Generated only if all are true:
  • has Create method
  • Create method does not return long-running operation
  • has user settable ID
already exists If method support user settable IDs and the same ID is reused the method should return AlreadyExists. Generated only if all are true:
  • has Create method
  • Create method does not return long-running operation
  • has user settable ID
required fields The method should fail with InvalidArgument if the resource has any required fields and they are not provided. Generated only if all are true:
  • has Create method
  • resource has any required fields
resource references The method should fail with InvalidArgument if the resource has any resource references and they are invalid. Generated only if all are true:
  • has Create method
  • resource has any mutable resource references
Get
Name Description Only if
missing name Method should fail with InvalidArgument if no name is provided. Generated only if all are true:
  • has Get method
invalid name Method should fail with InvalidArgument if the provided name is not valid. Generated only if all are true:
  • has Get method
exists Resource should be returned without errors if it exists. Generated only if all are true:
  • has Get method
not found Method should fail with NotFound if the resource does not exist. Generated only if all are true:
  • has Get method
only wildcards Method should fail with InvalidArgument if the provided name only contains wildcards ('-') Generated only if all are true:
  • has Get method
BatchGet
Name Description Only if
invalid parent Method should fail with InvalidArgument if provided parent is invalid. Generated only if all are true:
  • resource has a parent
  • has BatchGet method
  • is not alternative batch request message
names missing Method should fail with InvalidArgument if no names are provided. Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
invalid names Method should fail with InvalidArgument if a provided name is not valid. Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
wildcard name Method should fail with InvalidArgument if a provided name only contains wildcards (-) Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
all exists Resources should be returned without errors if they exist. Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
atomic The method must be atomic; it must fail for all resources or succeed for all resources (no partial success). Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
parent mismatch If a caller sets the "parent", and the parent collection in the name of any resource being retrieved does not match, the request must fail. Generated only if all are true:
  • resource has a parent
  • has BatchGet method
  • is not alternative batch request message
ordered The order of resources in the response must be the same as the names in the request. Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
duplicate names If a caller provides duplicate names, the service should return duplicate resources. Generated only if all are true:
  • has BatchGet method
  • is not alternative batch request message
Update
Name Description Only if
missing name Method should fail with InvalidArgument if no name is provided. Generated only if all are true:
  • has Update method
invalid name Method should fail with InvalidArgument if provided name is not valid. Generated only if all are true:
  • has Update method
update time Field update_time should be updated when the resource is updated. Generated only if all are true:
  • has Create method
  • Create method does not return long-running operation
  • has Update method
  • Update method does not return long-running operation
  • has field 'update_time'
persisted The updated resource should be persisted and reachable with Get. Generated only if all are true:
  • has Update method
  • Update method does not return long-running operation
  • has Get method
preserve create_time The field create_time should be preserved when a '*'-update mask is used. Generated only if all are true:
  • has Update method
  • Update method does not return long-running operation
  • has field 'create_time'
  • resource has any required fields
not found Method should fail with NotFound if the resource does not exist. Generated only if all are true:
  • has Update method
invalid update mask The method should fail with InvalidArgument if the update_mask is invalid. Generated only if all are true:
  • has Update method
  • Update method has update_mask
required fields Method should fail with InvalidArgument if any required field is missing when called with '*' update_mask. Generated only if all are true:
  • has Update method
  • resource has any required fields
List
Name Description Only if
invalid parent Method should fail with InvalidArgument if provided parent is invalid. Generated only if all are true:
  • has List method
  • resource has a parent
invalid page token Method should fail with InvalidArgument is provided page token is not valid. Generated only if all are true:
  • has List method
negative page size Method should fail with InvalidArgument is provided page size is negative. Generated only if all are true:
  • has List method
isolation If parent is provided the method must only return resources under that parent. Generated only if all are true:
  • resource has a parent
  • has List method
  • resource has a parent
last page If there are no more resources, next_page_token should not be set. Generated only if all are true:
  • resource has a parent
  • has List method
  • resource has a parent
more pages If there are more resources, next_page_token should be set. Generated only if all are true:
  • resource has a parent
  • has List method
  • resource has a parent
one by one Listing resource one by one should eventually return all resources. Generated only if all are true:
  • resource has a parent
  • has List method
  • resource has a parent
deleted Method should not return deleted resources. Generated only if all are true:
  • resource has a parent
  • has List method
  • has Delete method
  • resource has a parent
Name Description Only if
invalid parent Method should fail with InvalidArgument if provided parent is invalid. Generated only if all are true:
  • has Search method
  • resource has a parent
invalid page token Method should fail with InvalidArgument is provided page token is not valid. Generated only if all are true:
  • has Search method
negative page size Method should fail with InvalidArgument is provided page size is negative. Generated only if all are true:
  • has Search method
isolation If parent is provided the method must only return resources under that parent. Generated only if all are true:
  • resource has a parent
  • has Search method
  • resource has a parent
last page If there are no more resources, next_page_token should not be set. Generated only if all are true:
  • resource has a parent
  • has Search method
  • resource has a parent
more pages If there are more resources, next_page_token should be set. Generated only if all are true:
  • resource has a parent
  • has Search method
  • resource has a parent
one by one Searching resource one by one should eventually return all resources. Generated only if all are true:
  • resource has a parent
  • has Search method
  • resource has a parent
deleted Method should not return deleted resources. Generated only if all are true:
  • resource has a parent
  • has Search method
  • has Delete method
  • resource has a parent
Delete
Name Description Only if
missing name Method should fail with InvalidArgument if no name is provided. Generated only if all are true:
  • has Delete method
invalid name Method should fail with InvalidArgument if the provided name is not valid. Generated only if all are true:
  • has Delete method
exists Resource should be deleted without errors if it exists. Generated only if all are true:
  • has Delete method
not found Method should fail with NotFound if the resource does not exist. Generated only if all are true:
  • has Delete method
only wildcards Method should fail with InvalidArgument if the provided name only contains wildcards ('-') Generated only if all are true:
  • has Delete method

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL