@@ -3,7 +3,7 @@ var Tree = require('../lib/tree');
3
3
var Async = require ( 'async' ) ;
4
4
var should = require ( 'should' ) ;
5
5
var _ = require ( 'lodash' ) ;
6
-
6
+ var shortId = require ( 'shortid' ) ;
7
7
8
8
var Schema = Mongoose . Schema ;
9
9
@@ -12,11 +12,29 @@ Mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/mongoose-
12
12
13
13
describe ( 'tree tests' , function ( ) {
14
14
15
- // Schema for tests
16
- var UserSchema = new Schema ( {
15
+ var userSchema = {
17
16
name : String
18
- } ) ;
19
- UserSchema . plugin ( Tree , { pathSeparator : '.' } ) ;
17
+ } ;
18
+
19
+ var pluginOptions = {
20
+ pathSeparator : '.'
21
+ } ;
22
+
23
+ if ( process . env . MONGOOSE_TREE_SHORTID === '1' ) {
24
+ userSchema . _id = {
25
+ type : String ,
26
+ unique : true ,
27
+ 'default' : function ( ) {
28
+ return shortId . generate ( ) ;
29
+ }
30
+ } ;
31
+
32
+ pluginOptions . idType = String
33
+ }
34
+
35
+ // Schema for tests
36
+ var UserSchema = new Schema ( userSchema ) ;
37
+ UserSchema . plugin ( Tree , pluginOptions ) ;
20
38
var User = Mongoose . model ( 'User' , UserSchema ) ;
21
39
22
40
// Set up the fixture
@@ -77,6 +95,7 @@ describe('tree tests', function () {
77
95
User . findOne ( { name : 'Emily' } , function ( err , emily ) {
78
96
79
97
emily . remove ( function ( err ) {
98
+
80
99
should . not . exist ( err ) ;
81
100
82
101
User . find ( function ( err , users ) {
@@ -190,6 +209,7 @@ describe('tree tests', function () {
190
209
should . not . exist ( err ) ;
191
210
192
211
adam . getChildren ( function ( err , users ) {
212
+
193
213
should . not . exist ( err ) ;
194
214
195
215
users . length . should . equal ( 2 ) ;
@@ -276,8 +296,8 @@ describe('tree tests', function () {
276
296
User . findOne ( { 'name' : 'Dann' } , function ( err , dann ) {
277
297
278
298
dann . getAncestors ( function ( err , ancestors ) {
279
- should . not . exist ( err ) ;
280
299
300
+ should . not . exist ( err ) ;
281
301
ancestors . length . should . equal ( 2 ) ;
282
302
_ . pluck ( ancestors , 'name' ) . should . include ( 'Carol' ) . and . include ( 'Adam' ) ;
283
303
done ( ) ;
@@ -329,8 +349,28 @@ describe('tree tests', function () {
329
349
330
350
should . not . exist ( err ) ;
331
351
childrenTree . length . should . equal ( 2 ) ;
332
- childrenTree [ 0 ] . children [ 1 ] . children [ 0 ] . children [ 0 ] . name . should . equal ( 'Emily' ) ;
333
- childrenTree [ 0 ] . children [ 1 ] . children [ 0 ] . children . length . should . equal ( 1 ) ;
352
+
353
+ var adamTree = _ . find ( childrenTree , function ( x ) { return x . name == 'Adam' } ) ;
354
+ var edenTree = _ . find ( childrenTree , function ( x ) { return x . name == 'Eden' } ) ;
355
+
356
+ var bobTree = _ . find ( adamTree . children , function ( x ) { return x . name == 'Bob' } ) ;
357
+
358
+ var carolTree = _ . find ( adamTree . children , function ( x ) { return x . name == 'Carol' } ) ;
359
+ var danTree = _ . find ( carolTree . children , function ( x ) { return x . name == 'Dann' } ) ;
360
+ var emilyTree = _ . find ( danTree . children , function ( x ) { return x . name == 'Emily' } ) ;
361
+
362
+
363
+ adamTree . children . length . should . equal ( 2 ) ;
364
+ edenTree . children . length . should . equal ( 0 ) ;
365
+
366
+ bobTree . children . length . should . equal ( 0 ) ;
367
+
368
+ carolTree . children . length . should . equal ( 1 ) ;
369
+
370
+ danTree . children . length . should . equal ( 1 ) ;
371
+ danTree . children [ 0 ] . name . should . equal ( 'Emily' ) ;
372
+
373
+ emilyTree . children . length . should . equal ( 0 ) ;
334
374
done ( ) ;
335
375
} ) ;
336
376
} ) ;
@@ -342,9 +382,19 @@ describe('tree tests', function () {
342
382
adam . getChildrenTree ( function ( err , childrenTree ) {
343
383
344
384
should . not . exist ( err ) ;
345
- childrenTree . length . should . equal ( 2 ) ;
346
- childrenTree [ 1 ] . children [ 0 ] . children [ 0 ] . name . should . equal ( 'Emily' ) ;
347
- childrenTree [ 1 ] . children [ 0 ] . children . length . should . equal ( 1 ) ;
385
+
386
+ var bobTree = _ . find ( childrenTree , function ( x ) { return x . name == 'Bob' } ) ;
387
+
388
+ var carolTree = _ . find ( childrenTree , function ( x ) { return x . name == 'Carol' } ) ;
389
+ var danTree = _ . find ( carolTree . children , function ( x ) { return x . name == 'Dann' } ) ;
390
+ var emilyTree = _ . find ( danTree . children , function ( x ) { return x . name == 'Emily' } ) ;
391
+
392
+ bobTree . children . length . should . equal ( 0 ) ;
393
+ carolTree . children . length . should . equal ( 1 ) ;
394
+ danTree . children . length . should . equal ( 1 ) ;
395
+ danTree . children [ 0 ] . name . should . equal ( 'Emily' ) ;
396
+ emilyTree . children . length . should . equal ( 0 ) ;
397
+
348
398
done ( ) ;
349
399
} ) ;
350
400
} ) ;
0 commit comments