@@ -22,6 +22,15 @@ type User struct {
22
22
DeletedAt * time.Time
23
23
}
24
24
25
+ type UUIDUser struct {
26
+ UUID string `db:"uuid,primary"`
27
+ Name string
28
+ Age int
29
+ CreatedAt time.Time
30
+ UpdatedAt time.Time
31
+ DeletedAt * time.Time
32
+ }
33
+
25
34
type Transaction struct {
26
35
ID int
27
36
Item string
@@ -206,3 +215,113 @@ func TestChangesetApply_constraint(t *testing.T) {
206
215
UpdatedAt : now ,
207
216
}, user )
208
217
}
218
+
219
+ //If PK is explicitly caseted then it should be updated
220
+ func TestChangesetApply_updatePK (t * testing.T ) {
221
+ var (
222
+ user UUIDUser
223
+ now = time .Now ().Truncate (time .Second )
224
+ doc = rel .NewDocument (& user )
225
+ input = params.Map {
226
+ "uuid" : "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ,
227
+ "name" : "Luffy" ,
228
+ "age" : 20 ,
229
+ }
230
+ userMutation = rel .Apply (rel .NewDocument (& UUIDUser {}),
231
+ rel .Set ("uuid" , "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ),
232
+ rel .Set ("name" , "Luffy" ),
233
+ rel .Set ("age" , 20 ),
234
+ rel .Set ("created_at" , now ),
235
+ rel .Set ("updated_at" , now ),
236
+ )
237
+ )
238
+
239
+ ch := Cast (user , input , []string {"name" , "age" , "uuid" })
240
+ UniqueConstraint (ch , "name" )
241
+ mut := rel .Apply (doc , ch )
242
+
243
+ assert .Nil (t , ch .Error ())
244
+ assert .Equal (t , userMutation .Mutates , mut .Mutates )
245
+ assert .NotNil (t , mut .ErrorFunc )
246
+ assert .Equal (t , UUIDUser {
247
+ UUID : "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ,
248
+ Name : "Luffy" ,
249
+ Age : 20 ,
250
+ CreatedAt : now ,
251
+ UpdatedAt : now ,
252
+ }, user )
253
+ }
254
+
255
+ //Covert function should ignore the PK updates for safety reasons
256
+ func TestChangesetApply_updatePK_fromConvert (t * testing.T ) {
257
+ var (
258
+ user UUIDUser
259
+ now = time .Now ().Truncate (time .Second )
260
+ doc = rel .NewDocument (& user )
261
+ input = struct {
262
+ UUID string
263
+ Name string
264
+ Age int
265
+ }{
266
+ UUID : "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ,
267
+ Name : "Luffy" ,
268
+ Age : 20 ,
269
+ }
270
+ userMutation = rel .Apply (rel .NewDocument (& UUIDUser {}),
271
+ rel .Set ("name" , "Luffy" ),
272
+ rel .Set ("age" , 20 ),
273
+ rel .Set ("created_at" , now ),
274
+ rel .Set ("updated_at" , now ),
275
+ )
276
+ )
277
+
278
+ ch := Convert (input )
279
+ UniqueConstraint (ch , "name" )
280
+ mut := rel .Apply (doc , ch )
281
+
282
+ assert .Nil (t , ch .Error ())
283
+ assert .Equal (t , userMutation .Mutates , mut .Mutates )
284
+ assert .NotNil (t , mut .ErrorFunc )
285
+ assert .Equal (t , UUIDUser {
286
+ Name : "Luffy" ,
287
+ Age : 20 ,
288
+ CreatedAt : now ,
289
+ UpdatedAt : now ,
290
+ }, user )
291
+ }
292
+
293
+ //If PK is explicitly set in the Change function it should be updated
294
+ func TestChangesetApply_updatePK_fromChange (t * testing.T ) {
295
+ var (
296
+ user UUIDUser
297
+ now = time .Now ().Truncate (time .Second )
298
+ doc = rel .NewDocument (& user )
299
+ input = map [string ]interface {}{
300
+ "uuid" : "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ,
301
+ "name" : "Luffy" ,
302
+ "age" : 20 ,
303
+ }
304
+ userMutation = rel .Apply (rel .NewDocument (& UUIDUser {}),
305
+ rel .Set ("uuid" , "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ),
306
+ rel .Set ("name" , "Luffy" ),
307
+ rel .Set ("age" , 20 ),
308
+ rel .Set ("created_at" , now ),
309
+ rel .Set ("updated_at" , now ),
310
+ )
311
+ )
312
+
313
+ ch := Change (user , input )
314
+ UniqueConstraint (ch , "name" )
315
+ mut := rel .Apply (doc , ch )
316
+
317
+ assert .Nil (t , ch .Error ())
318
+ assert .Equal (t , userMutation .Mutates , mut .Mutates )
319
+ assert .NotNil (t , mut .ErrorFunc )
320
+ assert .Equal (t , UUIDUser {
321
+ UUID : "3a90fc96-6cff-4914-9ce8-01c9e607b28b" ,
322
+ Name : "Luffy" ,
323
+ Age : 20 ,
324
+ CreatedAt : now ,
325
+ UpdatedAt : now ,
326
+ }, user )
327
+ }
0 commit comments