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 ¶
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") )
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.