Permalink
Browse files

Initial preparation for tests

  • Loading branch information...
1 parent db9cf48 commit ace7723a2ad3dc7074c357d0fcc70b37e6eacdca @luccastera luccastera committed Dec 28, 2011
Showing with 163 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 index.js
  3. +12 −0 lib/nested_set.js
  4. +32 −0 package.json
  5. +17 −0 tests/helpers.js
  6. +97 −0 tests/nested_set_test.js
View
@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log
View
@@ -0,0 +1,3 @@
+'use strict'
+
+module.exports = require('./lib/nested_set');
View
@@ -0,0 +1,12 @@
+'use strict'
+
+var mongoose = require('mongoose'),
+ Schema = mongoose.Schema;
+
+var NestedSetPlugin = function(schema, options) {
+ schema.add({ lft: {type: Number, min: 0} });
+ schema.add({ rgt: {type: Number, min: 0} });
+ schema.add({ parentId: {type: Schema.ObjectId} });
+}
+
+module.exports = NestedSetPlugin;
View
@@ -0,0 +1,32 @@
+{
+ "author": "Luc Castera <luc.castera@gmail.com> (https://github.com/dambalah)",
+ "name": "mongoose-nested-set",
+ "description": "A mongoose plugin implementing the nested set pattern for mongoose models",
+ "homepage": "https://github.com/groupdock/mongoose-nested-set",
+ "keywords": [
+ "mongoose",
+ "mongodb",
+ "nested set",
+ "nested-set",
+ "nested set pattern"
+ ],
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/groupdock/mongoose-nested-set.git"
+ },
+ "main": "index.js",
+ "scripts": {
+ "test": "nodeunit tests/*_test.js"
+ },
+ "engines": {
+ "node": "< 0.7.x"
+ },
+ "dependencies": {
+ "mongoose": "~2.4.5",
+ "step": "~0.0.5"
+ },
+ "devDependencies": {
+ "nodeunit": "~0.6.4"
+ }
+}
View
@@ -0,0 +1,17 @@
+var ObjectId = require('mongoose').Types.ObjectId
+
+exports.isObjectId = function(id) {
+ if (!id) {return false}
+ try {
+ if ('string' === typeof id) {
+ new ObjectId(id)
+ } else {
+ new ObjectId(id.toString())
+ }
+ } catch (err) {
+ console.log(err);
+ return false
+ }
+ return true
+}
+
@@ -0,0 +1,97 @@
+'use strict'
+
+var testCase = require('nodeunit').testCase,
+ mongoose = require('mongoose'),
+ NestedSetPlugin = require('../lib/nested_set'),
+ Schema = mongoose.Schema,
+ Step = require('step'),
+ Helpers = require('./helpers');
+
+var UserSchema,
+ User
+
+var tests = testCase({
+ setUp: function(callback) {
+ Step(
+ function() {
+ mongoose.connect('mongodb://localhost/nested_set_test');
+ this();
+ },
+ function() {
+ UserSchema = new Schema({
+ username: {type: String}
+ });
+ UserSchema.plugin(NestedSetPlugin);
+ User = mongoose.model('User', UserSchema);
+ this();
+ },
+ function() {
+ // drop users from mongodb
+ User.remove({}, this);
+ },
+ function() {
+ var michael = new User({username: 'michael'});
+
+ var meredith = new User({username: 'meredith', parentId: michael._id});
+ var jim = new User({username: 'jim', parentId: michael._id});
+ var angela = new User({username: 'angela', parentId: michael._id});
+
+ var kelly = new User({username: 'kelly', parentId: meredith._id});
+ var creed = new User({username: 'creed', parentId: meredith._id});
+
+ var phyllis = new User({username: 'phyllis', parentId: jim._id});
+ var stanley = new User({username: 'stanley', parentId: jim._id});
+ var dwight = new User({username: 'dwight', parentId: jim._id});
+
+ var oscar = new User({username: 'oscar', parentId: angela._id});
+
+ michael.save(this.parallel());
+ meredith.save(this.parallel());
+ jim.save(this.parallel());
+ angela.save(this.parallel());
+ kelly.save(this.parallel());
+ creed.save(this.parallel());
+ phyllis.save(this.parallel());
+ stanley.save(this.parallel());
+ dwight.save(this.parallel());
+ oscar.save(this.parallel());
+ },
+ function(err) {
+ if (err) { console.log(err); }
+ callback();
+ }
+ )
+ },
+ 'is sane': function(test) {
+ test.expect(3);
+ test.ok(User);
+ test.equal('function', typeof User);
+ test.equal('User', User.modelName);
+ test.done();
+ },
+ 'has created users for testing': function(test) {
+ test.expect(4);
+ User.find(function(err, users) {
+ if (err) { console.log(err); }
+ test.ok(!err);
+ test.ok(users);
+ test.ok(users instanceof Array);
+ test.equal(10, users.length);
+ test.done();
+ });
+ },
+ 'can read parentIds as ObjectIDs': function(test) {
+ User.find(function(err, users) {
+ if (err) { console.log(err); }
+ test.ok(!err);
+ users.forEach(function(user) {
+ if (user.parentId) {
+ test.ok(Helpers.isObjectId(user.parentId));
+ }
+ });
+ test.done();
+ });
+ }
+});
+
+module.exports = tests;

0 comments on commit ace7723

Please sign in to comment.