Documentation ¶
Overview ¶
Package dmltest provides functions for testing the dml package.
Index ¶
- Constants
- func CheckLastInsertID(t interface{ ... }, msg ...string) func(sql.Result, error) int64
- func Close(t testing.TB, c io.Closer)
- func FatalIfError(t testing.TB, err error)
- func LoadCSV(opts ...csvOptions) (columns []string, rows [][]driver.Value, err error)
- func MockClose(t testing.TB, c io.Closer, m sqlmock.Sqlmock)
- func MockDB(t testing.TB, opts ...dml.ConnPoolOption) (*dml.ConnPool, sqlmock.Sqlmock)
- func MockDBCallBack(t testing.TB, mockCB func(sqlmock.Sqlmock), opts ...dml.ConnPoolOption) (*dml.ConnPool, sqlmock.Sqlmock)
- func MockRows(opts ...csvOptions) (*sqlmock.Rows, error)
- func MustConnectDB(t testing.TB, opts ...dml.ConnPoolOption) *dml.ConnPool
- func MustGetDSN(t testing.TB) string
- func MustMockRows(opts ...csvOptions) *sqlmock.Rows
- func SQLDumpLoad(t testing.TB, globPattern string, o *SQLDumpOptions) struct{ ... }
- func SQLMockQuoteMeta(s string) string
- func WithFile(elem ...string) csvOptions
- func WithReaderConfig(cr CSVConfig) csvOptions
- func WithSQLLog(buf io.Writer, printNamedArgs bool) dml.ConnPoolOption
- func WithTestMode() csvOptions
- type CSVConfig
- type SQLDumpOptions
Constants ¶
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 FatalIfError ¶
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 ¶
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 ¶
MockClose for usage in conjunction with defer.
defer dmltest.MockClose(t, db, dbMock)
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 ¶
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 ¶
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 ¶
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.