Skip to content

Commit 1328bdf

Browse files
Merge pull request #20 from chenmingyong0423/feature/callback
Callback: rename function
2 parents 4173f7c + ebfd393 commit 1328bdf

File tree

2 files changed

+232
-2
lines changed

2 files changed

+232
-2
lines changed

callback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"github.com/chenmingyong0423/go-mongox/operation"
2020
)
2121

22-
func Register(name string, cb callback.CbFn, opType operation.OpType) {
22+
func RegisterPlugin(name string, cb callback.CbFn, opType operation.OpType) {
2323
callback.Callbacks.Register(opType, name, cb)
2424
}
2525

26-
func Remove(name string, opType operation.OpType) {
26+
func RemovePlugin(name string, opType operation.OpType) {
2727
callback.Callbacks.Remove(opType, name)
2828
}

callback_test.go

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright 2024 chenmingyong0423
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mongox
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/chenmingyong0423/go-mongox/callback"
22+
"github.com/chenmingyong0423/go-mongox/operation"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
"go.mongodb.org/mongo-driver/bson"
26+
)
27+
28+
func TestRegisterPlugin_Insert(t *testing.T) {
29+
type User struct {
30+
Name string
31+
Age int
32+
}
33+
isCalled := false
34+
// before insert
35+
t.Run("before insert", func(t *testing.T) {
36+
RegisterPlugin("before insert", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
37+
user := opCtx.Doc.(*User)
38+
require.NotNil(t, user)
39+
isCalled = true
40+
return nil
41+
}, operation.OpTypeBeforeInsert)
42+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc(&User{Name: "Mingyong Chen", Age: 18})), operation.OpTypeBeforeInsert)
43+
require.Nil(t, err)
44+
assert.True(t, isCalled)
45+
isCalled = false
46+
RemovePlugin("before insert", operation.OpTypeBeforeInsert)
47+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc(&User{Name: "Mingyong Chen", Age: 18})), operation.OpTypeBeforeInsert)
48+
require.Nil(t, err)
49+
assert.False(t, isCalled)
50+
})
51+
52+
// after insert
53+
t.Run("after insert", func(t *testing.T) {
54+
RegisterPlugin("after insert", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
55+
users := opCtx.Doc.([]*User)
56+
require.NotNil(t, users)
57+
isCalled = true
58+
return nil
59+
}, operation.OpTypeAfterInsert)
60+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc([]*User{{Name: "Mingyong Chen", Age: 18}})), operation.OpTypeAfterInsert)
61+
require.Nil(t, err)
62+
assert.True(t, isCalled)
63+
isCalled = false
64+
RemovePlugin("after insert", operation.OpTypeAfterInsert)
65+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc([]*User{{Name: "Mingyong Chen", Age: 18}})), operation.OpTypeAfterInsert)
66+
require.Nil(t, err)
67+
assert.False(t, isCalled)
68+
})
69+
}
70+
71+
func TestRegisterPlugin_Find(t *testing.T) {
72+
type User struct {
73+
Name string
74+
Age int
75+
}
76+
isCalled := false
77+
// before find
78+
t.Run("before find", func(t *testing.T) {
79+
RegisterPlugin("before find", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
80+
require.NotNil(t, opCtx.Filter)
81+
isCalled = true
82+
return nil
83+
}, operation.OpTypeBeforeFind)
84+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"})), operation.OpTypeBeforeFind)
85+
require.Nil(t, err)
86+
assert.True(t, isCalled)
87+
isCalled = false
88+
RemovePlugin("before find", operation.OpTypeBeforeFind)
89+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"})), operation.OpTypeBeforeFind)
90+
require.Nil(t, err)
91+
assert.False(t, isCalled)
92+
})
93+
94+
// after find
95+
t.Run("after find", func(t *testing.T) {
96+
RegisterPlugin("after find", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
97+
users := opCtx.Doc.([]*User)
98+
require.NotNil(t, users)
99+
isCalled = true
100+
return nil
101+
}, operation.OpTypeAfterFind)
102+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc([]*User{{Name: "Mingyong Chen", Age: 18}})), operation.OpTypeAfterFind)
103+
require.Nil(t, err)
104+
assert.True(t, isCalled)
105+
isCalled = false
106+
RemovePlugin("after find", operation.OpTypeAfterFind)
107+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc([]*User{{Name: "Mingyong Chen", Age: 18}})), operation.OpTypeAfterFind)
108+
require.Nil(t, err)
109+
assert.False(t, isCalled)
110+
})
111+
}
112+
113+
func TestRegisterPlugin_Delete(t *testing.T) {
114+
type User struct {
115+
Name string
116+
Age int
117+
}
118+
isCalled := false
119+
// before delete
120+
t.Run("before delete", func(t *testing.T) {
121+
RegisterPlugin("before delete", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
122+
require.NotNil(t, opCtx.Filter)
123+
isCalled = true
124+
return nil
125+
}, operation.OpTypeBeforeDelete)
126+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"})), operation.OpTypeBeforeDelete)
127+
require.Nil(t, err)
128+
assert.True(t, isCalled)
129+
isCalled = false
130+
RemovePlugin("before delete", operation.OpTypeBeforeDelete)
131+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"})), operation.OpTypeBeforeDelete)
132+
require.Nil(t, err)
133+
assert.False(t, isCalled)
134+
})
135+
136+
// after delete
137+
t.Run("after delete", func(t *testing.T) {
138+
RegisterPlugin("after delete", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
139+
require.NotNil(t, opCtx.Filter)
140+
isCalled = true
141+
return nil
142+
}, operation.OpTypeAfterDelete)
143+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"})), operation.OpTypeAfterDelete)
144+
require.Nil(t, err)
145+
assert.True(t, isCalled)
146+
isCalled = false
147+
RemovePlugin("after delete", operation.OpTypeAfterDelete)
148+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithDoc([]*User{{Name: "Mingyong Chen", Age: 18}})), operation.OpTypeAfterDelete)
149+
require.Nil(t, err)
150+
assert.False(t, isCalled)
151+
})
152+
}
153+
154+
func TestRegisterPlugin_Update(t *testing.T) {
155+
isCalled := false
156+
// before update
157+
t.Run("before update", func(t *testing.T) {
158+
RegisterPlugin("before update", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
159+
require.NotNil(t, opCtx.Filter)
160+
require.NotNil(t, opCtx.Updates)
161+
isCalled = true
162+
return nil
163+
}, operation.OpTypeBeforeUpdate)
164+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithUpdate(bson.M{"$set": bson.M{"name": "Burt"}})), operation.OpTypeBeforeUpdate)
165+
require.Nil(t, err)
166+
assert.True(t, isCalled)
167+
isCalled = false
168+
RemovePlugin("before update", operation.OpTypeBeforeUpdate)
169+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithUpdate(bson.M{"$set": bson.M{"name": "Burt"}})), operation.OpTypeBeforeUpdate)
170+
require.Nil(t, err)
171+
assert.False(t, isCalled)
172+
})
173+
174+
// after update
175+
t.Run("after update", func(t *testing.T) {
176+
RegisterPlugin("after update", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
177+
require.NotNil(t, opCtx.Filter)
178+
require.NotNil(t, opCtx.Updates)
179+
isCalled = true
180+
return nil
181+
}, operation.OpTypeAfterUpdate)
182+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithUpdate(bson.M{"$set": bson.M{"name": "Burt"}})), operation.OpTypeAfterUpdate)
183+
require.Nil(t, err)
184+
assert.True(t, isCalled)
185+
isCalled = false
186+
RemovePlugin("after update", operation.OpTypeAfterUpdate)
187+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithUpdate(bson.M{"$set": bson.M{"name": "Burt"}})), operation.OpTypeAfterUpdate)
188+
require.Nil(t, err)
189+
assert.False(t, isCalled)
190+
})
191+
}
192+
193+
func TestRegisterPlugin_Upsert(t *testing.T) {
194+
isCalled := false
195+
// before upsert
196+
t.Run("before upsert", func(t *testing.T) {
197+
RegisterPlugin("before upsert", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
198+
require.NotNil(t, opCtx.Filter)
199+
require.NotNil(t, opCtx.Replacement)
200+
isCalled = true
201+
return nil
202+
}, operation.OpTypeBeforeUpsert)
203+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithReplacement(bson.M{"name": "Burt"})), operation.OpTypeBeforeUpsert)
204+
require.Nil(t, err)
205+
assert.True(t, isCalled)
206+
isCalled = false
207+
RemovePlugin("before upsert", operation.OpTypeBeforeUpsert)
208+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithReplacement(bson.M{"name": "Burt"})), operation.OpTypeBeforeUpsert)
209+
require.Nil(t, err)
210+
assert.False(t, isCalled)
211+
})
212+
213+
// after upsert
214+
t.Run("after upsert", func(t *testing.T) {
215+
RegisterPlugin("after upsert", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
216+
require.NotNil(t, opCtx.Filter)
217+
require.NotNil(t, opCtx.Replacement)
218+
isCalled = true
219+
return nil
220+
}, operation.OpTypeAfterUpsert)
221+
err := callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithReplacement(bson.M{"name": "Burt"})), operation.OpTypeAfterUpsert)
222+
require.Nil(t, err)
223+
assert.True(t, isCalled)
224+
isCalled = false
225+
RemovePlugin("after upsert", operation.OpTypeAfterUpsert)
226+
err = callback.Callbacks.Execute(context.Background(), operation.NewOpContext(nil, operation.WithFilter(bson.M{"name": "Mingyong Chen"}), operation.WithReplacement(bson.M{"name": "Burt"})), operation.OpTypeAfterUpsert)
227+
require.Nil(t, err)
228+
assert.False(t, isCalled)
229+
})
230+
}

0 commit comments

Comments
 (0)