@@ -18,20 +18,44 @@ package creator
1818
1919import (
2020 "context"
21+ "errors"
2122 "testing"
23+ "time"
24+
25+ "github.com/chenmingyong0423/go-mongox/callback"
26+ "github.com/chenmingyong0423/go-mongox/operation"
27+
28+ "go.mongodb.org/mongo-driver/bson/primitive"
2229
2330 "github.com/stretchr/testify/require"
2431
2532 "github.com/chenmingyong0423/go-mongox/builder/query"
2633
27- "github.com/chenmingyong0423/go-mongox/types"
28-
2934 "github.com/stretchr/testify/assert"
3035 "go.mongodb.org/mongo-driver/mongo"
3136 "go.mongodb.org/mongo-driver/mongo/options"
3237 "go.mongodb.org/mongo-driver/mongo/readpref"
3338)
3439
40+ type User struct {
41+ ID primitive.ObjectID `bson:"_id,omitempty"`
42+ Name string `bson:"name"`
43+ Age int64
44+ UnknownField string `bson:"-"`
45+ CreatedAt time.Time `bson:"created_at"`
46+ UpdatedAt time.Time `bson:"updated_at"`
47+ }
48+
49+ func (u * User ) DefaultCreatedAt () {
50+ if u .CreatedAt .IsZero () {
51+ u .CreatedAt = time .Now ().Local ()
52+ }
53+ }
54+
55+ func (u * User ) DefaultUpdatedAt () {
56+ u .UpdatedAt = time .Now ().Local ()
57+ }
58+
3559func newCollection (t * testing.T ) * mongo.Collection {
3660 client , err := mongo .Connect (context .Background (), options .Client ().ApplyURI ("mongodb://localhost:27017" ).SetAuth (options.Credential {
3761 Username : "test" ,
@@ -47,16 +71,15 @@ func newCollection(t *testing.T) *mongo.Collection {
4771
4872func TestCreator_e2e_One (t * testing.T ) {
4973 collection := newCollection (t )
50- creator := NewCreator [types. TestUser ](collection )
74+ creator := NewCreator [User ](collection )
5175 testCases := []struct {
5276 name string
5377 before func (ctx context.Context , t * testing.T )
5478 after func (ctx context.Context , t * testing.T )
5579 opts []* options.InsertOneOptions
5680 ctx context.Context
57- doc * types. TestUser
81+ doc * User
5882
59- wantId string
6083 wantError assert.ErrorAssertionFunc
6184 }{
6285 {
@@ -82,7 +105,7 @@ func TestCreator_e2e_One(t *testing.T) {
82105 opts : []* options.InsertOneOptions {
83106 options .InsertOne ().SetComment ("test" ),
84107 },
85- doc : & types. TestUser {
108+ doc : & User {
86109 Name : "chenmingyong" ,
87110 Age : 24 ,
88111 },
@@ -109,18 +132,46 @@ func TestCreator_e2e_One(t *testing.T) {
109132 }
110133 })
111134 }
135+ t .Run ("before hook error" , func (t * testing.T ) {
136+ ctx := context .Background ()
137+ doc := & User {}
138+ callback .GetCallback ().Register (operation .OpTypeBeforeInsert , "before hook error" , func (ctx context.Context , opCtx * operation.OpContext , opts ... any ) error {
139+ return errors .New ("before hook error" )
140+ })
141+ insertOneResult , err := creator .InsertOne (ctx , doc )
142+ require .Equal (t , err , errors .New ("before hook error" ))
143+ require .Nil (t , insertOneResult )
144+ callback .GetCallback ().Remove (operation .OpTypeBeforeInsert , "before hook error" )
145+ })
146+ t .Run ("after hook error" , func (t * testing.T ) {
147+ ctx := context .Background ()
148+ doc := & User {
149+ Name : "chenmingyong" ,
150+ Age : 24 ,
151+ }
152+ callback .GetCallback ().Register (operation .OpTypeAfterInsert , "after hook error" , func (ctx context.Context , opCtx * operation.OpContext , opts ... any ) error {
153+ return errors .New ("after hook error" )
154+ })
155+ insertOneResult , err := creator .InsertOne (ctx , doc )
156+ require .Equal (t , err , errors .New ("after hook error" ))
157+ require .Nil (t , insertOneResult )
158+ callback .GetCallback ().Remove (operation .OpTypeAfterInsert , "after hook error" )
159+ deleteResult , err := collection .DeleteOne (ctx , query .Eq ("name" , "chenmingyong" ))
160+ require .NoError (t , err )
161+ require .Equal (t , int64 (1 ), deleteResult .DeletedCount )
162+ })
112163}
113164
114165func TestCreator_e2e_Many (t * testing.T ) {
115166 collection := newCollection (t )
116- creator := NewCreator [types. TestUser ](collection )
167+ creator := NewCreator [User ](collection )
117168 testCases := []struct {
118169 name string
119170 before func (ctx context.Context , t * testing.T )
120171 after func (ctx context.Context , t * testing.T )
121172
122173 ctx context.Context
123- docs []* types. TestUser
174+ docs []* User
124175 opts []* options.InsertManyOptions
125176
126177 wantIdsLength int
@@ -149,7 +200,7 @@ func TestCreator_e2e_Many(t *testing.T) {
149200 options .InsertMany ().SetComment ("test" ),
150201 },
151202 ctx : context .Background (),
152- docs : []* types. TestUser {
203+ docs : []* User {
153204 {
154205 Name : "chenmingyong" ,
155206 Age : 24 ,
@@ -180,4 +231,32 @@ func TestCreator_e2e_Many(t *testing.T) {
180231 }
181232 })
182233 }
234+ t .Run ("before hook error" , func (t * testing.T ) {
235+ ctx := context .Background ()
236+ docs := []* User {{}}
237+ callback .GetCallback ().Register (operation .OpTypeBeforeInsert , "before hook error" , func (ctx context.Context , opCtx * operation.OpContext , opts ... any ) error {
238+ return errors .New ("before hook error" )
239+ })
240+ insertOneResult , err := creator .InsertMany (ctx , docs )
241+ require .Equal (t , err , errors .New ("before hook error" ))
242+ require .Nil (t , insertOneResult )
243+ callback .GetCallback ().Remove (operation .OpTypeBeforeInsert , "before hook error" )
244+ })
245+ t .Run ("after hook error" , func (t * testing.T ) {
246+ ctx := context .Background ()
247+ docs := []* User {{
248+ Name : "chenmingyong" ,
249+ Age : 24 ,
250+ }}
251+ callback .GetCallback ().Register (operation .OpTypeAfterInsert , "after hook error" , func (ctx context.Context , opCtx * operation.OpContext , opts ... any ) error {
252+ return errors .New ("after hook error" )
253+ })
254+ insertOneResult , err := creator .InsertMany (ctx , docs )
255+ require .Equal (t , err , errors .New ("after hook error" ))
256+ require .Nil (t , insertOneResult )
257+ callback .GetCallback ().Remove (operation .OpTypeAfterInsert , "after hook error" )
258+ deleteResult , err := collection .DeleteOne (ctx , query .Eq ("name" , "chenmingyong" ))
259+ require .NoError (t , err )
260+ require .Equal (t , int64 (1 ), deleteResult .DeletedCount )
261+ })
183262}
0 commit comments