{"modulePath":"github.com/coderanger/go-sqlmock","version":"v1.3.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqlmock\n\nPackage sqlmock is a mock library implementing sql driver. Which has one and only purpose - to simulate any sql driver behavior in tests, without needing a real database connection. It helps to maintain correct \\*\\*TDD\\*\\* workflow.\n\nIt does not require any modifications to your source code in order to test and mock database operations. Supports concurrency and multiple database mocking.\n\nThe driver allows to mock any sql driver method behavior.\n\n## Variables\n\n```go\nvar CSVColumnParser = func(s string) []byte {\n\tswitch {\n\tcase strings.ToLower(s) == \"null\":\n\t\treturn nil\n\t}\n\treturn []byte(s)\n}\n```\nCSVColumnParser is a function which converts trimmed csv column string to a \\[]byte representation. currently transforms NULL to nil\n\n```go\nvar ErrCancelled = errors.New(\"canceling query due to user request\")\n```\n\n## Functions\n\n```go\nfunc NewErrorResult(err error) driver.Result\n```\nNewErrorResult creates a new sql driver Result which returns an error given for both interface methods\n\n```go\nfunc NewResult(lastInsertID int64, rowsAffected int64) driver.Result\n```\nNewResult creates a new sql driver Result for Exec based query mocks.\n\n## Types\n\n```go\ntype Argument interface {\n\tMatch(driver.Value) bool\n}\n```\nArgument interface allows to match any argument in specific way when used with ExpectedQuery and ExpectedExec expectations.\n\n```go\nfunc AnyArg() Argument\n```\nAnyArg will return an Argument which can match any kind of arguments.\n\nUseful for time.Time or similar kinds of arguments.\n\n```go\ntype ExpectedBegin struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedBegin is used to manage \\*sql.DB.Begin expectation returned by \\*Sqlmock.ExpectBegin.\n\n```go\nfunc (e *ExpectedBegin) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedBegin) WillDelayFor(duration time.Duration) *ExpectedBegin\n```\nWillDelayFor allows to specify duration for which it will delay result. May be used together with Context\n\n```go\nfunc (e *ExpectedBegin) WillReturnError(err error) *ExpectedBegin\n```\nWillReturnError allows to set an error for \\*sql.DB.Begin action\n\n```go\ntype ExpectedClose struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedClose is used to manage \\*sql.DB.Close expectation returned by \\*Sqlmock.ExpectClose.\n\n```go\nfunc (e *ExpectedClose) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedClose) WillReturnError(err error) *ExpectedClose\n```\nWillReturnError allows to set an error for \\*sql.DB.Close action\n\n```go\ntype ExpectedCommit struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedCommit is used to manage \\*sql.Tx.Commit expectation returned by \\*Sqlmock.ExpectCommit.\n\n```go\nfunc (e *ExpectedCommit) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedCommit) WillReturnError(err error) *ExpectedCommit\n```\nWillReturnError allows to set an error for \\*sql.Tx.Close action\n\n```go\ntype ExpectedExec struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedExec is used to manage \\*sql.DB.Exec, \\*sql.Tx.Exec or \\*sql.Stmt.Exec expectations. Returned by \\*Sqlmock.ExpectExec.\n\n```go\nfunc (e *ExpectedExec) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec\n```\nWillDelayFor allows to specify duration for which it will delay result. May be used together with Context\n\n```go\nfunc (e *ExpectedExec) WillReturnError(err error) *ExpectedExec\n```\nWillReturnError allows to set an error for expected database exec action\n\n```go\nfunc (e *ExpectedExec) WillReturnResult(result driver.Result) *ExpectedExec\n```\nWillReturnResult arranges for an expected Exec() to return a particular result, there is sqlmock.NewResult(lastInsertID int64, affectedRows int64) method to build a corresponding result. Or if actions needs to be tested against errors sqlmock.NewErrorResult(err error) to return a given error.\n\n```go\nfunc (e *ExpectedExec) WithArgs(args ...driver.Value) *ExpectedExec\n```\nWithArgs will match given expected args to actual database exec operation arguments. if at least one argument does not match, it will return an error. For specific arguments an sqlmock.Argument interface can be used to match an argument.\n\n```go\ntype ExpectedPrepare struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedPrepare is used to manage \\*sql.DB.Prepare or \\*sql.Tx.Prepare expectations. Returned by \\*Sqlmock.ExpectPrepare.\n\n```go\nfunc (e *ExpectedPrepare) ExpectExec() *ExpectedExec\n```\nExpectExec allows to expect Exec() on this prepared statement. this method is convenient in order to prevent duplicating sql query string matching.\n\n```go\nfunc (e *ExpectedPrepare) ExpectQuery() *ExpectedQuery\n```\nExpectQuery allows to expect Query() or QueryRow() on this prepared statement. this method is convenient in order to prevent duplicating sql query string matching.\n\n```go\nfunc (e *ExpectedPrepare) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedPrepare) WillBeClosed() *ExpectedPrepare\n```\nWillBeClosed expects this prepared statement to be closed.\n\n```go\nfunc (e *ExpectedPrepare) WillDelayFor(duration time.Duration) *ExpectedPrepare\n```\nWillDelayFor allows to specify duration for which it will delay result. May be used together with Context\n\n```go\nfunc (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare\n```\nWillReturnCloseError allows to set an error for this prepared statement Close action\n\n```go\nfunc (e *ExpectedPrepare) WillReturnError(err error) *ExpectedPrepare\n```\nWillReturnError allows to set an error for the expected \\*sql.DB.Prepare or \\*sql.Tx.Prepare action.\n\n```go\ntype ExpectedQuery struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedQuery is used to manage \\*sql.DB.Query, \\*dql.DB.QueryRow, \\*sql.Tx.Query, \\*sql.Tx.QueryRow, \\*sql.Stmt.Query or \\*sql.Stmt.QueryRow expectations. Returned by \\*Sqlmock.ExpectQuery.\n\n```go\nfunc (e *ExpectedQuery) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery\n```\nWillDelayFor allows to specify duration for which it will delay result. May be used together with Context\n\n```go\nfunc (e *ExpectedQuery) WillReturnError(err error) *ExpectedQuery\n```\nWillReturnError allows to set an error for expected database query\n\n```go\nfunc (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery\n```\nWillReturnRows specifies the set of resulting rows that will be returned by the triggered query\n\n```go\nfunc (e *ExpectedQuery) WithArgs(args ...driver.Value) *ExpectedQuery\n```\nWithArgs will match given expected args to actual database query arguments. if at least one argument does not match, it will return an error. For specific arguments an sqlmock.Argument interface can be used to match an argument.\n\n```go\ntype ExpectedRollback struct {\n\t// contains filtered or unexported fields\n}\n```\nExpectedRollback is used to manage \\*sql.Tx.Rollback expectation returned by \\*Sqlmock.ExpectRollback.\n\n```go\nfunc (e *ExpectedRollback) String() string\n```\nString returns string representation\n\n```go\nfunc (e *ExpectedRollback) WillReturnError(err error) *ExpectedRollback\n```\nWillReturnError allows to set an error for \\*sql.Tx.Rollback action\n\n```go\ntype Rows struct {\n\t// contains filtered or unexported fields\n}\n```\nRows is a mocked collection of rows to return for Query result\n\n```go\nfunc NewRows(columns []string) *Rows\n```\nNewRows allows Rows to be created from a sql driver.Value slice or from the CSV string and to be used as sql driver.Rows\n\n```go\nfunc (r *Rows) AddRow(values ...driver.Value) *Rows\n```\nAddRow composed from database driver.Value slice return the same instance to perform subsequent actions. Note that the number of values must match the number of columns\n\n```go\nfunc (r *Rows) CloseError(err error) *Rows\n```\nCloseError allows to set an error which will be returned by rows.Close function.\n\nThe close error will be triggered only in cases when rows.Next() EOF was not yet reached, that is a default sql library behavior\n\n```go\nfunc (r *Rows) FromCSVString(s string) *Rows\n```\nFromCSVString build rows from csv string. return the same instance to perform subsequent actions. Note that the number of values must match the number of columns\n\n```go\nfunc (r *Rows) RowError(row int, err error) *Rows\n```\nRowError allows to set an error which will be returned when a given row number is read\n\n```go\ntype Sqlmock interface {\n\n\t// ExpectClose queues an expectation for this database\n\t// action to be triggered. the *ExpectedClose allows\n\t// to mock database response\n\tExpectClose() *ExpectedClose\n\n\t// ExpectationsWereMet checks whether all queued expectations\n\t// were met in order. If any of them was not met - an error is returned.\n\tExpectationsWereMet() error\n\n\t// ExpectPrepare expects Prepare() to be called with sql query\n\t// which match sqlRegexStr given regexp.\n\t// the *ExpectedPrepare allows to mock database response.\n\t// Note that you may expect Query() or Exec() on the *ExpectedPrepare\n\t// statement to prevent repeating sqlRegexStr\n\tExpectPrepare(sqlRegexStr string) *ExpectedPrepare\n\n\t// ExpectQuery expects Query() or QueryRow() to be called with sql query\n\t// which match sqlRegexStr given regexp.\n\t// the *ExpectedQuery allows to mock database response.\n\tExpectQuery(sqlRegexStr string) *ExpectedQuery\n\n\t// ExpectExec expects Exec() to be called with sql query\n\t// which match sqlRegexStr given regexp.\n\t// the *ExpectedExec allows to mock database response\n\tExpectExec(sqlRegexStr string) *ExpectedExec\n\n\t// ExpectBegin expects *sql.DB.Begin to be called.\n\t// the *ExpectedBegin allows to mock database response\n\tExpectBegin() *ExpectedBegin\n\n\t// ExpectCommit expects *sql.Tx.Commit to be called.\n\t// the *ExpectedCommit allows to mock database response\n\tExpectCommit() *ExpectedCommit\n\n\t// ExpectRollback expects *sql.Tx.Rollback to be called.\n\t// the *ExpectedRollback allows to mock database response\n\tExpectRollback() *ExpectedRollback\n\n\t// MatchExpectationsInOrder gives an option whether to match all\n\t// expectations in the order they were set or not.\n\t//\n\t// By default it is set to - true. But if you use goroutines\n\t// to parallelize your query executation, that option may\n\t// be handy.\n\t//\n\t// This option may be turned on anytime during tests. As soon\n\t// as it is switched to false, expectations will be matched\n\t// in any order. Or otherwise if switched to true, any unmatched\n\t// expectations will be expected in order\n\tMatchExpectationsInOrder(bool)\n}\n```\nSqlmock interface serves to create expectations for any kind of database action in order to mock and test real database behavior.\n\n```go\nfunc New() (*sql.DB, Sqlmock, error)\n```\nNew creates sqlmock database connection and a mock to manage expectations. Pings db so that all expectations could be asserted.\n\n```go\nfunc NewWithDSN(dsn string) (*sql.DB, Sqlmock, error)\n```\nNewWithDSN creates sqlmock database connection with a specific DSN and a mock to manage expectations. Pings db so that all expectations could be asserted.\n\nThis method is introduced because of sql abstraction libraries, which do not provide a way to initialize with sql.DB instance. For example GORM library.\n\nNote, it will error if attempted to create with an already used dsn\n\nIt is not recommended to use this method, unless you really need it and there is no other way around.\n\n","path":"github.com/coderanger/go-sqlmock","name":"sqlmock","synopsis":"Package sqlmock is a mock library implementing sql driver.","isRedistributable":true}
