Documentation ¶
Index ¶
- Constants
- Variables
- func IsConnQueryDisabled() bool
- func IsOpen() bool
- func IsRecording() bool
- func Open(t testingT) io.Closer
- func OpenNamed(t testingT, pathName, recordingName string) io.Closer
- func OpenSource(t testingT, source Source, recordingName string) io.Closer
- func Register(driverName string)
- func SetSessionInit(callback SessionInitCallback)
- type SessionInitCallback
- type Source
Constants ¶
const ( DriverOpen recordType ConnExec ConnPrepare ConnQuery ConnBegin StmtNumInput StmtExec StmtQuery TxCommit TxRollback ResultLastInsertId ResultRowsAffected RowsColumns RowsNext )
This is a list of the event types, which correspond 1:1 with SQL driver methods.
Variables ¶
var MaxRecordingSize = 1024 * 1024
MaxRecordingSize is the maximum size, in bytes, of a single recording in its text format.
Functions ¶
func IsConnQueryDisabled ¶
func IsConnQueryDisabled() bool
IsConnQueryDisabled returns true if usage of ConnQuery is disabled.
func IsOpen ¶
func IsOpen() bool
IsOpen is true if a recording or playback session is currently in progress. That is, Open or OpenNamed has been called, but Close has not yet been called. This is useful when some tests use copyist and some don't, and testing utility code wants to automatically determine whether to open a connection using the copyist driver or the "real" driver.
func IsRecording ¶
func IsRecording() bool
IsRecording returns true if copyist is currently in recording mode.
func Open ¶
Open begins a recording or playback session, depending on the value of the "record" command-line flag. If recording, then all calls to registered drivers will be recorded and then saved in a copyist recording file that sits alongside the calling test file. If playing back, then the recording will be fetched from that recording file. Here is a typical calling pattern:
func init() { copyist.Register("postgres") } func TestMyStuff(t *testing.T) { defer copyist.Open(t).Close() ... }
The call to Open will initiate a new recording session. The deferred call to Close will complete the recording session and write the recording to a file in the testdata/ directory, like:
mystuff_test.go testdata/ mystuff_test.copyist
Each test or sub-test that needs to be executed independently needs to record its own session.
func OpenNamed ¶
OpenNamed is a variant of Open which accepts a caller-specified pathName and recordingName rather than deriving default values for them. The given pathName will be used as the name of the output file containing the recordings rather than the default "_test.copyist" file in the testdata directory. The given recordingName will be used as the recording name in that file rather than using the testing.T.Name() value.
func OpenSource ¶
OpenSource is a variant of Open which accepts a caller-specified source and recordingName rather than deriving default values for them. The given source will be used to persist and load recordings rather than the default "_test.copyist" file in the testdata directory. The given recordingName will be used as the recording name in that file rather than using the testing.T.Name() value.
func Register ¶
func Register(driverName string)
Register constructs a proxy driver that wraps the "real" driver of the given name. Depending on the value of the "record" command-line flag, the constructed proxy will either record calls to the wrapped driver, or else play back calls that were previously recorded. Register must be called before copyist.Open can be called, typically in an init() method. Note that the wrapped driver is lazily fetched from the `sql` package, so if a driver of that name does not exist, an error will not be raised until a connection is opened for the first time.
The Register method takes the name of the SQL driver to be wrapped (e.g. "postgres"). Below is an example of how copyist.Register should be invoked.
copyist.Register("postgres")
Note that Register can only be called once for a given driver; subsequent attempts will fail with an error. In addition, the same copyist driver must be used with playback as was was used during recording.
func SetSessionInit ¶
func SetSessionInit(callback SessionInitCallback)
SetSessionInit sets the callback function that will be invoked at the beginning of each copyist session. This can be used to initialize the test database to a clean, well-known state.
NOTE: The callback is only invoked in "recording" mode. There is no need to call it in "playback" mode, as the database is not actually accessed at that time.
Types ¶
type SessionInitCallback ¶
type SessionInitCallback func()
SessionInitCallback types a function that is invoked once per session for each driver, when in recording mode, in order to initialize the database to a clean, well-known state.
type Source ¶
type Source interface { // ReadAll reads the underlying resource this Source represents and returns // the contents. ReadAll() ([]byte, error) // WriteAll persists the given data to the underlying resource this Source // represents. WriteAll([]byte) error }
Source represents a persistent copyist recording source, generally a file on disk.