@@ -1227,9 +1227,10 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
12271227 beforeHook []beforeHookFn [TestUser ]
12281228 afterHook []afterHookFn [TestUser ]
12291229
1230- ctx context.Context
1231- want * TestUser
1232- wantErr error
1230+ ctx context.Context
1231+ want * TestUser
1232+ wantErr error
1233+ resultIsUpdated bool
12331234 }{
12341235 {
12351236 name : "nil document" ,
@@ -1248,8 +1249,9 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
12481249
12491250 finder .filter = bson.D {}
12501251 },
1251- filter : query .Eq ("name" , "burt" ),
1252- wantErr : mongo .ErrNilDocument ,
1252+ filter : query .Eq ("name" , "burt" ),
1253+ wantErr : mongo .ErrNilDocument ,
1254+ resultIsUpdated : false ,
12531255 },
12541256 {
12551257 name : "find by name and update age" ,
@@ -1262,6 +1264,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
12621264 require .NotNil (t , insertOneResult .InsertedID )
12631265 },
12641266 after : func (ctx context.Context , t * testing.T ) {
1267+ result , err := finder .Filter (query .Eq ("name" , "Mingyong Chen" )).FindOne (ctx )
1268+ require .NotZero (t , result .UpdatedAt )
12651269 deleteOneResult , err := collection .DeleteOne (ctx , query .Eq ("name" , "Mingyong Chen" ))
12661270 require .NoError (t , err )
12671271 require .Equal (t , int64 (1 ), deleteOneResult .DeletedCount )
@@ -1275,6 +1279,7 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
12751279 Name : "Mingyong Chen" ,
12761280 Age : 24 ,
12771281 },
1282+ resultIsUpdated : true ,
12781283 },
12791284 {
12801285 name : "global before hook error" ,
@@ -1290,7 +1295,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
12901295 },
12911296 },
12921297 },
1293- wantErr : errors .New ("global before hook error" ),
1298+ wantErr : errors .New ("global before hook error" ),
1299+ resultIsUpdated : false ,
12941300 },
12951301 {
12961302 name : "global after hook error" ,
@@ -1321,7 +1327,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
13211327 },
13221328 },
13231329 },
1324- wantErr : errors .New ("global after hook error" ),
1330+ wantErr : errors .New ("global after hook error" ),
1331+ resultIsUpdated : false ,
13251332 },
13261333 {
13271334 name : "global before and after hook" ,
@@ -1351,7 +1358,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
13511358 if opCtx .Filter .(bson.D )[0 ].Key != "name" || opCtx .Filter .(bson.D )[0 ].Value .(bson.D )[0 ].Value != "Mingyong Chen" {
13521359 return errors .New ("filter error" )
13531360 }
1354- if opCtx .Updates .(bson.D )[0 ].Value .(bson.D )[0 ].Key != "age" || opCtx .Updates .(bson.D )[0 ].Value .(bson.D )[0 ].Value != 24 {
1361+
1362+ if opCtx .Updates .(bson.M )["$set" ].(bson.M )["age" ].(int32 ) != 24 {
13551363 return errors .New ("updates error" )
13561364 }
13571365 return nil
@@ -1365,6 +1373,7 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
13651373 if user .Name != "Mingyong Chen" || user .Age != 24 {
13661374 return errors .New ("result error" )
13671375 }
1376+ require .NotZero (t , user .UpdatedAt )
13681377 return nil
13691378 },
13701379 },
@@ -1373,6 +1382,7 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
13731382 Name : "Mingyong Chen" ,
13741383 Age : 24 ,
13751384 },
1385+ resultIsUpdated : true ,
13761386 },
13771387 {
13781388 name : "before hook error" ,
@@ -1384,7 +1394,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
13841394 return errors .New ("before hook error" )
13851395 },
13861396 },
1387- wantErr : errors .New ("before hook error" ),
1397+ wantErr : errors .New ("before hook error" ),
1398+ resultIsUpdated : false ,
13881399 },
13891400 {
13901401 name : "after hook error" ,
@@ -1411,7 +1422,8 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
14111422 return errors .New ("after hook error" )
14121423 },
14131424 },
1414- wantErr : errors .New ("after hook error" ),
1425+ wantErr : errors .New ("after hook error" ),
1426+ resultIsUpdated : false ,
14151427 },
14161428 {
14171429 name : "before and after hook" ,
@@ -1438,7 +1450,7 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
14381450 if opCtx .Filter .(bson.D )[0 ].Key != "name" || opCtx .Filter .(bson.D )[0 ].Value .(bson.D )[0 ].Value != "Mingyong Chen" {
14391451 return errors .New ("filter error" )
14401452 }
1441- if opCtx .Updates .(bson.D )[ 0 ]. Value . (bson.D )[ 0 ]. Key != "age" || opCtx . Updates .(bson. D )[ 0 ]. Value .(bson. D )[ 0 ]. Value != 24 {
1453+ if opCtx .Updates .(bson.M )[ "$set" ]. (bson.M )[ "age" ].( int32 ) != 24 {
14421454 return errors .New ("updates error" )
14431455 }
14441456 return nil
@@ -1450,13 +1462,15 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
14501462 if user .Name != "Mingyong Chen" || user .Age != 24 {
14511463 return errors .New ("after error" )
14521464 }
1465+ require .NotZero (t , user .UpdatedAt )
14531466 return nil
14541467 },
14551468 },
14561469 want : & TestUser {
14571470 Name : "Mingyong Chen" ,
14581471 Age : 24 ,
14591472 },
1473+ resultIsUpdated : true ,
14601474 },
14611475 }
14621476
@@ -1473,6 +1487,10 @@ func TestFinder_e2e_FindOneAndUpdate(t *testing.T) {
14731487 require .Equal (t , tc .wantErr , err )
14741488 if err == nil {
14751489 tc .want .ID = user .ID
1490+ if tc .resultIsUpdated {
1491+ require .NotZero (t , user .UpdatedAt )
1492+ tc .want .UpdatedAt = user .UpdatedAt
1493+ }
14761494 require .Equal (t , tc .want , user )
14771495 }
14781496 for _ , hook := range tc .globalHook {
0 commit comments