testutil

package
v1.60.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodBeginTransaction    string = "BEGIN_TRANSACTION"
	MethodCommitTransaction   string = "COMMIT_TRANSACTION"
	MethodBatchCreateSession  string = "BATCH_CREATE_SESSION"
	MethodCreateSession       string = "CREATE_SESSION"
	MethodDeleteSession       string = "DELETE_SESSION"
	MethodGetSession          string = "GET_SESSION"
	MethodExecuteSql          string = "EXECUTE_SQL"
	MethodExecuteStreamingSql string = "EXECUTE_STREAMING_SQL"
	MethodExecuteBatchDml     string = "EXECUTE_BATCH_DML"
	MethodStreamingRead       string = "EXECUTE_STREAMING_READ"
	MethodBatchWrite          string = "BATCH_WRITE"
)

The method names that can be used to register execution times and errors.

View Source
const SelectFooFromBar = "SELECT FOO FROM BAR"

SelectFooFromBar is a SELECT statement that is added to the mocked test server and will return a one-col-two-rows result set containing the INT64 values 1 and 2.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbums = "SELECT SingerId, AlbumId, AlbumTitle FROM Albums"

SelectSingerIDAlbumIDAlbumTitleFromAlbums i a SELECT statement that is added to the mocked test server and will return a 3-cols-3-rows result set.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount int = 3

SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount is the number of cols returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement.

View Source
const SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount int64 = 3

SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount is the number of rows returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement.

View Source
const UpdateBarSetFoo = "UPDATE FOO SET BAR=1 WHERE BAZ=2"

UpdateBarSetFoo is an UPDATE statement that is added to the mocked test server that will return an update count of 5.

View Source
const UpdateBarSetFooRowCount = 5

UpdateBarSetFooRowCount is the constant update count value returned by the statement defined in UpdateBarSetFoo.

Variables

View Source
var (
	// KvMeta is the Metadata for mocked KV table.
	KvMeta = spannerpb.ResultSetMetadata{
		RowType: &spannerpb.StructType{
			Fields: []*spannerpb.StructType_Field{
				{
					Name: "Key",
					Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING},
				},
				{
					Name: "Value",
					Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING},
				},
			},
		},
	}
)

Functions

func DecodeResumeToken

func DecodeResumeToken(t []byte) (uint64, error)

DecodeResumeToken decodes a mock resume token into an uint64 integer.

func EncodeResumeToken

func EncodeResumeToken(t uint64) []byte

EncodeResumeToken return mock resume token encoding for an uint64 integer.

Types

type InMemInstanceAdminServer added in v1.2.0

type InMemInstanceAdminServer interface {
	instancepb.InstanceAdminServer
	Stop()
	Resps() []proto.Message
	SetResps([]proto.Message)
	Reqs() []proto.Message
	SetReqs([]proto.Message)
	SetErr(error)
}

InMemInstanceAdminServer contains the InstanceAdminServer interface plus a couple of specific methods for setting mocked results.

func NewInMemInstanceAdminServer added in v1.2.0

func NewInMemInstanceAdminServer() InMemInstanceAdminServer

NewInMemInstanceAdminServer creates a new in-mem test server.

type InMemSpannerServer

type InMemSpannerServer interface {
	spannerpb.SpannerServer

	// Stops this server.
	Stop()

	// Resets the in-mem server to its default state, deleting all sessions and
	// transactions that have been created on the server. Mocked results are
	// not deleted.
	Reset()

	// Sets an error that will be returned by the next server call. The server
	// call will also automatically clear the error.
	SetError(err error)

	// Puts a mocked result on the server for a specific sql statement. The
	// server does not parse the SQL string in any way, it is merely used as
	// a key to the mocked result. The result will be used for all methods that
	// expect a SQL statement, including (batch) DML methods.
	PutStatementResult(sql string, result *StatementResult) error

	// Puts a mocked result on the server for a specific partition token. The
	// result will only be used for query requests that specify a partition
	// token.
	PutPartitionResult(partitionToken []byte, result *StatementResult) error

	// Adds a PartialResultSetExecutionTime to the server that should be returned
	// for the specified SQL string.
	AddPartialResultSetError(sql string, err PartialResultSetExecutionTime)

	// Removes a mocked result on the server for a specific sql statement.
	RemoveStatementResult(sql string)

	// Aborts the specified transaction . This method can be used to test
	// transaction retry logic.
	AbortTransaction(id []byte)

	// Puts a simulated execution time for one of the Spanner methods.
	PutExecutionTime(method string, executionTime SimulatedExecutionTime)
	// Freeze stalls all requests.
	Freeze()
	// Unfreeze restores processing requests.
	Unfreeze()

	TotalSessionsCreated() uint
	TotalSessionsDeleted() uint
	SetMaxSessionsReturnedByServerPerBatchRequest(sessionCount int32)
	SetMaxSessionsReturnedByServerInTotal(sessionCount int32)

	ReceivedRequests() chan interface{}
	DumpSessions() map[string]bool
	ClearPings()
	DumpPings() []string
}

InMemSpannerServer contains the SpannerServer interface plus a couple of specific methods for adding mocked results and resetting the server.

func NewInMemSpannerServer

func NewInMemSpannerServer() InMemSpannerServer

NewInMemSpannerServer creates a new in-mem test server.

type MockedSpannerInMemTestServer

type MockedSpannerInMemTestServer struct {
	TestSpanner       InMemSpannerServer
	TestInstanceAdmin InMemInstanceAdminServer
	// contains filtered or unexported fields
}

MockedSpannerInMemTestServer is an InMemSpannerServer with results for a number of SQL statements readily mocked.

func NewMockedSpannerInMemTestServer

func NewMockedSpannerInMemTestServer(t *testing.T) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func())

NewMockedSpannerInMemTestServer creates a MockedSpannerInMemTestServer at localhost with a random port and returns client options that can be used to connect to it.

func NewMockedSpannerInMemTestServerWithAddr added in v1.2.0

func NewMockedSpannerInMemTestServerWithAddr(t *testing.T, addr string) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func())

NewMockedSpannerInMemTestServerWithAddr creates a MockedSpannerInMemTestServer at a given listening address and returns client options that can be used to connect to it.

func (*MockedSpannerInMemTestServer) CreateSingleRowSingersResult added in v1.5.0

func (s *MockedSpannerInMemTestServer) CreateSingleRowSingersResult(rowNum int64) *StatementResult

CreateSingleRowSingersResult creates a result set containing a single row of the SelectSingerIDAlbumIDAlbumTitleFromAlbums result set, or zero rows if the given rowNum is greater than the number of rows in the result set. This method can be used to mock results for different partitions of a BatchReadOnlyTransaction.

type PartialResultSetExecutionTime added in v1.1.0

type PartialResultSetExecutionTime struct {
	ResumeToken   []byte
	ExecutionTime time.Duration
	Err           error
}

PartialResultSetExecutionTime represents execution times and errors that should be used when a PartialResult at the specified resume token is to be returned.

type SimulatedExecutionTime

type SimulatedExecutionTime struct {
	MinimumExecutionTime time.Duration
	RandomExecutionTime  time.Duration
	Errors               []error
	// Keep error after execution. The error will continue to be returned until
	// it is cleared.
	KeepError bool
}

SimulatedExecutionTime represents the time the execution of a method should take, and any errors that should be returned by the method.

type StatementResult

type StatementResult struct {
	Type         StatementResultType
	Err          error
	ResultSet    *spannerpb.ResultSet
	UpdateCount  int64
	ResumeTokens [][]byte
}

StatementResult represents a mocked result on the test server. The result is either of: a ResultSet, an update count or an error.

func (*StatementResult) ToPartialResultSets added in v1.8.0

func (s *StatementResult) ToPartialResultSets(resumeToken []byte) (result []*spannerpb.PartialResultSet, err error)

ToPartialResultSets converts a ResultSet to a PartialResultSet. This method is used to convert a mocked result to a PartialResultSet when one of the streaming methods are called.

type StatementResultType

type StatementResultType int

StatementResultType indicates the type of result returned by a SQL statement.

const (
	// StatementResultError indicates that the sql statement returns an error.
	StatementResultError StatementResultType = 0
	// StatementResultResultSet indicates that the sql statement returns a
	// result set.
	StatementResultResultSet StatementResultType = 1
	// StatementResultUpdateCount indicates that the sql statement returns an
	// update count.
	StatementResultUpdateCount StatementResultType = 2
	// MaxRowsPerPartialResultSet is the maximum number of rows returned in
	// each PartialResultSet. This number is deliberately set to a low value to
	// ensure that most queries return more than one PartialResultSet.
	MaxRowsPerPartialResultSet = 1
)

Jump to

Keyboard shortcuts

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