From 5dcbee626b0a501800b8379a1e54e7ac3c0b1167 Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Thu, 2 Jul 2015 11:31:50 +0200 Subject: [PATCH] added mongoData tests --- lib/mongoData.js | 19 ++++--- test/test.mongoData.js | 114 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 8 deletions(-) diff --git a/lib/mongoData.js b/lib/mongoData.js index 3149365..efbd802 100644 --- a/lib/mongoData.js +++ b/lib/mongoData.js @@ -12,7 +12,7 @@ module.exports = function (config, db, shareModel) { function getMongoContent(options, callback) { config.debug && config.debug('getMongoContent options', options); if (!options.collection) { - return callback(new Error('Data not found / missing collection')); + return callback && callback(new Error('Data not found / missing collection')); } return db.collection(options.collection, function collection(err, col) { if (err) { @@ -20,7 +20,7 @@ module.exports = function (config, db, shareModel) { return callback && callback(err); } if (!options.query) { - return callback && callback(new Error('Data not found ' + options.collection + '/ missing query'), null, col); + return callback && callback(new Error('Data not found ' + options.collection + ' / missing query'), null, col); } if (options.query._id && !(options.query._id instanceof Object)) { try { @@ -52,11 +52,14 @@ module.exports = function (config, db, shareModel) { */ function getMongoAttribute(options, callback) { config.debug && config.debug('getMongoAttribute options', options); - return getMongoContent(options, function document(err, result) { + return getMongoContent(options, function document(err, result, col) { if (err) { config.error && config.error('ERR1 getMongoAttribute', err); return callback && callback(err); } + if (!options.attribute) { + return callback && callback(new Error('Data not found / ' + options.collection + '/' + JSON.stringify(options.query) + ' missing attribute', null, col)); + } var attribute_options = null; config.debug && config.debug('getMongoAttribute result', result); if (result && @@ -68,13 +71,13 @@ module.exports = function (config, db, shareModel) { }; config.debug && config.debug('getMongoAttribute attribute_options', attribute_options); - getMongoContent(attribute_options, function attribute(err, attribute_result) { + getMongoContent(attribute_options, function attribute(err, attribute_result, coll) { if (err) { config.error && config.error('ERR2 getMongoAttribute', err); return callback && callback(err); } config.debug && config.debug('getMongoAttribute attribute_result', attribute_result); - return callback && callback(err, attribute_result); + return callback && callback(err, attribute_result, coll); }); } else { config.debug && config.debug('getMongoAttribute try direct lookup'); @@ -87,13 +90,13 @@ module.exports = function (config, db, shareModel) { }; config.debug && config.debug('getMongoAttribute attribute_options', attribute_options); - return getMongoContent(attribute_options, function attribute(err, attribute_result) { + return getMongoContent(attribute_options, function attribute(err, attribute_result, coll) { if (err) { config.error && config.error('ERR getMongoAttribute', err); return callback && callback(err); } config.debug && config.debug('getMongoAttribute direct attribute_result', attribute_result); - return callback && callback(err, attribute_result); + return callback && callback(err, attribute_result, coll); }); } }); @@ -334,7 +337,7 @@ module.exports = function (config, db, shareModel) { /* options: - * no_share (optional): prevent share to update itself. + * no_share (optional): prevent share from updating itself. */ function setMongoAttribute(data, options, callback) { config.debug && config.debug('setMongoAttribute options', options); diff --git a/test/test.mongoData.js b/test/test.mongoData.js index 27469e3..29c6365 100644 --- a/test/test.mongoData.js +++ b/test/test.mongoData.js @@ -11,6 +11,9 @@ var config = { debug: function () { //console.log(arguments); }, + info: function () { + //console.log(arguments); + }, error: function () { //console.error(arguments); } @@ -104,4 +107,115 @@ describe('mongoData', function () { ); } }); + + describe('getMongoContentAttribute', function () { + var ok = { + _id: '345678901234567890123456', + parent: '456789012345678901234567', + name: 'test.content_attribute' + }; + function findOne(q, cb) { + if (q._id && q._id.toString() === '123456789012345678901234') { + return cb(null, { + _id: '123456789012345678901234', + name: 'test', + error_attribute: { + guid: '234567890123456789012345' + }, + content_attribute: { + guid: '345678901234567890123456' + } + }); + } + if (q._id && q._id.toString() === '234567890123456789012345') { + return cb(new Error(q)); + } + if (q._id && q._id.toString() === '345678901234567890123456') { + return cb(null, ok); + } + if (q._id && q._id.toString() === '456789012345678901234567') { + return cb(null, { + _id: '456789012345678901234567', + name: 'test' + }); + } + if (q.parent && q.parent.toString() === '456789012345678901234567') { + if (q.name === 'test.content_attribute'){ + return cb(null, ok); + } else { + return cb(new Error(q)); + } + } + throw new Error('fail:' + JSON.stringify(arguments)); + } + + var col = {findOne: findOne}; + var db = { + collection: function (c, cb) { + //console.log('collection arguments', arguments); + if (c === 'test_error') { + return cb(new Error(c)); + } + return cb(null, col); + } + }; + var shareModel = {}; + var option_list = [ + {}, // no collection + {query: 'q'}, // no collection + {collection: 'col'}, // no query + { + collection: 'no_attr', + query: {_id: '123456789012345678901234'} // no attribute + }, + { + collection: 'col', // trigger null result + query: {_id: '123456789012345678901234'}, + attribute: 'error_attribute' + }, + { + collection: 'ok', // trigger 'ok' result + query: {_id: '123456789012345678901234'}, + attribute: 'content_attribute' + + }, + { + collection: 'col', // trigger error result + query: {_id: '456789012345678901234567'}, + attribute: 'error_attribute' + }, + { + collection: 'ok', // trigger 'ok' result + query: {_id: '456789012345678901234567'}, + attribute: 'content_attribute' + } + ]; + var mongoDataInstance = mongoData(config, db, shareModel); + var i; + + function testArguments(options) { + return function (done) { + mongoDataInstance.getMongoAttribute(options, function (err, result, coll) { + if (options.collection === 'ok') { + //console.log('result',result,'err',err,'coll',coll); + expect(result).to.equal(ok); + expect(err).to.not.be.ok; + expect(coll).to.equal(col); + done(); + } else { + //console.log('err',err); + expect(err).to.be.instanceOf(Error); + expect(result).to.not.be.ok; + done(); + } + }); + }; + } + + for (i = 0; i < option_list.length; i += 1) { + it('should handle arguments correctly ' + JSON.stringify(option_list[i]), + testArguments(option_list[i]) + ); + } + }); });