Documentation ¶
Overview ¶
Package testutil contains helper functions for writing tests.
Index ¶
- func ProjID() string
- func TestIteratorNext(want interface{}, done error, next func() (interface{}, error)) (string, bool)
- func TestIteratorNextPageExact(want interface{}, done error, defaultPageSize int, ...) (string, bool)
- func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource
- type PagingIterator
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ProjID ¶
func ProjID() string
ProjID returns the project ID to use in integration tests, or the empty string if none is configured.
func TestIteratorNext ¶
func TestIteratorNext(want interface{}, done error, next func() (interface{}, error)) (string, bool)
TestIteratorNext tests the Next method of a standard client iterator (see https://github.com/GoogleCloudPlatform/google-cloud-go/wiki/Iterator-Guidelines).
This function assumes that an iterator has already been created, and that the underlying sequence to be iterated over already exists. want should be a slice that contains the elements of this sequence. It must contain at least one element.
done is the special error value that signals that the iterator has provided all the items.
next is a function that returns the result of calling Next on the iterator. It can usually be defined as
func() (interface{}, error) { return iter.Next() }
TestIteratorNext checks that the iterator returns all the elements of want in order, followed by (zero, done). It also confirms that subsequent calls to next also return (zero, done).
On success, TestIteratorNext returns ("", true). On failure, it returns a suitable error message and false.
func TestIteratorNextPageExact ¶
func TestIteratorNextPageExact(want interface{}, done error, defaultPageSize int, newIter func() PagingIterator, nextPage func(PagingIterator) (interface{}, error)) (string, bool)
TestIteratorNextPageExact tests the NextPage method of a standard client iterator with paging (see PagingIterator).
This function assumes that the underlying sequence to be iterated over already exists. want should be a slice that contains the elements of this sequence. It must contain at least three elements, in order to test non-trivial paging behavior.
done is the special error value that signals that the iterator has provided all the items.
defaultPageSize is the page size to use when the user has not called SetPageSize, or calls it with a value <= 0.
newIter should return a new iterator each time it is called.
nextPage should return the result of calling NextPage on the iterator. It can usually be defined as
func(i testutil.PagingIterator) (interface{}, error) { return i.(*<iteratorType>).NextPage() }
TestIteratorNextPageExact checks that the iterator returns all the elements of want in order, divided into pages of the exactly the right size. It confirms that if the last page is partial, done is returned along with it, and in any case, done is returned subsequently along with a zero-length slice.
On success, TestIteratorNextPageExact returns ("", true). On failure, it returns a suitable error message and false.
func TokenSource ¶
func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource
TokenSource returns the OAuth2 token source to use in integration tests, or nil if none is configured. TokenSource will log.Fatal if the token source is specified but missing or invalid.
Types ¶
type PagingIterator ¶
PagingIterator describes the standard client iterator pattern with paging as best as possible in Go. See https://github.com/GoogleCloudPlatform/google-cloud-go/wiki/Iterator-Guidelines.
type Server ¶
A Server is an in-process gRPC server, listening on a system-chosen port on the local loopback interface. Servers are for testing only and are not intended to be used in production code.
To create a server, make a new Server, register your handlers, then call Start:
srv, err := NewServer() ... mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler) .... srv.Start()
Clients should connect to the server with no security:
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure()) ...