Package spannertest contains test helpers for working with Cloud Spanner.
This package is EXPERIMENTAL, and is lacking several features. See the README.md file in this directory for more details.
This package has an in-memory fake implementation of spanner. To use it, create a Server, and then connect to it with no security:
srv, err := spannertest.NewServer("localhost:0") ... conn, err := grpc.DialContext(ctx, srv.Addr, grpc.WithInsecure()) ... client, err := spanner.NewClient(ctx, db, option.WithGRPCConn(conn)) ...
Alternatively, create a Server, then set the SPANNER_EMULATOR_HOST environment variable and use the regular spanner.NewClient:
srv, err := spannertest.NewServer("localhost:0") ... os.Setenv("SPANNER_EMULATOR_HOST", srv.Addr) client, err := spanner.NewClient(ctx, db) ...
The same server also supports database admin operations for use with the cloud.google.com/go/spanner/admin/database/apiv1 package. This only simulates the existence of a single database; its name is ignored.
Logger is something that can be used for logging. It is matched by log.Printf and testing.T.Logf.
Server is an in-memory Cloud Spanner fake. It is unauthenticated, non-performant, and only a rough approximation.
NewServer creates a new Server. The Server will be listening for gRPC connections, without TLS, on the provided TCP address. The resolved address is available in the Addr field.
Close shuts down the server.
SetLogger sets a logger for the server. You can use a *testing.T as this argument to collate extra information from the execution of the server.
UpdateDDL applies the given DDL to the server.
This is a convenience method for tests that may assume an existing schema. The more general approach is to dial this server using an admin client, and use the UpdateDatabaseDdl RPC method.