@@ -257,6 +257,46 @@ func TestPluginInit_EnableEnableDefaultFieldHook(t *testing.T) {
257
257
require .NotZero (t , model .ID )
258
258
require .NotZero (t , model .CreatedAt )
259
259
RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeInsert )
260
+ RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpdate )
261
+ RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpsert )
262
+ })
263
+ t .Run ("beforeUpdate" , func (t * testing.T ) {
264
+ var (
265
+ model = & Model {}
266
+ m = bson.M {}
267
+ )
268
+ err := callback .GetCallback ().Execute (
269
+ context .Background (),
270
+ operation .NewOpContext (nil , operation .WithDoc (model ), operation .WithUpdates (m )),
271
+ operation .OpTypeBeforeUpdate ,
272
+ )
273
+ require .Nil (t , err )
274
+ require .Zero (t , model .ID )
275
+ require .Zero (t , model .CreatedAt )
276
+ require .Zero (t , model .UpdatedAt )
277
+
278
+ cfg := & PluginConfig {
279
+ EnableDefaultFieldHook : true ,
280
+ }
281
+ InitPlugin (cfg )
282
+
283
+ err = callback .GetCallback ().Execute (
284
+ context .Background (),
285
+ operation .NewOpContext (nil , operation .WithDoc (model ), operation .WithUpdates (m )),
286
+ operation .OpTypeBeforeUpdate ,
287
+ )
288
+ require .Nil (t , err )
289
+ require .Zero (t , model .ID )
290
+ require .Zero (t , model .CreatedAt )
291
+ require .NotZero (t , model .UpdatedAt )
292
+ require .Equal (t , bson.M {
293
+ "$set" : bson.M {
294
+ "updated_at" : model .UpdatedAt ,
295
+ },
296
+ }, m )
297
+
298
+ RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeInsert )
299
+ RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpdate )
260
300
RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpsert )
261
301
})
262
302
t .Run ("beforeUpsert" , func (t * testing.T ) {
@@ -299,6 +339,7 @@ func TestPluginInit_EnableEnableDefaultFieldHook(t *testing.T) {
299
339
}, m )
300
340
301
341
RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeInsert )
342
+ RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpdate )
302
343
RemovePlugin ("mongox:default_field" , operation .OpTypeBeforeUpsert )
303
344
})
304
345
}
@@ -315,6 +356,26 @@ func (t *testModelHookStruct) AfterInsert(_ context.Context) error {
315
356
return nil
316
357
}
317
358
359
+ func (t * testModelHookStruct ) BeforeDelete (_ context.Context ) error {
360
+ * t ++
361
+ return nil
362
+ }
363
+
364
+ func (t * testModelHookStruct ) AfterDelete (_ context.Context ) error {
365
+ * t ++
366
+ return nil
367
+ }
368
+
369
+ func (t * testModelHookStruct ) BeforeUpdate (_ context.Context ) error {
370
+ * t ++
371
+ return nil
372
+ }
373
+
374
+ func (t * testModelHookStruct ) AfterUpdate (_ context.Context ) error {
375
+ * t ++
376
+ return nil
377
+ }
378
+
318
379
func (t * testModelHookStruct ) BeforeUpsert (_ context.Context ) error {
319
380
* t ++
320
381
return nil
@@ -325,6 +386,11 @@ func (t *testModelHookStruct) AfterUpsert(_ context.Context) error {
325
386
return nil
326
387
}
327
388
389
+ func (t * testModelHookStruct ) BeforeFind (_ context.Context ) error {
390
+ * t ++
391
+ return nil
392
+ }
393
+
328
394
func (t * testModelHookStruct ) AfterFind (_ context.Context ) error {
329
395
* t ++
330
396
return nil
@@ -392,6 +458,82 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
392
458
wantErr : nil ,
393
459
want : 2 ,
394
460
},
461
+ {
462
+ name : "beforeDelete" ,
463
+ ctx : context .Background (),
464
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
465
+ return []operation.OpContextOption {
466
+ operation .WithModelHook (tm ),
467
+ }
468
+ },
469
+ opType : operation .OpTypeBeforeDelete ,
470
+ wantErr : nil ,
471
+ want : 1 ,
472
+ },
473
+ {
474
+ name : "afterDelete" ,
475
+ ctx : context .Background (),
476
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
477
+ return []operation.OpContextOption {
478
+ operation .WithModelHook (tm ),
479
+ }
480
+ },
481
+ opType : operation .OpTypeAfterDelete ,
482
+ wantErr : nil ,
483
+ want : 1 ,
484
+ },
485
+ {
486
+ name : "beforeUpdate" ,
487
+ ctx : context .Background (),
488
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
489
+ return []operation.OpContextOption {
490
+ operation .WithUpdates (tm ),
491
+ }
492
+ },
493
+ opType : operation .OpTypeBeforeUpdate ,
494
+ wantErr : nil ,
495
+ want : 1 ,
496
+ },
497
+ {
498
+ name : "beforeUpdate with model hook" ,
499
+ ctx : context .Background (),
500
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
501
+ * tm = 1
502
+ return []operation.OpContextOption {
503
+ operation .WithUpdates (new (testModelHookStruct )),
504
+ operation .WithModelHook (tm ),
505
+ }
506
+ },
507
+ opType : operation .OpTypeBeforeUpdate ,
508
+ wantErr : nil ,
509
+ want : 2 ,
510
+ },
511
+ {
512
+ name : "afterUpdate" ,
513
+ ctx : context .Background (),
514
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
515
+ return []operation.OpContextOption {
516
+ operation .WithUpdates (tm ),
517
+ }
518
+ },
519
+ opType : operation .OpTypeAfterUpdate ,
520
+ wantErr : nil ,
521
+ want : 1 ,
522
+ },
523
+ {
524
+ name : "afterUpdate with model hook" ,
525
+ ctx : context .Background (),
526
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
527
+ * tm = 1
528
+ return []operation.OpContextOption {
529
+ operation .WithUpdates (new (testModelHookStruct )),
530
+ operation .WithModelHook (tm ),
531
+ }
532
+ },
533
+ opType : operation .OpTypeAfterUpdate ,
534
+ wantErr : nil ,
535
+ want : 2 ,
536
+ },
395
537
{
396
538
name : "beforeUpsert" ,
397
539
ctx : context .Background (),
@@ -444,6 +586,18 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
444
586
wantErr : nil ,
445
587
want : 2 ,
446
588
},
589
+ {
590
+ name : "beforeFind" ,
591
+ ctx : context .Background (),
592
+ ocOption : func (tm * testModelHookStruct ) []operation.OpContextOption {
593
+ return []operation.OpContextOption {
594
+ operation .WithModelHook (tm ),
595
+ }
596
+ },
597
+ opType : operation .OpTypeBeforeFind ,
598
+ wantErr : nil ,
599
+ want : 1 ,
600
+ },
447
601
{
448
602
name : "afterFind" ,
449
603
ctx : context .Background (),
@@ -500,8 +654,13 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
500
654
func remoteModelPlugin () {
501
655
RemovePlugin ("mongox:model" , operation .OpTypeBeforeInsert )
502
656
RemovePlugin ("mongox:model" , operation .OpTypeAfterInsert )
657
+ RemovePlugin ("mongox:model" , operation .OpTypeBeforeDelete )
658
+ RemovePlugin ("mongox:model" , operation .OpTypeAfterDelete )
659
+ RemovePlugin ("mongox:model" , operation .OpTypeBeforeUpdate )
660
+ RemovePlugin ("mongox:model" , operation .OpTypeAfterUpdate )
503
661
RemovePlugin ("mongox:model" , operation .OpTypeBeforeUpsert )
504
662
RemovePlugin ("mongox:model" , operation .OpTypeAfterUpsert )
663
+ RemovePlugin ("mongox:model" , operation .OpTypeBeforeFind )
505
664
RemovePlugin ("mongox:model" , operation .OpTypeAfterFind )
506
665
}
507
666
0 commit comments