Skip to content

Commit 445195e

Browse files
authored
Add tests (#1)
1 parent 95ddc6e commit 445195e

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# dbwrap
22

33
[![Build Status](https://github.com/bool64/dbwrap/workflows/test-unit/badge.svg)](https://github.com/bool64/dbwrap/actions?query=branch%3Amaster+workflow%3Atest-unit)
4-
[![Coverage Status](https://codecov.io/gh/bool64/dbwrap/branch/master/graph/badge.svg)](https://codecov.io/gh/bool64/dbwrap)
54
[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/bool64/dbwrap)
65
[![Time Tracker](https://wakatime.com/badge/github/bool64/dbwrap.svg)](https://wakatime.com/badge/github/bool64/dbwrap)
76
![Code lines](https://sloc.xyz/github/bool64/dbwrap/?category=code)

driver_ext_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ func TestRegister(t *testing.T) {
2424
var l []string
2525

2626
driverName, err := dbwrap.Register("sqlmock",
27+
dbwrap.WithOptions(dbwrap.Options{}),
28+
dbwrap.WithAllOperations(),
29+
2730
dbwrap.WithMiddleware(
2831
func(ctx context.Context, operation dbwrap.Operation, statement string, args []driver.NamedValue) (nCtx context.Context, onFinish func(error)) {
32+
assert.Equal(t, "bool64/dbwrap_test.TestRegister", dbwrap.Caller())
2933
l = append(l, "mw1 triggered: "+string(operation)+": "+statement)
3034

3135
return ctx, func(err error) {
@@ -48,6 +52,7 @@ func TestRegister(t *testing.T) {
4852
}
4953
},
5054
),
55+
5156
dbwrap.WithInterceptor(func(ctx context.Context, operation dbwrap.Operation, statement string, args []driver.NamedValue) (context.Context, string, []driver.NamedValue) {
5257
l = append(l, "intercepted: "+string(operation)+": "+statement)
5358

@@ -171,6 +176,18 @@ mw1 triggered: query: SELECT a FROM b WHERE c = ? #intercepted
171176
mw2 triggered: query: SELECT a FROM b WHERE c = ? #intercepted
172177
mw2 done
173178
mw1 done
179+
mw1 triggered: rows_next:
180+
mw2 triggered: rows_next:
181+
mw2 done
182+
mw1 done
183+
mw1 triggered: rows_next:
184+
mw2 triggered: rows_next:
185+
mw2 done
186+
mw1 done
187+
mw1 triggered: rows_next:
188+
mw2 triggered: rows_next:
189+
mw2 failed: EOF
190+
mw1 failed: EOF
174191
mw1 triggered: rows_close:
175192
mw2 triggered: rows_close:
176193
mw2 done

options.go

+21
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ func WithOperations(op ...Operation) Option {
7878
}
7979
}
8080

81+
// WithAllOperations enables all operations to be wrapped with middleware.
82+
// This also includes RowsNext.
83+
func WithAllOperations() Option {
84+
return WithOperations(
85+
Ping,
86+
Exec,
87+
Query,
88+
Prepare,
89+
Begin,
90+
LastInsertID,
91+
RowsAffected,
92+
StmtExec,
93+
StmtQuery,
94+
StmtClose,
95+
RowsClose,
96+
RowsNext,
97+
Commit,
98+
Rollback,
99+
)
100+
}
101+
81102
// prepareOptions returns prepared Options and flag if they are operational.
82103
func prepareOptions(options []Option) (Options, bool) {
83104
o := Options{}

runtime.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ func Caller(skipPackages ...string) string {
2424

2525
for i := 0; i < n; i++ {
2626
f := runtime.FuncForPC(pc[i])
27+
28+
// Skip unnamed literals.
29+
if strings.Contains(f.Name(), "{") {
30+
continue
31+
}
32+
2733
parts := strings.Split(f.Name(), "/")
2834
parts[len(parts)-1] = strings.Split(parts[len(parts)-1], ".")[0]
2935
p = strings.Join(parts, "/")
3036

31-
if p == "database/sql" {
37+
if p == "database/sql" || p == "github.com/bool64/dbwrap" {
3238
continue
3339
}
3440

0 commit comments

Comments
 (0)