1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2024-11-24 08:12:13 +02:00

implements Pinger (without expectation yet) and prepared stmt Context methods

This commit is contained in:
gedi 2017-02-08 15:32:39 +02:00
parent 6bbe187a1a
commit 42ab7c33d0
2 changed files with 108 additions and 0 deletions

View File

@ -81,4 +81,21 @@ func (c *sqlmock) PrepareContext(ctx context.Context, query string) (driver.Stmt
}
}
// Implement the "Pinger" interface
// for now we do not have a Ping expectation
// may be something for the future
func (c *sqlmock) Ping(ctx context.Context) error {
return nil
}
// Implement the "StmtExecContext" interface
func (stmt *statement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return stmt.conn.ExecContext(ctx, stmt.query, args)
}
// Implement the "StmtQueryContext" interface
func (stmt *statement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return stmt.conn.QueryContext(ctx, stmt.query, args)
}
// @TODO maybe add ExpectedBegin.WithOptions(driver.TxOptions)

View File

@ -47,6 +47,50 @@ func TestContextExecCancel(t *testing.T) {
}
}
func TestPreparedStatementContextExecCancel(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
mock.ExpectPrepare("DELETE FROM users").
ExpectExec().
WillDelayFor(time.Second).
WillReturnResult(NewResult(1, 1))
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 10)
cancel()
}()
stmt, err := db.Prepare("DELETE FROM users")
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
_, err = stmt.ExecContext(ctx)
if err == nil {
t.Error("error was expected, but there was none")
}
if err != ErrCancelled {
t.Errorf("was expecting cancel error, but got: %v", err)
}
_, err = stmt.ExecContext(ctx)
if err != context.Canceled {
t.Error("error was expected since context was already done, but there was none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func TestContextExecWithNamedArg(t *testing.T) {
t.Parallel()
db, mock, err := New()
@ -164,6 +208,53 @@ func TestContextQueryCancel(t *testing.T) {
}
}
func TestPreparedStatementContextQueryCancel(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rs := NewRows([]string{"id", "title"}).AddRow(5, "hello world")
mock.ExpectPrepare("SELECT (.+) FROM articles WHERE id = ?").
ExpectQuery().
WithArgs(5).
WillDelayFor(time.Second).
WillReturnRows(rs)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 10)
cancel()
}()
stmt, err := db.Prepare("SELECT id, title FROM articles WHERE id = ?")
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
_, err = stmt.QueryContext(ctx, 5)
if err == nil {
t.Error("error was expected, but there was none")
}
if err != ErrCancelled {
t.Errorf("was expecting cancel error, but got: %v", err)
}
_, err = stmt.QueryContext(ctx, 5)
if err != context.Canceled {
t.Error("error was expected since context was already done, but there was none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func TestContextQuery(t *testing.T) {
t.Parallel()
db, mock, err := New()