Permalink
Browse files

adding static methods for publishDocument, unpublishDocument, trashDo…

…cument and test cases
  • Loading branch information...
1 parent 265eb7f commit 7b5401ba7d2012403f0a57a571844b8f14c94ede @mariuskubilius committed Jul 8, 2013
Showing with 107 additions and 6 deletions.
  1. +40 −1 lib/status.js
  2. +4 −3 test/ancestorTree.test.js
  3. +63 −2 test/status.test.js
View
41 lib/status.js
@@ -9,7 +9,46 @@ var mongoose = require('mongoose');
status = function (schema) {
+ var fields = {};
+ if (!schema.paths.status) {
+ fields.status = {
+ type: Number,
+ default: 0,
+ };
+ }
+ schema.add(fields);
+ schema.statics = {
+ /**
+ * Publish document according to _id.
+ * @param {mongoid} [id] id of the document to be published
+ * @param {function} [cb] callback function
+ */
+ publishDocument: function(id, cb) {
+ this.findByIdAndUpdate(id, {status: 1})
+ .exec(cb);
+ },
+
+ /**
+ * Un-Publish document according to _id.
+ * @param {mongoid} [id] id of the document to be published
+ * @param {function} [cb] callback function
+ */
+ unPublishDocument: function(id, cb) {
+ this.findByIdAndUpdate(id, {status: 0})
+ .exec(cb);
+ },
+
+ /**
+ * Trash document according to _id.
+ * @param {mongoid} [id] id of the document to be published
+ * @param {function} [cb] callback function
+ */
+ trashDocument: function(id, cb) {
+ this.findByIdAndUpdate(id, {status: 2})
+ .exec(cb);
+ }
+ }
}
-module.epxorts = status();
+module.exports = status;
View
7 test/ancestorTree.test.js
@@ -37,17 +37,18 @@ describe('ancestorTree', function () {
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.equal(doc.parent, undefined, 'no parent should be set');
+ 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(err, null, 'No errors while saving');
+ 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.length, 1, 'the node is a children.');
assert.strictEqual(doc.ancestors[0], parent._id);
cb(err);
});
View
65 test/status.test.js
@@ -3,7 +3,68 @@ var assert = require('assert');
var async = require('async');
var mongoose = require('mongoose');
//mongoose.set('debug', true);
-var ancestorTree = require('../lib/ancestorTree');
+var status = require('../lib/status');
var common = require('./utils/common');
var db = common.db;
-var Schema = mongoose.Schema;
+var Schema = mongoose.Schema;
+
+describe ('status', function () {
+ var MockSchema = new Schema();
+ MockSchema.plugin(status);
+ var MockModel = db.model('statusMock', MockSchema);
+ var doc = new MockModel();
+
+ describe('#schema', function () {
+ before(function () {
+ MockModel.remove(function (err) {
+ assert.strictEqual(err, null, 'There should be no errors while deleting status stuff');
+ });
+ });
+
+ it('Should set status field in schema', function (done) {
+ assert.strictEqual(typeof MockSchema.paths.status, 'object', 'Status field is set in schema');
+ done();
+ });
+
+ it('Should set field value to zero as default', function (done) {
+ assert.strictEqual(doc.status, 0, 'document status equals to 0');
+ done();
+ });
+ });
+
+ describe('#Statics', function () {
+ //publish
+ before( function () {
+ doc.save(function(err) {
+ assert.strictEqual(err, null, 'There should be no errors');
+ });
+ });
+
+ it('Should publish the document', function (done) {
+ assert.strictEqual(typeof MockSchema.statics.publishDocument, 'function', 'publishDocument method exists');
+ MockModel.publishDocument(doc._id, function (err, result) {
+ assert.strictEqual(err, null, 'There should be no errors thrown');
+ assert.strictEqual(result.status, 1, 'Status of document strictly equals to 1');
+ done();
+ });
+ });
+ // unpublish
+ it('Should un-publish the document', function (done) {
+ assert.strictEqual(typeof MockSchema.statics.unPublishDocument, 'function', 'unPublishDocument method exists');
+ MockModel.unPublishDocument(doc._id, function (err, result) {
+ assert.strictEqual(err, null, 'There should be no errors thrown');
+ assert.strictEqual(result.status, 0, 'Status of document strictly equals to 0');
+ done();
+ });
+ });
+ // Trash
+ it('Should trash the document', function (done) {
+ assert.strictEqual(typeof MockSchema.statics.trashDocument, 'function', 'trashDocument method exists');
+ MockModel.trashDocument(doc._id, function (err, result) {
+ assert.strictEqual(err, null, 'There should be no errors thrown');
+ assert.strictEqual(result.status, 2, 'Status of document strictly equals to 2');
+ done();
+ });
+ });
+ });
+});

0 comments on commit 7b5401b

Please sign in to comment.