Skip to content

Commit 2064648

Browse files
authored
Merge pull request #42 from qustavo/test/fix_nil_onerr
test: fix nil onError handler on testHooks
2 parents d5cee4c + a770ce1 commit 2064648

5 files changed

+19
-8
lines changed

benchmark_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func init() {
1515
hooks := &testHooks{}
16-
hooks.noop()
16+
hooks.reset()
1717

1818
sql.Register("sqlite3-benchmark", Wrap(&sqlite3.SQLiteDriver{}, hooks))
1919
sql.Register("mysql-benchmark", Wrap(&mysql.MySQLDriver{}, hooks))

sqlhooks_mysql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestMySQL(t *testing.T) {
3636
s.TestErrHookHook(t, "SELECT * FROM users WHERE id = $2", "INVALID_ARGS")
3737

3838
t.Run("DBWorks", func(t *testing.T) {
39-
s.hooks.noop()
39+
s.hooks.reset()
4040
if _, err := s.db.Exec("DELETE FROM users"); err != nil {
4141
t.Fatal(err)
4242
}

sqlhooks_postgres_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestPostgres(t *testing.T) {
3636
s.TestErrHookHook(t, "SELECT * FROM users WHERE id = $2", "INVALID_ARGS")
3737

3838
t.Run("DBWorks", func(t *testing.T) {
39-
s.hooks.noop()
39+
s.hooks.reset()
4040
if _, err := s.db.Exec("DELETE FROM users"); err != nil {
4141
t.Fatal(err)
4242
}

sqlhooks_sqlite3_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestSQLite3(t *testing.T) {
3434
s.TestErrHookHook(t, "SELECT * FROM users WHERE id = $2", "INVALID_ARGS")
3535

3636
t.Run("DBWorks", func(t *testing.T) {
37-
s.hooks.noop()
37+
s.hooks.reset()
3838
if _, err := s.db.Exec("DELETE FROM users"); err != nil {
3939
t.Fatal(err)
4040
}

sqlhooks_test.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ type testHooks struct {
1919
onError ErrorHook
2020
}
2121

22-
func (h *testHooks) noop() {
23-
noop := func(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
22+
func newTestHooks() *testHooks {
23+
th := &testHooks{}
24+
th.reset()
25+
return th
26+
}
27+
28+
func (h *testHooks) reset() {
29+
noop := func(ctx context.Context, _ string, _ ...interface{}) (context.Context, error) {
2430
return ctx, nil
2531
}
2632

27-
h.before, h.after = noop, noop
33+
noopErr := func(_ context.Context, err error, _ string, _ ...interface{}) error {
34+
return err
35+
}
36+
37+
h.before, h.after, h.onError = noop, noop, noopErr
2838
}
2939

3040
func (h *testHooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
@@ -45,7 +55,8 @@ type suite struct {
4555
}
4656

4757
func newSuite(t *testing.T, driver driver.Driver, dsn string) *suite {
48-
hooks := &testHooks{}
58+
hooks := newTestHooks()
59+
4960
driverName := fmt.Sprintf("sqlhooks-%s", time.Now().String())
5061
sql.Register(driverName, Wrap(driver, hooks))
5162

0 commit comments

Comments
 (0)