mockspanner

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 17 Imported by: 0

README

Mock Google Cloud Spanner

Tests goreportcard

Provides a mock of Google Cloud Spanner using the official Google Cloud Spanner Emulator.

These mocks will automatically create a new emulator, wait for it to be available, then create a mock database. You will need to run your database migrations prior to performing your tests.

I recommend reusing the instance across multiple tests to reduce test run times.

This library is built on top of testcontainers.

Usage

Creating a mock instance for creating a customer connection.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockspanner.NewInstance(ctx, t)

	if err != nil {
		t.Fatalf("creating the instance: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

	// ... my test code
}

Creating a mock Spanner client for interacting with Spanner via the Go client.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockspanner.NewClient(ctx, t)

	if err != nil {
		t.Fatalf("creating the client: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

    spannerClient := mock.Client()

    t.Run("MyTest1", func(t *testing.T) {
        // ... my test code
    })

    t.Run("MyTest2", func(t *testing.T) {
        // ... my test code
    })
}

Documentation

Index

Constants

View Source
const (
	ProjectID  = "test-project-id"
	InstanceID = "test-instance-id"
	DatabaseID = "test-database-id"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents the underlying container that is running the mock Spanner instance. It includes all of the clients that are needed to interact with the instance, database, and user client.

func NewClient

func NewClient(ctx context.Context, t *testing.T) (*Client, error)

func (*Client) Client

func (r *Client) Client() *spanner.Client

Client returns the underlying Spanner client.

func (*Client) Close

func (r *Client) Close(ctx context.Context)

Close closes the underlying Spanner clients and the instance.

func (*Client) DatabaseAdmin

func (r *Client) DatabaseAdmin() *databaseAdmin.DatabaseAdminClient

Client returns the underlying Spanner Database Admin client.

func (*Client) GRPC

func (r *Client) GRPC() *grpc.ClientConn

GRPC fetches the connection to the underlying Spanner gRPC server.

func (*Client) InstanceAdmin

func (r *Client) InstanceAdmin() *instanceAdmin.InstanceAdminClient

Client returns the underlying Spanner Instance Admin client.

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance represents the underlying container that is running the mock Spanner instance.

func NewInstance

func NewInstance(ctx context.Context, t *testing.T) (*Instance, error)

func (*Instance) Close

func (r *Instance) Close(ctx context.Context)

Close terminates the underlying container.

func (*Instance) Port

func (r *Instance) Port(ctx context.Context) (nat.Port, error)

Port returns the mapped port of the underlying container.

Jump to

Keyboard shortcuts

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