forked from qiniu/qmgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
37 lines (31 loc) · 911 Bytes
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package middleware
import (
"context"
"fmt"
"testing"
"github.com/nguyenngodinh/qmgo/operator"
"github.com/stretchr/testify/require"
)
func TestMiddleware(t *testing.T) {
ast := require.New(t)
ctx := context.Background()
// not register
ast.NoError(Do(ctx, "success", operator.BeforeInsert))
// valid register
Register(callbackTest)
ast.NoError(Do(ctx, "success", operator.BeforeInsert))
ast.Error(Do(ctx, "failure", operator.BeforeUpsert))
ast.NoError(Do(ctx, "failure", operator.BeforeUpdate, "success"))
}
func callbackTest(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
if doc.(string) == "success" && opType == operator.BeforeInsert {
return nil
}
if len(opts) > 0 && opts[0].(string) == "success" {
return nil
}
if doc.(string) == "failure" && opType == operator.BeforeUpsert {
return fmt.Errorf("this is error")
}
return nil
}