fake

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Example (AdminClient)
package main

import (
	"context"
	"log"

	admindatabasev1 "cloud.google.com/go/spanner/admin/database/apiv1"

	databasepb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
	"github.com/gcpug/handy-spanner/fake"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	dbName := "projects/fake/instances/fake/databases/fake"
	stmts := []string{
		`CREATE TABLE Table1 ( Id STRING(MAX) NOT NULL ) PRIMARY KEY (Id)`,
		`CREATE TABLE Table2 ( Id TIMESTAMP NOT NULL ) PRIMARY KEY (Id)`,
	}

	// Run fake server
	srv, conn, err := fake.Run()
	if err != nil {
		log.Fatal(err)
	}
	defer srv.Stop()

	// Prepare spanner client
	adminclient, err := admindatabasev1.NewDatabaseAdminClient(ctx, option.WithGRPCConn(conn))
	if err != nil {
		log.Fatalf("failed to connect fake spanner server: %v", err)
	}

	// Use the client
	_, err = adminclient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
		Database:   dbName,
		Statements: stmts,
	})
	if err != nil {
		log.Fatal(err)
	}

}
Example (ApplyDDL)
package main

import (
	"context"
	"log"
	"strings"

	"github.com/gcpug/handy-spanner/fake"
)

func main() {
	ctx := context.Background()
	dbName := "projects/fake/instances/fake/databases/fake"
	schema := `
CREATE TABLE Table1 ( Id STRING(MAX) NOT NULL ) PRIMARY KEY (Id);
CREATE TABLE Table2 ( Id TIMESTAMP NOT NULL ) PRIMARY KEY (Id);
`

	// Run fake server
	srv, _, err := fake.Run()
	if err != nil {
		log.Fatal(err)
	}
	defer srv.Stop()

	err = srv.ParseAndApplyDDL(ctx, dbName, strings.NewReader(schema))
	if err != nil {
		log.Fatal(err)
	}

}
Example (SpannerClient)
package main

import (
	"context"
	"log"

	"cloud.google.com/go/spanner"
	"github.com/gcpug/handy-spanner/fake"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	dbName := "projects/fake/instances/fake/databases/fake"

	// Run fake server
	srv, conn, err := fake.Run()
	if err != nil {
		log.Fatal(err)
	}
	defer srv.Stop()
	defer conn.Close()

	// Prepare spanner client
	client, err := spanner.NewClient(ctx, dbName, option.WithGRPCConn(conn))
	if err != nil {
		log.Fatalf("failed to connect fake spanner server: %v", err)
	}

	// Use the client
	_, _ = client.Apply(ctx, []*spanner.Mutation{
		spanner.Insert("Test",
			[]string{"ColA", "ColB"},
			[]interface{}{"foo", 100},
		),
	})

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

func New

func New(lis net.Listener) (*Server, error)

New returns Server for fake spanner.

func Run

func Run() (*Server, *grpc.ClientConn, error)

func (*Server) Addr

func (s *Server) Addr() string

func (*Server) ApplyDDL

func (s *Server) ApplyDDL(ctx context.Context, databaseName string, ddl []ast.DDL) error

func (*Server) ParseAndApplyDDL

func (s *Server) ParseAndApplyDDL(ctx context.Context, databaseName string, r io.Reader) error

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop()

Jump to

Keyboard shortcuts

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