fakevtsql

package
v0.15.10 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package fakevtsql provides an interface for mocking out sql.DB responses in tests that depend on a vtsql.DB instance.

To use fakevtsql, you will need to create a discovery implementation that does not error, e.g. with fakediscovery:

disco := fakediscovery.New()
disco.AddTaggedGates(nil, []*vtadminpb.VTGate{Hostname: "gate"})

Then, you will call vtsql.New(), passing the faked discovery implementation into the config:

		db := vtsql.New(&vtsql.Config{
         Cluster: &vtadminpb.Cluster{Id: "cid", Name: "cluster"},
			Discovery: disco,
		})

Finally, with your instantiated VTGateProxy instance, you can mock out the DialFunc to always return a fakevtsql.Connector. The Tablets and ShouldErr attributes of the connector control the behavior:

db.DialFunc = func(cfg vitessdriver.Configuration) (*sql.DB, error) {
	return sql.OpenDB(&fakevtsql.Connector{
		Tablets: mockTablets,
		ShouldErr: shouldErr,
	})
}
cluster := &cluster.Cluster{
	/* other attributes */
	DB: db,
}

go/vt/vtadmin/api_test.go has several examples of usage.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnClosed is returend when attempting to query a closed connection.
	// It is the identical message to vtsql.ErrConnClosed, but redefined to
	// prevent an import cycle in package vtsql's tests.
	ErrConnClosed = errors.New("use of closed connection")
	// ErrUnrecognizedQuery is returned when QueryCnotext is given a query
	// string the mock is not set up to handle.
	ErrUnrecognizedQuery = errors.New("unrecognized query")
)
View Source
var (
	// ErrBadRow is returned from Next() when a row has an incorrect number of
	// fields.
	ErrBadRow = errors.New("bad sql row")
	// ErrRowsClosed is returned when attempting to operate on an already-closed
	// Rows.
	ErrRowsClosed = errors.New("err rows closed")
)

Functions

This section is empty.

Types

type Connector

type Connector struct {
	Tablets []*vtadminpb.Tablet
	// (TODO:@amason) - allow distinction between Query errors and errors on
	// Rows operations (e.g. Next, Err, Scan).
	ShouldErr bool
}

Connector implements the driver.Connector interface, providing a sql-like thing that can respond to vtadmin vtsql queries with mocked data.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect is part of the driver.Connector interface.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver is part of the driver.Connector interface.

Jump to

Keyboard shortcuts

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