db

package
v0.0.0-...-4c78a19 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: GPL-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package db contains test helper functions dealing with a database.

Index

Constants

This section is empty.

Variables

View Source
var (
	DbType = "sqlite3"
	DbName = ":memory:"
)

Functions

func DbWithSetupFile

func DbWithSetupFile(filename string) (*sql.DB, error)

DbWithSetupFile creates a new database and executes SQL commands from the given file.

func DbWithSetupString

func DbWithSetupString(setupSql string) (*sql.DB, error)

DbWithSetupFile creates a new database and executes SQL commands from the given string.

func EmptyDb

func EmptyDb() (*sql.DB, error)

EmptyDb creates an empty database from the values in our variables DbType and DbName.

func ExecMulti

func ExecMulti(db *sql.DB, sql string) error

ExecMulti executes multiple sql statements from a string. It strips out all carriage returns, breaks the string into segments at double-newlines, removes lines starting with a "#" (as comments), and separately executes each segment. If any segment returns an error, it stop executing and returns that error.

func ExecSegment

func ExecSegment(db *sql.DB, segment string) error

ExecSegment executes a single sql statement from a string. It removes lines starting with "#" (as comments).

func LoadSetupFile

func LoadSetupFile(db *sql.DB, filename string) error

LoadSetupFile reads and executes SQL commands from the specified file.

func LoadSetupString

func LoadSetupString(db *sql.DB, setupSql string) error

LoadSetupString reads and executes SQL commands from the given string.

Types

type Tester

type Tester struct {
	base.Tester

	// Base name for the test setup file; if not set, uses BaseName.
	SetupBaseName string
	// Path to the test setup file; if not set, uses SetupBaseName.
	SetupPath string

	DB *sql.DB
}

Tester provides the structure for running unit tests with database setup files.

func NewTester

func NewTester(basename string, callback func(*sql.DB, io.Writer) error) *Tester

NewTester creates a new instance of a Tester that will call the specified callback as the test function.

func (*Tester) Arrange

func (r *Tester) Arrange() error

Arrange prepares the output file and loads the setup file.

func (*Tester) Close

func (r *Tester) Close() error

Close closes the database

func (*Tester) Init

func (r *Tester) Init() error

Init initializes our database.

func (*Tester) SetupFilePath

func (r *Tester) SetupFilePath() string

SetupFilePath returns the complete path to the setup file.

Jump to

Keyboard shortcuts

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