spanemuboost

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 17 Imported by: 1

README

SPANner EMUlator BOOtSTrapper

Go Reference

spanemuboost bootstraps Cloud Spanner Emulator and client with no required configuration using testcontainers-go.

It inspired by autoConfigEmulator of:

This package doesn't have functionality of splitting statements and stripping comments. Consider to use memefish or helper packages.

Examples

Simple setup

spanemuboost.NewEmulatorWithclients() can be used without required configurations.

func ExampleNewEmulatorWithClients() {
    ctx := context.Background()

    _, clients, teardown, err := spanemuboost.NewEmulatorWithClients(ctx)
    if err != nil {
        log.Fatalln(err)
        return
    }

    defer teardown()

    err = clients.Client.Single().Query(ctx, spanner.NewStatement("SELECT 1")).Do(func(r *spanner.Row) error {
        fmt.Println(r)
        // Output: {fields: [type:{code:INT64}], values: [string_value:"1"]}
		
        return nil
    })
    if err != nil {
        log.Fatalln(err)
    }
}
Multiple databases setup

spanemuboost supports more practical setup as recommended by Cloud Spanner Emulator FAQ

What is the recommended test setup? Use a single emulator process and create a Cloud Spanner instance within it. Since creating databases is cheap in the emulator, we recommend that each test bring up and tear down its own database. This ensures hermetic testing and allows the test suite to run tests in parallel if needed.

func ExampleNewEmulatorAndNewClients() {
    ctx := context.Background()

    emulator, emulatorTeardown, err := spanemuboost.NewEmulator(ctx,
        spanemuboost.EnableInstanceAutoConfigOnly(),
    )
    if err != nil {
        log.Fatalln(err)
        return
    }

    defer emulatorTeardown()

    var pks []int64
    for i := 0; i < 10; i++ {
        func() {
            clients, clientsTeardown, err := spanemuboost.NewClients(ctx, emulator,
                spanemuboost.EnableDatabaseAutoConfigOnly(),
                spanemuboost.WithRandomDatabaseID(),
                spanemuboost.WithSetupDDLs([]string{"CREATE TABLE tbl (PK INT64 PRIMARY KEY)"}),
                spanemuboost.WithSetupDMLs([]spanner.Statement{
                    {SQL: "INSERT INTO tbl(PK) VALUES(@i)", Params: map[string]any{"i": i}},
                }),
            )
            if err != nil {
                log.Fatalln(err)
                return
            }

            defer clientsTeardown()

            err = clients.Client.Single().Query(ctx, spanner.NewStatement("SELECT PK FROM tbl")).Do(func(r *spanner.Row) error {
                var pk int64
                if err := r.ColumnByName("PK", &pk); err != nil {
                    return err
                }
                pks = append(pks, pk)
                return nil
            })
            if err != nil {
                log.Fatalln(err)
            }
        }()
    }

    fmt.Println(pks)
    // Output: [0 1 2 3 4 5 6 7 8 9]
}

Documentation

Index

Examples

Constants

View Source
const (
	DefaultEmulatorImage = "gcr.io/cloud-spanner-emulator/emulator:1.5.28"
	DefaultProjectID     = "emulator-project"
	DefaultInstanceID    = "emulator-instance"
	DefaultDatabaseID    = "emulator-database"
)

Variables

This section is empty.

Functions

func NewEmulator

func NewEmulator(ctx context.Context, options ...Option) (emulator *gcloud.GCloudContainer, teardown func(), err error)

NewEmulator initializes Cloud Spanner Emulator. The emulator will be closed when teardown is called. You should call it.

Types

type Clients

type Clients struct {
	InstanceClient *instance.InstanceAdminClient
	DatabaseClient *database.DatabaseAdminClient
	Client         *spanner.Client

	ProjectID, InstanceID, DatabaseID string
}

Clients struct is container of Spanner clients.

func NewClients added in v0.2.0

func NewClients(ctx context.Context, emulator *gcloud.GCloudContainer, options ...Option) (clients *Clients, teardown func(), err error)

NewClients setup existing Cloud Spanner Emulator with Spanner clients. The clients will be closed when teardown is called. You should call it.

func NewEmulatorWithClients

func NewEmulatorWithClients(ctx context.Context, options ...Option) (emulator *gcloud.GCloudContainer, clients *Clients, teardown func(), err error)

NewEmulatorWithClients initializes Cloud Spanner Emulator with Spanner clients. The emulator and clients will be closed when teardown is called. You should call it.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/spanner"

	"github.com/apstndb/spanemuboost"
)

func main() {
	ctx := context.Background()

	_, clients, teardown, err := spanemuboost.NewEmulatorWithClients(ctx)
	if err != nil {
		log.Fatalln(err)
		return
	}

	defer teardown()

	err = clients.Client.Single().Query(ctx, spanner.NewStatement("SELECT 1")).Do(func(r *spanner.Row) error {
		fmt.Println(r)

		return nil
	})
	if err != nil {
		log.Fatalln(err)
	}
}
Output:

{fields: [type:{code:INT64}], values: [string_value:"1"]}

func (*Clients) DatabasePath added in v0.2.2

func (c *Clients) DatabasePath() string

func (*Clients) InstancePath added in v0.2.2

func (c *Clients) InstancePath() string

func (*Clients) ProjectPath added in v0.2.2

func (c *Clients) ProjectPath() string

type Option

type Option func(*emulatorOptions) error

func DisableAutoConfig

func DisableAutoConfig() Option

DisableAutoConfig disables auto config.(default enable)

func EnableAutoConfig

func EnableAutoConfig() Option

EnableAutoConfig enables auto config.(default enable)

func EnableDatabaseAutoConfigOnly added in v0.2.0

func EnableDatabaseAutoConfigOnly() Option

func EnableFaultInjection added in v0.2.5

func EnableFaultInjection() Option

EnableFaultInjection enables fault injection of Cloud Spanner Emulator.

func EnableInstanceAutoConfigOnly added in v0.2.0

func EnableInstanceAutoConfigOnly() Option

func WithClientConfig added in v0.2.0

func WithClientConfig(config spanner.ClientConfig) Option

WithClientConfig sets spanner.ClienConfig for NewClients and NewEmulatorWithClients.

func WithClientOptionsForClient added in v0.2.5

func WithClientOptionsForClient(option ...option.ClientOption) Option

WithClientOptionsForClient configures ClientOption for Clients.Client.

func WithContainerCustomizers added in v0.2.5

func WithContainerCustomizers(containerCustomizers ...testcontainers.ContainerCustomizer) Option

WithContainerCustomizers sets any testcontainers.ContainerCustomizer

func WithDatabaseDialect

func WithDatabaseDialect(dialect databasepb.DatabaseDialect) Option

WithDatabaseDialect configures the database dialect.

func WithDatabaseID

func WithDatabaseID(databaseID string) Option

WithDatabaseID configures the database ID. Empty string resets to default.

func WithEmulatorImage

func WithEmulatorImage(image string) Option

WithEmulatorImage configures the Spanner Emulator container image. Empty string will be ignored.

func WithInstanceID

func WithInstanceID(instanceID string) Option

WithInstanceID configures the instance ID. Empty string resets to default.

func WithProjectID

func WithProjectID(projectID string) Option

WithProjectID configures the project ID. Empty string resets to default.

func WithRandomDatabaseID added in v0.2.3

func WithRandomDatabaseID() Option

WithRandomDatabaseID enables the random database ID. Default is disabled.

func WithRandomInstanceID added in v0.2.4

func WithRandomInstanceID() Option

WithRandomInstanceID enables the random instance ID. Default is disabled.

func WithRandomProjectID added in v0.2.4

func WithRandomProjectID() Option

WithRandomProjectID enables the random project ID. Default is disabled.

func WithSetupDDLs

func WithSetupDDLs(ddls []string) Option

WithSetupDDLs sets DDLs to be executed. Note: comments are not permitted.

func WithSetupDMLs

func WithSetupDMLs(dmls []spanner.Statement) Option

WithSetupDMLs sets DMLs in spanner.Statement to be executed.

func WithSetupRawDMLs

func WithSetupRawDMLs(rawDMLs []string) Option

WithSetupRawDMLs sets string DMLs to be executed.

func WithoutRandomDatabaseID added in v0.2.4

func WithoutRandomDatabaseID() Option

WithoutRandomDatabaseID disables the random database ID. Default is disabled.

func WithoutRandomInstanceID added in v0.2.4

func WithoutRandomInstanceID() Option

WithoutRandomInstanceID disables the random instance ID. Default is disabled.

func WithoutRandomProjectID added in v0.2.4

func WithoutRandomProjectID() Option

WithoutRandomProjectID disables the random project ID. Default is disabled.

Jump to

Keyboard shortcuts

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