From 4ee1fdaf444883d6fbe9398f2b7f0d77daae96fa Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 7 Jun 2019 09:26:40 +0300 Subject: [PATCH] closes #159 --- go.mod | 2 ++ sqlmock_go18_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/go.mod b/go.mod index eaf8a5a..0d598b0 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/DATA-DOG/go-sqlmock + +go 1.12 diff --git a/sqlmock_go18_test.go b/sqlmock_go18_test.go index e53d9c7..1db3f5f 100644 --- a/sqlmock_go18_test.go +++ b/sqlmock_go18_test.go @@ -474,3 +474,34 @@ func TestContextExecErrorDelay(t *testing.T) { t.Errorf("expecting a delay of less than %v before error, actual delay was %v", delay, elapsed) } } + +func TestStatementTX(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() + + prep := mock.ExpectPrepare("SELECT") + mock.ExpectBegin() + + prep.ExpectQuery().WithArgs(1).WillReturnError(errors.New("fast fail")) + + stmt, err := db.Prepare("SELECT title, body FROM articles WHERE id = ?") + if err != nil { + t.Fatalf("unexpected error on prepare: %v", err) + } + + tx, err := db.Begin() + if err != nil { + t.Fatalf("unexpected error on begin: %v", err) + } + + // upgrade connection for statement + txStmt := tx.Stmt(stmt) + _, err = txStmt.Query(1) + if err == nil || err.Error() != "fast fail" { + t.Fatalf("unexpected result: %v", err) + } +}