@@ -95,6 +95,51 @@ func TestBeforeInsert(t *testing.T) {
9595 require .NotEqual (t , u .CreateSecondTime , u .UpdateSecondTime )
9696 },
9797 },
98+ {
99+ name : "not inlined struct in user2" ,
100+ doc : reflect .ValueOf (& user2 {}).Elem (),
101+ currentTime : time .Now (),
102+ fields : field .ParseFields (& user2 {}),
103+ wantErr : nil ,
104+ validateFunc : func (t * testing.T , v any ) {
105+ u , ok := v .(user2 )
106+ require .True (t , ok )
107+ require .NotZero (t , u .ID )
108+ require .NotZero (t , u .CreatedAt )
109+ require .NotZero (t , u .UpdatedAt )
110+ require .NotZero (t , u .CreateSecondTime )
111+ require .NotZero (t , u .CreateMilliTime )
112+ require .NotZero (t , u .CreateNanoTime )
113+ require .NotZero (t , u .UpdateSecondTime )
114+ require .NotZero (t , u .UpdateMilliTime )
115+ require .NotZero (t , u .UpdateNanoTime )
116+ },
117+ },
118+ {
119+ name : "not inlined struct with CreatedAt and UpdateSecondTime in user2" ,
120+ doc : reflect .ValueOf (& user2 {
121+ CreatedAt : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (),
122+ UpdateSecondTime : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (),
123+ }).Elem (),
124+ currentTime : time .Now (),
125+ fields : field .ParseFields (& user2 {}),
126+ wantErr : nil ,
127+ validateFunc : func (t * testing.T , v any ) {
128+ u , ok := v .(user2 )
129+ require .True (t , ok )
130+ require .NotZero (t , u .ID )
131+ require .NotZero (t , u .CreatedAt )
132+ require .NotZero (t , u .UpdatedAt )
133+ require .NotEqual (t , u .CreatedAt , u .UpdatedAt )
134+ require .NotZero (t , u .CreateSecondTime )
135+ require .NotZero (t , u .CreateMilliTime )
136+ require .NotZero (t , u .CreateNanoTime )
137+ require .NotZero (t , u .UpdateSecondTime )
138+ require .NotZero (t , u .UpdateMilliTime )
139+ require .NotZero (t , u .UpdateNanoTime )
140+ require .NotEqual (t , u .CreateSecondTime , u .UpdateSecondTime )
141+ },
142+ },
98143 {
99144 name : "inlined struct" ,
100145 doc : reflect .ValueOf (& inlinedUser {}).Elem (),
@@ -215,6 +260,20 @@ func Test_beforeUpdate(t *testing.T) {
215260 fields : field .ParseFields (& user {}),
216261 want : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ), "update_second_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (), "update_milli_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_nano_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixNano ()}},
217262 },
263+ {
264+ name : "a bson.M updates and additional fields" ,
265+ updates : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" }},
266+ currentTime : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
267+ fields : field .ParseFields (& user2 {}),
268+ want : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_second_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (), "update_milli_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_nano_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixNano ()}},
269+ },
270+ {
271+ name : "a bson.M updates with updateTime and additional fields" ,
272+ updates : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC )}},
273+ currentTime : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
274+ fields : field .ParseFields (& user2 {}),
275+ want : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC ), "update_second_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (), "update_milli_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_nano_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixNano ()}},
276+ },
218277 {
219278 name : "a bson.M updates and additional-inlined fields" ,
220279 updates : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" }},
@@ -276,6 +335,13 @@ func Test_beforeUpsert(t *testing.T) {
276335 fields : field .ParseFields (& user {}),
277336 want : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ), "update_second_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (), "update_milli_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_nano_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixNano ()}, "$setOnInsert" : "invalid" },
278337 },
338+ {
339+ name : "a bson.M updates with invalid $setOnInsert" ,
340+ updates : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" }, "$setOnInsert" : "invalid" },
341+ currentTime : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
342+ fields : field .ParseFields (& user2 {}),
343+ want : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" , "updated_at" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_second_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Unix (), "update_milli_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixMilli (), "update_nano_time" : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).UnixNano ()}, "$setOnInsert" : "invalid" },
344+ },
279345 {
280346 name : "a bson.M updates" ,
281347 updates : bson.M {"$set" : bson.M {"name" : "Mingyong Chen" }},
0 commit comments