Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding tests for ancestorTree.method

  • Loading branch information...
commit 45535dfa4af65740bddc4209f1935376b3063d06 1 parent ed9c3fa
@mariuskubilius authored
Showing with 119 additions and 41 deletions.
  1. +119 −41 test/ancestorTree.test.js
View
160 test/ancestorTree.test.js
@@ -1,5 +1,6 @@
//dependencies
var assert = require('assert');
+var async = require('async');
var mongoose = require('mongoose');
//mongoose.set('debug', true);
var ancestorTree = require('../lib/ancestorTree');
@@ -31,29 +32,39 @@ describe('ancestorTree', function () {
});
it('should save and set hierarchy where aplicable', function (done) {
- parent.save(function(err, doc){
- assert.strictEqual(err, null);
- assert.strictEqual(typeof doc.parent, 'undefined');
- assert.strictEqual(typeof doc.ancestors, 'object');
- assert.strictEqual(doc.parent, undefined);
- assert.strictEqual(doc.ancestors.length, 0);
- child.save(function(err,doc){
- assert.strictEqual(err, null);
- assert.strictEqual(typeof doc.parent, 'object');
- assert.strictEqual(doc.parent, parent._id);
- assert.strictEqual(typeof doc.ancestors, 'object');
- assert.strictEqual(doc.ancestors.length, 1);
- assert.strictEqual(doc.ancestors[0], parent._id);
+ async.waterfall([
+ function(cb){
+ parent.save(function(err, doc){
+ assert.strictEqual(typeof doc.parent, 'undefined', 'parent should be not set');
+ assert.strictEqual(typeof doc.ancestors, 'object', 'ancestors should be array');
+ assert.strictEqual(doc.parent, undefined, 'no parent should be set');
+ assert.strictEqual(doc.ancestors.length, 0, 'ancestors array should be empty');
+ cb(err);
+ });
+ },
+ function(cb){
+ child.save(function(err,doc){
+ assert.strictEqual(typeof doc.parent, 'object');
+ assert.strictEqual(doc.parent, parent._id);
+ assert.strictEqual(typeof doc.ancestors, 'object');
+ assert.strictEqual(doc.ancestors.length, 1);
+ assert.strictEqual(doc.ancestors[0], parent._id);
+ cb(err);
+ });
+ },
+ function(cb){
subChild.save(function(err, doc){
- assert.strictEqual(err, null, 'should be no errors thrown');
assert.strictEqual(doc.ancestors.length, 2, 'should be length of 2');
assert.strictEqual(doc.ancestors[1], child._id, 'second item should be last added');
- done();
+ cb(err);
});
- });
+ }
+ ],
+ function(err){
+ assert.strictEqual(err, null, 'should be no errors thrown');
+ done();
});
});
-
});
describe('#update()', function(){
@@ -73,35 +84,102 @@ describe('ancestorTree', function () {
});
it ('should save and update ancestors.', function (done) {
- p.save(function(err,doc){
- assert.strictEqual(err, null, 'item without parent should save.');
- c.save(function(err, doc){
+ async.waterfall([
+ function(cb) {
+ p.save(function(err,doc) {
+ cb(err);
+ });
+ },
+ function(cb) {
+ c.save(function(err,doc) {
+ cb(err);
+ });
+ },
+ function(cb) {
sc.save(function(err,doc) {
- ap.save(function(err,doc) {
- ac.save(function(err,doc) {
- asc.save(function(err,doc) {
- assert.strictEqual(err, null, 'should be no errors while saving.');
- ac.parent = c;
- ac.save(function(err,doc){
- assert.strictEqual(err, null, 'should be no errors while saving.');
- MockModel.findOne({_id: asc._id}, function(err, doc){
- assert.strictEqual(doc.ancestors[0].toString(), p._id.toString(), 'hierarchy should be updated to the new tree');
- assert.strictEqual(doc.ancestors[1].toString(), c._id.toString(), 'hierarchy should be updated in children');
- assert.strictEqual(doc.ancestors.length, 3)
- c.parent = undefined;
- c.save(function(err,doc){
- assert.strictEqual(err, null);
- done();
- });
- });
- })
- });
- });
- });
+ cb(err);
+ });
+ },
+ function(cb) {
+ ap.save(function(err,doc) {
+ cb(err);
+ });
+ },
+ function(cb) {
+ ac.save(function(err,doc) {
+ cb(err);
+ });
+ },
+ function(cb) {
+ asc.save(function(err,doc) {
+ cb(err);
});
+ },
+ function(cb){
+ ac.parent = c;
+ ac.save(function(err,doc){
+ MockModel.findOne({_id: asc._id}, function(err, doc){
+ assert.strictEqual(doc.ancestors[0].toString(), p._id.toString(), 'hierarchy should be updated to the new tree');
+ assert.strictEqual(doc.ancestors[1].toString(), c._id.toString(), 'hierarchy should be updated in children');
+ assert.strictEqual(doc.ancestors.length, 3, 'there should be 3 ancestors');
+ cb(err);
+ });
+ });
+ },
+ function(cb){
+ c.parent = undefined;
+ c.save(function(err,doc){
+ MockModel.findOne({_id: sc._id}, function(err, doc){
+ assert.strictEqual(doc.ancestors.length, 1, 'there should be only one ancestor left');
+ assert.strictEqual(c._id.toString(), doc.ancestors[0].toString(), 'ancestores are being changed correctly');
+ cb(err);
+ });
+ });
+ }
+ ],
+ function(err){
+ assert.strictEqual(err, null, 'should be no errors');
+ done();
+ });
+ });
+
+ it('should retrieve children.', function(done){
+ MockModel.findOne({_id: c._id}, function(err, doc){
+ async.waterfall([
+ function(cb) {
+ doc.findDirectChildren({fields:{_id:1}}, function(err, children){
+ assert.strictEqual(children.length, 2, 'there should be 2 direct children');
+ cb(err);
+ });
+ },
+ function(cb) {
+ doc.findChildren({fields:{_id:1}}, function(err, children){
+ assert.strictEqual(children.length, 3, 'there should be 3 children');
+ assert.strictEqual(children[0]._id.toString(), sc._id.toString());
+ assert.strictEqual(children[1]._id.toString(), ac._id.toString());
+ assert.strictEqual(children[2]._id.toString(), asc._id.toString());
+ cb(err);
+ });
+ }
+
+ ],
+ function(err){
+ assert.strictEqual(err, null, 'should be no errors');
+ done();
});
+
});
+ });
+ it('should retrieve ancestors.', function(done){
+ MockModel.findOne({_id: asc._id}, function(err, doc){
+ doc.getAncestors({fields:'_id'}, function(err, ancestors) {
+ assert.strictEqual(ancestors.length, 2, 'asc is 2 levels deep');
+ assert.strictEqual(ancestors[0]._id.toString(), c._id.toString());
+ assert.strictEqual(ancestors[1]._id.toString(), ac._id.toString());
+ done();
+ });
+ });
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.