googlecloudgotesting

package module
v0.0.0-...-1c9a4c6 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

Testing Support for the Google Cloud Client Libraries for Go

Status

This project is no longer being maintained. If you would like to see patternes you can use to test against the Go client libraries please see the links below:

Install

go get github.com/googleapis/google-cloud-go-testing

This repository contains code that can help you test against the Cloud Client Libraries for Go.

If you plan to use this code for mocks, we recommend you read Don't Overuse Mocks first.

NOTE: This code has alpha status. It may be subject to backwards-incompatible changes.

Documentation

Overview

These packages contain code that can help you test against the GCP Client Libraries for Go (https://github.com/GoogleCloudPlatform/google-cloud-go).

We do not recommend using mocks for most testing. Please read https://testing.googleblog.com/2013/05/testing-on-toilet-dont-overuse-mocks.html.

Note: These packages are in alpha. Some backwards-incompatible changes may occur.

Embedding Interfaces

All interfaces in this package include an embedToIncludeNewMethods method. This is intentionally unexported so that any implementor of the interface must embed the interface in their implementation. Embedding the interface in an implementation has the effect that any future methods added to the interface will not cause compile-time errors (the implementation does not implement the newly-added method), since embedded interfaces provide a default method for unimplemented methods.

See Example (RecordBuckets) for an example of how to implement interfaces (including embedding the interface).

Example (RecordBuckets)
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/storage"
	"github.com/googleapis/google-cloud-go-testing/storage/stiface"
)

type RecordingClient struct {
	stiface.Client
	bucketCalls int
}

func (rc *RecordingClient) Bucket(name string) stiface.BucketHandle {
	rc.bucketCalls++
	return rc.Client.Bucket(name)
}

// We do not need to implement methods that we don't want to record - by default
// the embedded type will be used.

func main() {
	// This example demonstrates building a simple mock that counts the number
	// of Bucket calls before calling the real client and returning its output.

	ctx := context.Background()
	c, err := storage.NewClient(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	client := stiface.AdaptClient(c)
	recordingClient := RecordingClient{client, 0}

	recordingClient.Bucket("my-bucket-1")
	recordingClient.Bucket("my-bucket-2")
	recordingClient.Bucket("my-bucket-3")

	fmt.Println(recordingClient.bucketCalls)
}
Output:

3

Directories

Path Synopsis
bigquery
bqiface
Package bqiface provides a set of interfaces for the types in cloud.google.com/go/bigquery.
Package bqiface provides a set of interfaces for the types in cloud.google.com/go/bigquery.
datastore
dsiface
Package dsiface provides a set of interfaces for the types in cloud.google.com/go/datastore.
Package dsiface provides a set of interfaces for the types in cloud.google.com/go/datastore.
pubsub
psiface
Package psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub.
Package psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub.
storage
stiface
Package stiface provides a set of interfaces for the types in cloud.google.com/go/storage.
Package stiface provides a set of interfaces for the types in cloud.google.com/go/storage.

Jump to

Keyboard shortcuts

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