@@ -12,66 +12,67 @@ let mongoose = require('mongoose'),
12
12
let UserSchema ,
13
13
User ;
14
14
15
- before ( function ( done ) {
16
- async . series ( [
17
- function ( callback ) {
18
- mongoose . connect ( 'mongodb://localhost/nested_set_test' , { useNewUrlParser : true } ) ;
19
- mongoose . set ( 'useCreateIndex' , true ) ;
20
- callback ( ) ;
21
- } ,
22
- function ( callback ) {
23
- UserSchema = new Schema ( {
24
- username : { type : String }
25
- } ) ;
26
- UserSchema . plugin ( NestedSetPlugin ) ;
27
- User = mongoose . model ( 'User' , UserSchema ) ;
28
- callback ( null ) ;
29
- } ,
30
- function ( callback ) {
31
- // see diagram in docs/test_tree.png for a representation of this tree
32
- let michael = new User ( { username : 'michael' } ) ;
33
-
34
- let meredith = new User ( { username : 'meredith' , parentId : michael . _id } ) ;
35
- let jim = new User ( { username : 'jim' , parentId : michael . _id } ) ;
36
- let angela = new User ( { username : 'angela' , parentId : michael . _id } ) ;
37
-
38
- let kelly = new User ( { username : 'kelly' , parentId : meredith . _id } ) ;
39
- let creed = new User ( { username : 'creed' , parentId : meredith . _id } ) ;
40
-
41
- let phyllis = new User ( { username : 'phyllis' , parentId : jim . _id } ) ;
42
- let stanley = new User ( { username : 'stanley' , parentId : jim . _id } ) ;
43
- let dwight = new User ( { username : 'dwight' , parentId : jim . _id } ) ;
44
-
45
- let oscar = new User ( { username : 'oscar' , parentId : angela . _id } ) ;
46
-
47
- async . eachSeries ( [
48
- michael ,
49
- meredith ,
50
- jim ,
51
- angela ,
52
- kelly ,
53
- creed ,
54
- phyllis ,
55
- stanley ,
56
- dwight ,
57
- oscar
58
- ] , function ( item , cb ) {
59
- item . save ( cb ) ;
60
- } , callback ) ;
61
- }
62
- ] , function ( err , results ) {
63
- if ( ! err ) done ( ) ;
15
+
16
+ describe ( 'NestedSet' , function ( ) {
17
+ before ( function ( done ) {
18
+ async . series ( [
19
+ function ( callback ) {
20
+ mongoose . connect ( 'mongodb://localhost/nested_set_test' , { useNewUrlParser : true } ) ;
21
+ mongoose . set ( 'useCreateIndex' , true ) ;
22
+ callback ( ) ;
23
+ } ,
24
+ function ( callback ) {
25
+ UserSchema = new Schema ( {
26
+ username : { type : String }
27
+ } ) ;
28
+ UserSchema . plugin ( NestedSetPlugin ) ;
29
+ User = mongoose . model ( 'User' , UserSchema ) ;
30
+ callback ( null ) ;
31
+ } ,
32
+ function ( callback ) {
33
+ // see diagram in docs/test_tree.png for a representation of this tree
34
+ let michael = new User ( { username : 'michael' } ) ;
35
+
36
+ let meredith = new User ( { username : 'meredith' , parentId : michael . _id } ) ;
37
+ let jim = new User ( { username : 'jim' , parentId : michael . _id } ) ;
38
+ let angela = new User ( { username : 'angela' , parentId : michael . _id } ) ;
39
+
40
+ let kelly = new User ( { username : 'kelly' , parentId : meredith . _id } ) ;
41
+ let creed = new User ( { username : 'creed' , parentId : meredith . _id } ) ;
42
+
43
+ let phyllis = new User ( { username : 'phyllis' , parentId : jim . _id } ) ;
44
+ let stanley = new User ( { username : 'stanley' , parentId : jim . _id } ) ;
45
+ let dwight = new User ( { username : 'dwight' , parentId : jim . _id } ) ;
46
+
47
+ let oscar = new User ( { username : 'oscar' , parentId : angela . _id } ) ;
48
+
49
+ async . eachSeries ( [
50
+ michael ,
51
+ meredith ,
52
+ jim ,
53
+ angela ,
54
+ kelly ,
55
+ creed ,
56
+ phyllis ,
57
+ stanley ,
58
+ dwight ,
59
+ oscar
60
+ ] , function ( item , cb ) {
61
+ item . save ( cb ) ;
62
+ } , callback ) ;
63
+ }
64
+ ] , function ( err , results ) {
65
+ if ( ! err ) done ( ) ;
66
+ } ) ;
64
67
} ) ;
65
- } ) ;
66
68
67
- after ( function ( done ) {
68
- mongoose . connection . collections . users . drop ( function ( err ) {
69
- mongoose . disconnect ( ) ;
69
+ after ( function ( done ) {
70
+ mongoose . connection . collections . users . drop ( function ( err ) {
71
+ mongoose . disconnect ( ) ;
72
+ } ) ;
73
+ done ( ) ;
70
74
} ) ;
71
- done ( ) ;
72
- } ) ;
73
75
74
- describe ( 'Normal nested set' , function ( ) {
75
76
it ( 'is same' , function ( done ) {
76
77
assert . ok ( User ) ;
77
78
assert . equal ( 'function' , typeof User ) ;
@@ -467,14 +468,14 @@ describe('Normal nested set', function () {
467
468
} ) ;
468
469
469
470
it ( 'removing a node to a built tree should re-arrange the tree correctly' , function ( done ) {
470
- User . findOne ( { username : 'michael' } , function ( err , michael ) {
471
- User . rebuildTree ( michael , 1 , function ( ) {
472
- User . findOne ( { username : 'creed' } , function ( err , creed ) {
473
- creed . remove ( function ( ) {
474
- User . find ( function ( err , users ) {
471
+ User . findOne ( { username : 'michael' } , function ( err , michael ) {
472
+ User . rebuildTree ( michael , 1 , function ( ) {
473
+ User . findOne ( { username : 'creed' } , function ( err , creed ) {
474
+ creed . remove ( function ( ) {
475
+ User . find ( function ( err , users ) {
475
476
// see docs/test_tree_after_leaf_insertion.png for the graphical representation of this tree
476
477
// with lft/rgt values after the insertion
477
- users . forEach ( function ( person ) {
478
+ users . forEach ( function ( person ) {
478
479
if ( person . username === 'michael' ) {
479
480
assert . equal ( 1 , person . lft ) ;
480
481
assert . equal ( 18 , person . rgt ) ;
0 commit comments