dmltest

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package dmltest provides functions for testing the dml package.

Index

Constants

View Source
const EnvDSN = "CS_DSN"

EnvDSN is the name of the environment variable

Variables

This section is empty.

Functions

func CheckLastInsertID

func CheckLastInsertID(t interface {
	Errorf(format string, args ...any)
}, msg ...string) func(sql.Result, error) int64

CheckLastInsertID returns a function which accepts the return result from Exec*() and returns itself the last_insert_id or emits an error.

func Close

func Close(t testing.TB, c io.Closer)

Close for usage in conjunction with defer.

defer dmltest.Close(t, db)

func FatalIfError

func FatalIfError(t testing.TB, err error)

FatalIfError fails the tests if an unexpected error occurred. If the error is gift wrapped, it prints the location. If `t` is nil, this function panics.

func LoadCSV

func LoadCSV(opts ...csvOptions) (columns []string, rows [][]driver.Value, err error)

LoadCSV loads a csv file for mocked database testing. Like github.com/DATA-DOG/go-sqlmock does. CSV file should be comma separated.

func MockClose

func MockClose(t testing.TB, c io.Closer, m sqlmock.Sqlmock)

MockClose for usage in conjunction with defer.

defer dmltest.MockClose(t, db, dbMock)

func MockDB

func MockDB(t testing.TB, opts ...dml.ConnPoolOption) (*dml.ConnPool, sqlmock.Sqlmock)

MockDB creates a mocked database connection. Fatals on error.

func MockDBCallBack

func MockDBCallBack(t testing.TB, mockCB func(sqlmock.Sqlmock), opts ...dml.ConnPoolOption) (*dml.ConnPool, sqlmock.Sqlmock)

MockDBCallBack same as MockDB but allows to add expectations early to the mock.

func MockRows

func MockRows(opts ...csvOptions) (*sqlmock.Rows, error)

MockRows same as LoadCSV() but creates a fully functional driver.Rows interface from a CSV file.

func MustConnectDB

func MustConnectDB(t testing.TB, opts ...dml.ConnPoolOption) *dml.ConnPool

MustConnectDB is a helper function that creates a new database connection using a DSN from an environment variable found in the constant csdb.EnvDSN. If the DSN environment variable has not been set it skips the test. It creates a random database if the DSN database name is the word "random".

func MustGetDSN

func MustGetDSN(t testing.TB) string

MustGetDSN returns the data source name from an environment variable or panics on error.

func MustMockRows

func MustMockRows(opts ...csvOptions) *sqlmock.Rows

MustMockRows same as MockRows but panics on error

func SQLDumpLoad

func SQLDumpLoad(t testing.TB, globPattern string, o *SQLDumpOptions) struct{ Deferred func() }

SQLDumpLoad reads all files recognized by `globPattern` argument into MySQL/MariaDB. The password will NOT be visible via process manager but gets temporarily written into the TMP dir of the OS. This function does even work when the server and the client runs on different machines. For now it only works when the program `bash` has been installed. This function supports any file size of a `.sql` file. Bonus: if file names contain the string "cleanup", they will be run in the defer function. The returned function must be run in the defer part of a test. This function skips a test, if the DSN environment variable cannot be found.

func SQLMockQuoteMeta

func SQLMockQuoteMeta(s string) string

SQLMockQuoteMeta hacky work around to remove multiple \s via regexp and replace them with a single whitespace. Because the SQL Mock driver creates from a multi line string a single line string.

func WithFile

func WithFile(elem ...string) csvOptions

WithFile sets the file name. File path prefix is always RootPath variable.

func WithReaderConfig

func WithReaderConfig(cr CSVConfig) csvOptions

WithReaderConfig sets CSV reader options

func WithSQLLog

func WithSQLLog(buf io.Writer, printNamedArgs bool) dml.ConnPoolOption

WithSQLLog displays the SQL query and its named arguments on driver level.

func WithTestMode

func WithTestMode() csvOptions

WithTestMode allows better testing. Converts []bytes in driver.Value to text.Chars

Types

type CSVConfig

type CSVConfig struct {
	// Comma is the field delimiter.
	// It is set to comma (',') by NewReader.
	Comma rune
	// Comment, if not 0, is the comment character. Lines beginning with the
	// Comment character without preceding whitespace are ignored. With leading
	// whitespace the Comment character becomes part of the field, even if
	// TrimLeadingSpace is true.
	Comment rune
	// FieldsPerRecord is the number of expected fields per record. If
	// FieldsPerRecord is positive, Read requires each record to have the given
	// number of fields. If FieldsPerRecord is 0, Read sets it to the number of
	// fields in the first record, so that future records must have the same
	// field count. If FieldsPerRecord is negative, no check is made and records
	// may have a variable number of fields.
	FieldsPerRecord int
	// If LazyQuotes is true, a quote may appear in an unquoted field and a
	// non-doubled quote may appear in a quoted field.
	LazyQuotes *bool
	// If TrimLeadingSpace is true, leading white space in a field is ignored.
	// This is done even if the field delimiter, Comma, is white space.
	TrimLeadingSpace *bool
}

CSVConfig allows to set special options when parsing the csv file.

type SQLDumpOptions

type SQLDumpOptions struct {
	MySQLPath     string
	MySQLArgs     []string
	DSN           string
	SkipDBCleanup bool
	// contains filtered or unexported fields
}

SQLDumpOptions can set a different MySQL/MariaDB binary path and adds more arguments.

Jump to

Keyboard shortcuts

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