@@ -344,6 +344,164 @@ func TestRebuild(t *testing.T) {
344
344
assertNodeEqual (t , lilysDresses , 4 , 5 , 1 , 0 , lilysClothing .ID )
345
345
}
346
346
347
+ func TestRebuildBatched (t * testing.T ) {
348
+ const batchSize = 5
349
+ initData ()
350
+ affectedCount , err := RebuildBatched (db , clothing , true , batchSize )
351
+ assert .NoError (t , err )
352
+ assert .Equal (t , 0 , affectedCount )
353
+ reloadCategories ()
354
+
355
+ assertNodeEqual (t , clothing , 1 , 22 , 0 , 2 , 0 )
356
+ assertNodeEqual (t , mens , 2 , 9 , 1 , 1 , clothing .ID )
357
+ assertNodeEqual (t , suits , 3 , 8 , 2 , 2 , mens .ID )
358
+ assertNodeEqual (t , slacks , 4 , 5 , 3 , 0 , suits .ID )
359
+ assertNodeEqual (t , jackets , 6 , 7 , 3 , 0 , suits .ID )
360
+ assertNodeEqual (t , womens , 10 , 21 , 1 , 3 , clothing .ID )
361
+ assertNodeEqual (t , dresses , 11 , 16 , 2 , 2 , womens .ID )
362
+ assertNodeEqual (t , eveningGowns , 12 , 13 , 3 , 0 , dresses .ID )
363
+ assertNodeEqual (t , sunDresses , 14 , 15 , 3 , 0 , dresses .ID )
364
+ assertNodeEqual (t , skirts , 17 , 18 , 2 , 0 , womens .ID )
365
+ assertNodeEqual (t , blouses , 19 , 20 , 2 , 0 , womens .ID )
366
+
367
+ sunDresses .Rgt = 123
368
+ sunDresses .Lft = 12
369
+ sunDresses .Depth = 1
370
+ sunDresses .ChildrenCount = 100
371
+ err = db .Updates (& sunDresses ).Error
372
+ assert .NoError (t , err )
373
+ reloadCategories ()
374
+ assertNodeEqual (t , sunDresses , 12 , 123 , 1 , 100 , dresses .ID )
375
+
376
+ affectedCount , err = RebuildBatched (db , clothing , true , batchSize )
377
+ assert .NoError (t , err )
378
+ assert .Equal (t , 2 , affectedCount )
379
+ reloadCategories ()
380
+
381
+ assertNodeEqual (t , clothing , 1 , 22 , 0 , 2 , 0 )
382
+ assertNodeEqual (t , mens , 2 , 9 , 1 , 1 , clothing .ID )
383
+ assertNodeEqual (t , suits , 3 , 8 , 2 , 2 , mens .ID )
384
+ assertNodeEqual (t , slacks , 4 , 5 , 3 , 0 , suits .ID )
385
+ assertNodeEqual (t , jackets , 6 , 7 , 3 , 0 , suits .ID )
386
+ assertNodeEqual (t , womens , 10 , 21 , 1 , 3 , clothing .ID )
387
+ assertNodeEqual (t , dresses , 11 , 16 , 2 , 2 , womens .ID )
388
+ assertNodeEqual (t , eveningGowns , 14 , 15 , 3 , 0 , dresses .ID )
389
+ assertNodeEqual (t , sunDresses , 12 , 13 , 3 , 0 , dresses .ID )
390
+ assertNodeEqual (t , skirts , 17 , 18 , 2 , 0 , womens .ID )
391
+ assertNodeEqual (t , blouses , 19 , 20 , 2 , 0 , womens .ID )
392
+
393
+ affectedCount , err = RebuildBatched (db , clothing , true , batchSize )
394
+ assert .NoError (t , err )
395
+ assert .Equal (t , 0 , affectedCount )
396
+ reloadCategories ()
397
+
398
+ assertNodeEqual (t , clothing , 1 , 22 , 0 , 2 , 0 )
399
+ assertNodeEqual (t , mens , 2 , 9 , 1 , 1 , clothing .ID )
400
+ assertNodeEqual (t , suits , 3 , 8 , 2 , 2 , mens .ID )
401
+ assertNodeEqual (t , slacks , 4 , 5 , 3 , 0 , suits .ID )
402
+ assertNodeEqual (t , jackets , 6 , 7 , 3 , 0 , suits .ID )
403
+ assertNodeEqual (t , womens , 10 , 21 , 1 , 3 , clothing .ID )
404
+ assertNodeEqual (t , dresses , 11 , 16 , 2 , 2 , womens .ID )
405
+ assertNodeEqual (t , eveningGowns , 14 , 15 , 3 , 0 , dresses .ID )
406
+ assertNodeEqual (t , sunDresses , 12 , 13 , 3 , 0 , dresses .ID )
407
+ assertNodeEqual (t , skirts , 17 , 18 , 2 , 0 , womens .ID )
408
+ assertNodeEqual (t , blouses , 19 , 20 , 2 , 0 , womens .ID )
409
+
410
+ hat := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
411
+ "Title" : "Hat" ,
412
+ "ParentID" : sql.NullInt64 {Valid : false },
413
+ }).(* Category )
414
+
415
+ affectedCount , err = RebuildBatched (db , clothing , false , batchSize )
416
+ assert .NoError (t , err )
417
+ assert .Equal (t , 1 , affectedCount )
418
+
419
+ affectedCount , err = RebuildBatched (db , clothing , true , batchSize )
420
+ assert .NoError (t , err )
421
+ assert .Equal (t , 1 , affectedCount )
422
+ reloadCategories ()
423
+ hat , _ = findNode (db , hat .ID )
424
+
425
+ assertNodeEqual (t , clothing , 1 , 22 , 0 , 2 , 0 )
426
+ assertNodeEqual (t , mens , 2 , 9 , 1 , 1 , clothing .ID )
427
+ assertNodeEqual (t , suits , 3 , 8 , 2 , 2 , mens .ID )
428
+ assertNodeEqual (t , slacks , 4 , 5 , 3 , 0 , suits .ID )
429
+ assertNodeEqual (t , jackets , 6 , 7 , 3 , 0 , suits .ID )
430
+ assertNodeEqual (t , womens , 10 , 21 , 1 , 3 , clothing .ID )
431
+ assertNodeEqual (t , dresses , 11 , 16 , 2 , 2 , womens .ID )
432
+ assertNodeEqual (t , eveningGowns , 14 , 15 , 3 , 0 , dresses .ID )
433
+ assertNodeEqual (t , sunDresses , 12 , 13 , 3 , 0 , dresses .ID )
434
+ assertNodeEqual (t , skirts , 17 , 18 , 2 , 0 , womens .ID )
435
+ assertNodeEqual (t , blouses , 19 , 20 , 2 , 0 , womens .ID )
436
+ assertNodeEqual (t , hat , 23 , 24 , 0 , 0 , 0 )
437
+
438
+ jacksClothing := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
439
+ "Title" : "Jack's Clothing" ,
440
+ "ParentID" : sql.NullInt64 {Valid : false },
441
+ "UserType" : "User" ,
442
+ "UserID" : 8686 ,
443
+ }).(* Category )
444
+ jacksSuits := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
445
+ "Title" : "Jack's Suits" ,
446
+ "ParentID" : sql.NullInt64 {Valid : true , Int64 : jacksClothing .ID },
447
+ "UserType" : "User" ,
448
+ "UserID" : 8686 ,
449
+ }).(* Category )
450
+ jacksHat := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
451
+ "Title" : "Jack's Hat" ,
452
+ "UserType" : "User" ,
453
+ "UserID" : 8686 ,
454
+ "ParentID" : sql.NullInt64 {Valid : false },
455
+ }).(* Category )
456
+ jacksSlacks := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
457
+ "Title" : "Jack's Slacks" ,
458
+ "ParentID" : sql.NullInt64 {Valid : true , Int64 : jacksClothing .ID },
459
+ "UserType" : "User" ,
460
+ "UserID" : 8686 ,
461
+ }).(* Category )
462
+
463
+ lilysHat := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
464
+ "Title" : "Lily's Hat" ,
465
+ "UserType" : "User" ,
466
+ "UserID" : 6666 ,
467
+ "ParentID" : sql.NullInt64 {Valid : false },
468
+ }).(* Category )
469
+ lilysClothing := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
470
+ "Title" : "Lily's Clothing" ,
471
+ "ParentID" : sql.NullInt64 {Valid : false },
472
+ "UserType" : "User" ,
473
+ "UserID" : 6666 ,
474
+ }).(* Category )
475
+ lilysDresses := * CategoryFactory .MustCreateWithOption (map [string ]interface {}{
476
+ "Title" : "Lily's Dresses" ,
477
+ "ParentID" : sql.NullInt64 {Valid : true , Int64 : lilysClothing .ID },
478
+ "UserType" : "User" ,
479
+ "UserID" : 6666 ,
480
+ }).(* Category )
481
+
482
+ affectedCount , err = RebuildBatched (db , jacksSuits , true , batchSize )
483
+ assert .NoError (t , err )
484
+ assert .Equal (t , 4 , affectedCount )
485
+ affectedCount , err = RebuildBatched (db , lilysHat , true , batchSize )
486
+ assert .NoError (t , err )
487
+ assert .Equal (t , 3 , affectedCount )
488
+ jacksClothing , _ = findNode (db , jacksClothing .ID )
489
+ jacksSuits , _ = findNode (db , jacksSuits .ID )
490
+ jacksSlacks , _ = findNode (db , jacksSlacks .ID )
491
+ jacksHat , _ = findNode (db , jacksHat .ID )
492
+ lilysHat , _ = findNode (db , lilysHat .ID )
493
+ lilysClothing , _ = findNode (db , lilysClothing .ID )
494
+ lilysDresses , _ = findNode (db , lilysDresses .ID )
495
+
496
+ assertNodeEqual (t , jacksClothing , 1 , 6 , 0 , 2 , 0 )
497
+ assertNodeEqual (t , jacksSuits , 2 , 3 , 1 , 0 , jacksClothing .ID )
498
+ assertNodeEqual (t , jacksSlacks , 4 , 5 , 1 , 0 , jacksClothing .ID )
499
+ assertNodeEqual (t , jacksHat , 7 , 8 , 0 , 0 , 0 )
500
+ assertNodeEqual (t , lilysHat , 1 , 2 , 0 , 0 , 0 )
501
+ assertNodeEqual (t , lilysClothing , 3 , 6 , 0 , 1 , 0 )
502
+ assertNodeEqual (t , lilysDresses , 4 , 5 , 1 , 0 , lilysClothing .ID )
503
+ }
504
+
347
505
func TestMoveToLeft (t * testing.T ) {
348
506
// case 1
349
507
initData ()
0 commit comments