Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed #54 let coverage up to 89%

  • Loading branch information...
commit 80129b89ff883382dac8a1b942ef57e5866ebe4b 1 parent b4fe5d8
@fengmk2 fengmk2 authored
View
43 .jshintrc
@@ -0,0 +1,43 @@
+{
+ "predef": [
+ "phantom",
+ "module",
+ "require",
+ "__dirname",
+ "process",
+ "console",
+ "it",
+ "describe",
+ "before",
+ "beforeEach",
+ "after",
+ "should",
+ "rewire",
+ "$"
+ ],
+
+ "browser": true,
+ "node" : true,
+ "es5": true,
+ "bitwise": true,
+ "curly": true,
+ "eqeqeq": true,
+ "forin": false,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "noempty": true,
+ "nonew": true,
+ "plusplus": false,
+ "undef": true,
+ "strict": true,
+ "trailing": false,
+ "globalstrict": true,
+ "nonstandard": true,
+ "white": false,
+ "indent": 2,
+ "expr": true,
+ "multistr": true,
+ "onevar": false
+}
View
2  Readme.md
@@ -8,7 +8,7 @@ All api is same to mongodb-native, please see the [document](http://christkv.git
## Test
* test results: [test_results.md](https://github.com/kissjs/node-mongoskin/blob/master/test_results.md)
-* jscoverage: [**70%**](http://fengmk2.github.com/coverage/mongoskin.html)
+* jscoverage: [**89%**](http://fengmk2.github.com/coverage/mongoskin.html)
## Test pass [mongodb] versions
View
62 lib/mongoskin/admin.js
@@ -1,35 +1,39 @@
-var Admin = require('mongodb').Admin
- , utils = require('./utils');
+/*!
+ * mongoskin - admin.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
-var SkinAdmin = exports.SkinAdmin = function(skinDb) {
- this.skinDb = skinDb;
-}
+"use strict";
+
+/**
+ * Module dependencies.
+ */
-SkinAdmin.prototype.open = function(callback) {
- if(this.admin) return callback(null, this.admin);
- this.skinDb.open(function(err, db){
- if(err) return callback(err);
- this.admin = new Admin(db);
- callback(null, this.admin);
- })
-}
+var Admin = require('mongodb').Admin;
+var utils = require('./utils');
-var bindSkin = function(name, method) {
- SkinAdmin.prototype[name] = function() {
- var args = arguments.length > 0 ? Array.prototype.slice.call(arguments, 0) : [];
- return this.open(function(err, admin) {
- if (err) {
- utils.error(err, args, 'SkinAdmin.' + name);
- } else {
- method.apply(admin, args);
- }
- });
- };
+var SkinAdmin = exports.SkinAdmin = function (skinDb) {
+ this.skinDb = skinDb;
+ this.admin = null;
};
-for (var name in Admin.prototype) {
- var method = Admin.prototype[name];
- bindSkin(name, method);
-}
+SkinAdmin.prototype.open = function (callback) {
+ if (this.admin) {
+ return callback(null, this.admin);
+ }
+ this.skinDb.open(function (err, db) {
+ if (err) {
+ return callback(err);
+ }
+ this.admin = new Admin(db);
+ callback(null, this.admin);
+ }.bind(this));
+};
-exports.SkinAdmin = SkinAdmin;
+for (var key in Admin.prototype) {
+ var method = Admin.prototype[key];
+ utils.bindSkin('SkinAdmin', SkinAdmin, 'admin', key, method);
+}
View
65 lib/mongoskin/collection.js
@@ -1,3 +1,17 @@
+/*!
+ * mongoskin - collection.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
/**
bind these methods from Collection.prototype to Provider
@@ -24,14 +38,15 @@
group
options
*/
-var __slice = Array.prototype.slice,
- events = require('events'),
- Collection = require('mongodb').Collection,
- SkinCursor = require('./cursor').SkinCursor,
- utils = require('./utils'),
- STATE_CLOSE = 0,
- STATE_OPENNING = 1,
- STATE_OPEN = 2;
+var __slice = Array.prototype.slice;
+var events = require('events');
+var Collection = require('mongodb').Collection;
+var SkinCursor = require('./cursor').SkinCursor;
+var utils = require('./utils');
+var constant = require('./constant');
+var STATE_CLOSE = constant.STATE_CLOSE;
+var STATE_OPENNING = constant.STATE_OPENNING;
+var STATE_OPEN = constant.STATE_OPEN;
/**
* Construct SkinCollection from SkinDb and collectionName
@@ -41,20 +56,20 @@ var __slice = Array.prototype.slice,
* @param collectionName
*
*/
-var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
+var SkinCollection = exports.SkinCollection = function (skinDb, collectionName) {
this.skinDb = skinDb;
this.ObjectID = this.skinDb.ObjectID;
this.collectionName = collectionName;
- this.collection;
+ this.collection = null;
this.state = STATE_CLOSE;
- this.internalHint;
+ this.internalHint = null;
var that = this;
this.__defineGetter__('hint', function () {
return this.internalHint;
});
this.__defineSetter__('hint', function (value) {
this.internalHint = value;
- this.open(function(err, collection) {
+ this.open(function (err, collection) {
collection.hint = value;
that.internalHint = collection.hint;
});
@@ -63,25 +78,9 @@ var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
this.emitter = new events.EventEmitter();
};
-/**
- * bind method of mongodb.Collection to mongoskin.SkinCollection
- */
-var bindSkin = function(name, method) {
- SkinCollection.prototype[name] = function() {
- var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
- this.open(function(err, collection) {
- if (err) {
- utils.error(err, args, 'SkinCollection.' + name);
- } else {
- method.apply(collection, args);
- }
- });
- };
-};
-
-for (var name in Collection.prototype) {
- var method = Collection.prototype[name];
- bindSkin(name, method);
+for (var _name in Collection.prototype) {
+ var method = Collection.prototype[_name];
+ utils.bindSkin('SkinCollection', SkinCollection, 'collection', _name, method);
}
/*
@@ -103,12 +102,12 @@ SkinCollection.prototype.open = function(fn) {
var that = this;
this.emitter.once('open', fn);
this.state = STATE_OPENNING;
- this.skinDb.open(function(err, db) {
+ this.skinDb.open(function (err, db) {
if (err) {
that.state = STATE_CLOSE;
return that.emitter.emit('open', err, null);
}
- that.skinDb.db.collection(that.collectionName, function (err, collection) {
+ db.collection(that.collectionName, function (err, collection) {
if (collection) {
that.state = STATE_OPEN;
that.collection = collection;
View
15 lib/mongoskin/constant.js
@@ -0,0 +1,15 @@
+/*!
+ * mongoskin - constant.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+exports.DEFAULT_PORT = 27017;
+
+exports.STATE_CLOSE = 0;
+exports.STATE_OPENNING = 1;
+exports.STATE_OPEN = 2;
View
54 lib/mongoskin/cursor.js
@@ -1,19 +1,31 @@
-var __slice = Array.prototype.slice,
- events = require('events'),
- Cursor = require('mongodb').Cursor,
- utils = require('./utils'),
- STATE_CLOSE = 0,
- STATE_OPENNING = 1,
- STATE_OPEN = 2;
+/*!
+ * mongoskin - cursor.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var EventEmitter = require('events').EventEmitter;
+var Cursor = require('mongodb').Cursor;
+var utils = require('./utils');
+var constant = require('./constant');
+var STATE_CLOSE = constant.STATE_CLOSE;
+var STATE_OPENNING = constant.STATE_OPENNING;
+var STATE_OPEN = constant.STATE_OPEN;
var SkinCursor = exports.SkinCursor = function (cursor, skinCollection, args) {
this.cursor = cursor;
this.skinCollection = skinCollection;
- this.args = args;
- this.emitter = new events.EventEmitter();
- if (!cursor) {
- this.state = STATE_CLOSE;
- } else {
+ this.args = args || [];
+ this.emitter = new EventEmitter();
+ this.state = STATE_CLOSE;
+ if (cursor) {
this.state = STATE_OPEN;
}
};
@@ -37,7 +49,7 @@ SkinCursor.prototype.open = function (fn) {
}
// copy args
var args = that.args.slice();
- args.push(function(err, cursor) {
+ args.push(function (err, cursor) {
if (cursor) {
that.state = STATE_OPEN;
that.cursor = cursor;
@@ -50,20 +62,6 @@ SkinCursor.prototype.open = function (fn) {
}
};
-var bindSkin = function (name, method) {
- SkinCursor.prototype[name] = function () {
- var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
- this.open(function (err, cursor) {
- if (err) {
- utils.error(err, args, 'SkinCursor.' + name);
- } else {
- method.apply(cursor, args);
- }
- });
- return this;
- };
-};
-
[
// callbacks
'toArray', 'each', 'count', 'nextObject', 'getMore', 'explain',
@@ -73,5 +71,5 @@ var bindSkin = function (name, method) {
//'rewind', 'close' ,...
].forEach(function (name) {
var method = Cursor.prototype[name];
- bindSkin(name, method);
+ utils.bindSkin('SkinCursor', SkinCursor, 'cursor', name, method);
});
View
84 lib/mongoskin/db.js
@@ -1,29 +1,43 @@
-var __slice = Array.prototype.slice,
- mongodb = require('mongodb'),
- events = require('events'),
- utils = require('./utils'),
- SkinAdmin = require('./admin').SkinAdmin,
- SkinCollection = require('./collection').SkinCollection,
- SkinGridStore = require('./gridfs').SkinGridStore,
- Db = mongodb.Db,
-
- STATE_CLOSE = 0,
- STATE_OPENNING = 1,
- STATE_OPEN = 2;
-
-var _extend = function(destination, source) {
+/*!
+ * mongoskin - db.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var __slice = Array.prototype.slice;
+var mongodb = require('mongodb');
+var EventEmitter = require('events').EventEmitter;
+var utils = require('./utils');
+var SkinAdmin = require('./admin').SkinAdmin;
+var SkinCollection = require('./collection').SkinCollection;
+var SkinGridStore = require('./gridfs').SkinGridStore;
+var Db = mongodb.Db;
+var constant = require('./constant');
+var STATE_CLOSE = constant.STATE_CLOSE;
+var STATE_OPENNING = constant.STATE_OPENNING;
+var STATE_OPEN = constant.STATE_OPEN;
+
+var _extend = function (destination, source) {
for (var property in source) {
- destination[property] = source[property];
+ destination[property] = source[property];
}
return destination;
};
-var SkinDb = exports.SkinDb = function(db, username, password) {
+var SkinDb = exports.SkinDb = function (db, username, password) {
this.db = db;
this.username = username;
this.password = password;
this.state = STATE_CLOSE;
- this.emitter = new events.EventEmitter();
+ this.emitter = new EventEmitter();
this.emitter.setMaxListeners(100);
this.admin = new SkinAdmin(this);
this._collections = {};
@@ -43,10 +57,9 @@ SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) {
/**
- * retrieve native_db
- *
- * @param fn function(err, native_db)
+ * Open the database connection.
*
+ * @param {Function(err, native_db)} [callback]
*/
SkinDb.prototype.open = function (callback) {
switch (this.state) {
@@ -91,7 +104,7 @@ SkinDb.prototype.open = function (callback) {
};
/**
- * Cloase the db instance.
+ * Close the database connection.
*
* @param {Function(err)} callback
* @api public
@@ -197,30 +210,6 @@ SkinDb.prototype.bind = function (collectionName, options) {
return this.bind(name, coll);
};
-/**
- * Wrap mongodb
- *
- * @param {String} name, method name
- * @param {Function} method
- * @return {Function} wrap function.
- * @api private
- */
-var bindSkin = function (name, method) {
- return SkinDb.prototype[name] = function () {
- var args = __slice.call(arguments);
- if (this.state === STATE_OPEN) {
- return method.apply(this.db, args);
- }
- return this.open(function (err, db) {
- if (err) {
- utils.error(err, args, 'SkinDb.' + name);
- } else {
- return method.apply(db, args);
- }
- });
- };
-};
-
var IGNORE_NAMES = [
'bind', 'open', 'close', 'collection', 'admin',
'state'
@@ -231,8 +220,5 @@ for (var name in Db.prototype) {
continue;
}
var method = Db.prototype[name];
- if (typeof method !== 'function') {
- continue;
- }
- bindSkin(name, method);
+ utils.bindSkin('SkinDb', SkinDb, 'db', name, method);
}
View
15 lib/mongoskin/gridfs.js
@@ -1,3 +1,16 @@
+/*!
+ * mongoskin - gridfs.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
var GridStore = require('mongodb').GridStore;
/**
@@ -5,7 +18,7 @@ var GridStore = require('mongodb').GridStore;
*/
var SkinGridStore = exports.SkinGridStore = function(skinDb) {
this.skinDb = skinDb;
-}
+};
/**
* @param id
View
60 lib/mongoskin/index.js
@@ -1,16 +1,30 @@
-var url = require('url'),
- Router = require('./router').Router,
- mongo = require('mongodb'),
- SkinServer = require('./server').SkinServer,
- SkinDb =require('./db').SkinDb,
- Db = mongo.Db,
- Server = mongo.Server,
- ReplSetServers = mongo.ReplSetServers,
- BSONNative = mongo.BSONNative,
- DEFAULT_PORT = 27017;
+/*!
+ * mongoskin - index.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var url = require('url');
+var Router = require('./router').Router;
+var mongo = require('mongodb');
+var SkinServer = require('./server').SkinServer;
+var SkinDb =require('./db').SkinDb;
+var Db = mongo.Db;
+var Server = mongo.Server;
+var ReplSetServers = mongo.ReplSetServers;
+var BSONNative = mongo.BSONNative;
+var constant = require('./constant');
+var DEFAULT_PORT = constant.DEFAULT_PORT;
function toBool(value) {
- return value !== undefined && value != 'false' && value != 'no' && value != 'off';
+ return value !== undefined && value !== 'false' && value !== 'no' && value !== 'off';
}
/**
@@ -19,24 +33,24 @@ function toBool(value) {
* [*://]username:password@host[:port]/database?options
*
*/
-var parseUrl = function(serverUrl) {
- var serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl,
- uri = url.parse(serverUrl, true),
- config = {},
- serverOptions = uri.query,
- reconnect = serverOptions['auto_reconnect'];
+var parseUrl = function (serverUrl) {
+ serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl;
+ var uri = url.parse(serverUrl, true);
+ var config = {};
+ var serverOptions = uri.query;
+ var reconnect = serverOptions.auto_reconnect;
config.host = uri.hostname;
- config.port = Number(uri.port) || DEFAULT_PORT;
- if(uri.pathname) {
+ config.port = parseInt(uri.port, 10) || DEFAULT_PORT;
+ if (uri.pathname) {
config.database = uri.pathname.replace(/\//g, '');
}
config.options = {};
- config.options['auto_reconnect'] = toBool(serverOptions['auto_reconnect']);
- config.options['poolSize'] = parseInt(serverOptions['poolSize'] || 1);
+ config.options.auto_reconnect = toBool(serverOptions.auto_reconnect);
+ config.options.poolSize = parseInt(serverOptions.poolSize || 1, 10);
if (uri && uri.auth) {
- var auth = uri.auth,
- separator = auth.indexOf(':');
+ var auth = uri.auth;
+ var separator = auth.indexOf(':');
config.username = auth.substr(0, separator);
config.password = auth.substr(separator + 1);
}
View
31 lib/mongoskin/router.js
@@ -1,24 +1,37 @@
-var Router = exports.Router = function(select) {
+/*!
+ * mongoskin - router.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var Router = exports.Router = function (select) {
this._select = select;
this._collections = {};
-}
+};
-Router.prototype.bind = function() {
- var args = Array.prototype.slice.call(arguments),
- name = args[0];
+Router.prototype.bind = function () {
+ var args = Array.prototype.slice.call(arguments);
+ var name = args[0];
var database = this._select(name);
var coll = database.bind.apply(database, args);
this._collections[name] = coll;
Object.defineProperty(this, name, {
- value: coll,
- writable: false,
- enumerable: true
+ value: coll,
+ writable: false,
+ enumerable: true
});
};
-Router.prototype.collection = function(name) {
+Router.prototype.collection = function (name) {
return this._collections[name] || (this._collections[name] = this._select(name).collection(name));
};
View
39 lib/mongoskin/server.js
@@ -1,17 +1,29 @@
-var __slice = Array.prototype.slice,
- mongodb = require('mongodb'),
- Db = mongodb.Db,
- Server = mongodb.Server,
- SkinDb = require('./db').SkinDb;
+/*!
+ * mongoskin - server.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var mongodb = require('mongodb');
+var Db = mongodb.Db;
+var Server = mongodb.Server;
+var SkinDb = require('./db').SkinDb;
/**
* Construct SkinServer with native Server
*
* @param server
*/
-var SkinServer = exports.SkinServer = function(server) {
+var SkinServer = exports.SkinServer = function (server) {
this.server = server;
- this._cache_ = [];
+ this._cache_ = {};
};
/**
@@ -20,18 +32,17 @@ var SkinServer = exports.SkinServer = function(server) {
* @param name database name
*
* @return SkinDb
- *
- * TODO add options
*/
-SkinServer.prototype.db = function(name, options) {
- var key = (username || '') + '@' + name;
+SkinServer.prototype.db = function (name, options) {
+ options = options || {};
+ var username = options.username || '';
+ var key = username + '@' + name;
var skinDb = this._cache_[key];
if (!skinDb || skinDb.fail) {
- var username = options.username,
- password = options.password;
+ var password = options.password;
delete options.username;
delete options.password;
- if(options.native_parser === undefined) {
+ if (!options.native_parser) {
options.native_parser = !! mongodb.BSONNative;
}
var db = new Db(name, this.server, options);
View
55 lib/mongoskin/utils.js
@@ -1,8 +1,55 @@
-exports.error = function(err, args, name) {
+/*!
+ * mongoskin - utils.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var __slice = Array.prototype.slice;
+var constant = require('./constant');
+var STATE_OPEN = constant.STATE_OPEN;
+
+exports.error = function (err, args, name) {
var cb = args.pop();
- if(cb && typeof cb === 'function') {
- cb(err)
+ if (cb && typeof cb === 'function') {
+ cb(err);
} else {
console.error("Error occured with no callback to handle it while calling " + name, err);
}
-}
+};
+
+/**
+ * Skin method binding.
+ *
+ * @param {String} objName
+ * @param {Function} obj
+ * @param {String} nativeObjName
+ * @param {String} methodName
+ * @param {Function} method
+ * @return {Function}
+ */
+exports.bindSkin = function (objName, obj, nativeObjName, methodName, method) {
+ if (typeof method !== 'function') {
+ return;
+ }
+ return obj.prototype[methodName] = function () {
+ var args = __slice.call(arguments);
+ if (this.state === STATE_OPEN) {
+ return method.apply(this[nativeObjName], args);
+ }
+ return this.open(function (err, nativeObj) {
+ if (err) {
+ exports.error(err, args, objName + '.' + methodName);
+ } else {
+ return method.apply(nativeObj, args);
+ }
+ });
+ };
+};
View
70 test/admin.js
@@ -0,0 +1,70 @@
+/*!
+ * mongoskin - test/admin.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+var SkinAdmin = require('../').SkinAdmin;
+
+describe('admin.js', function () {
+
+ var skinDb;
+ beforeEach(function () {
+ skinDb = {
+ open: function (callback) {
+ var that = this;
+ process.nextTick(function () {
+ callback(null, that.db);
+ });
+ },
+ db: {
+ name: 'mock db'
+ }
+ };
+ });
+
+ describe('open()', function () {
+
+ it('should return admin', function (done) {
+ var skinAdmin = new SkinAdmin(skinDb);
+ skinAdmin.open(function (err, admin) {
+ should.not.exist(err);
+ should.exist(admin);
+ should.exist(skinAdmin.admin);
+ skinAdmin.open(function (err, admin) {
+ should.not.exist(err);
+ should.exist(admin);
+ should.exist(skinAdmin.admin);
+ done();
+ });
+ });
+ });
+
+ it('should return mock open() error', function (done) {
+ skinDb.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock open() error'));
+ });
+ };
+ var skinAdmin = new SkinAdmin(skinDb);
+ skinAdmin.open(function (err, admin) {
+ should.exist(err);
+ err.should.have.property('message', 'mock open() error');
+ should.not.exist(admin);
+ should.not.exist(skinAdmin.admin);
+ done();
+ });
+ });
+
+ });
+
+});
View
72 test/collection.js
@@ -1,5 +1,7 @@
/*!
* mongoskin - test/collection.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
@@ -11,6 +13,8 @@
*/
var mongoskin = require('../');
+var SkinCollection = mongoskin.SkinCollection;
+var constant = require('../lib/mongoskin/constant');
var should = require('should');
describe('collection.js', function () {
@@ -37,6 +41,74 @@ describe('collection.js', function () {
});
});
+ describe('open()', function () {
+ var skinDb;
+ beforeEach(function () {
+ skinDb = {
+ open: function (callback) {
+ var that = this;
+ process.nextTick(function () {
+ callback(null, that.db);
+ });
+ },
+ db: {
+ name: 'mock db',
+ collection: function (name, callback) {
+ process.nextTick(function () {
+ callback(null, {
+ name: 'mock collection'
+ });
+ });
+ }
+ }
+ };
+ });
+
+ it('should return a collection', function (done) {
+ var collection = new SkinCollection(skinDb, 'foo');
+ collection.hint = 123;
+ collection.open(function (err, coll) {
+ should.not.exist(err);
+ coll.should.have.property('name', 'mock collection');
+ collection.state.should.equal(constant.STATE_OPEN);
+ done();
+ });
+ });
+
+ it('should return mock skinDb.open() error', function (done) {
+ skinDb.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock skinDb.open() error'));
+ });
+ };
+ var collection = new SkinCollection(skinDb, 'foo');
+ collection.open(function (err, coll) {
+ should.exist(err);
+ err.should.have.property('message', 'mock skinDb.open() error');
+ should.not.exist(coll);
+ collection.state.should.equal(constant.STATE_CLOSE);
+ done();
+ });
+ });
+
+ it('should return mock db.collection() error', function (done) {
+ skinDb.db.collection = function (name, callback) {
+ process.nextTick(function () {
+ callback(new Error('mock db.collection() error'));
+ });
+ };
+ var collection = new SkinCollection(skinDb, 'foo');
+ collection.open(function (err, coll) {
+ should.exist(err);
+ should.not.exist(coll);
+ err.should.have.property('message', 'mock db.collection() error');
+ collection.state.should.equal(constant.STATE_CLOSE);
+ done();
+ });
+ });
+
+ });
+
describe('id()', function () {
it('should convert string id to ObjectID success', function () {
var id = '4ec4b2b9f44a927223000001';
View
98 test/cursor.js
@@ -0,0 +1,98 @@
+/*!
+ * mongoskin - test/cursor.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+var constant = require('../lib/mongoskin/constant');
+var SkinCursor = require('../').SkinCursor;
+
+describe('cursor.js', function () {
+
+ describe('new SkinCursor()', function () {
+ it('should state is open when cursor exists', function () {
+ var cursor = new SkinCursor({}, {});
+ cursor.should.have.property('state', constant.STATE_OPEN);
+ });
+ it('should state is close when cursor not exists', function () {
+ var cursor = new SkinCursor(null, {});
+ cursor.should.have.property('state', constant.STATE_CLOSE);
+ });
+ });
+
+ describe('open()', function () {
+ var collection;
+ beforeEach(function () {
+ collection = {
+ open: function (callback) {
+ var that = this;
+ process.nextTick(function () {
+ callback(null, that);
+ });
+ },
+ find: function () {
+ var callback = arguments[arguments.length - 1];
+ process.nextTick(function () {
+ callback(null, {name: 'mock cursor'});
+ });
+ }
+ };
+ });
+
+ it('should success when state is close', function (done) {
+ var cursor = new SkinCursor(null, collection);
+ cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+ done();
+ });
+ });
+
+ it('should success when state is openning', function (done) {
+ var cursor = new SkinCursor(null, collection);
+ cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+ });
+ cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+ done();
+ });
+ });
+
+ it('should success when state is open', function (done) {
+ var cursor = new SkinCursor({name: 'mock cursor 2'}, collection);
+ cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor 2');
+ done();
+ });
+ });
+
+ it('should return mock error', function (done) {
+ collection.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock collection.open() error'));
+ });
+ };
+ var cursor = new SkinCursor(null, collection);
+ cursor.open(function (err, mockCursor) {
+ should.exist(err);
+ err.should.have.property('message', 'mock collection.open() error');
+ should.not.exist(mockCursor);
+ done();
+ });
+ });
+ });
+
+});
View
2  test/db.js
@@ -1,5 +1,7 @@
/*!
* mongoskin - test/db.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
View
21 test/gridfs.js
@@ -0,0 +1,21 @@
+/*!
+ * mongoskin - test/gridfs.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+
+describe('gridfs.js', function () {
+
+
+
+});
View
21 test/router.js
@@ -0,0 +1,21 @@
+/*!
+ * mongoskin - test/router.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+
+describe('router.js', function () {
+
+
+
+});
View
21 test/server.js
@@ -0,0 +1,21 @@
+/*!
+ * mongoskin - test/server.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+
+describe('server.js', function () {
+
+
+
+});
View
21 test/utils.js
@@ -0,0 +1,21 @@
+/*!
+ * mongoskin - test/utils.js
+ *
+ * Copyright(c) 2011 - 2012 kissjs.org
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+
+describe('utils.js', function () {
+
+
+
+});
View
174 test_results.md
@@ -1,5 +1,8 @@
# TOC
+ - [admin.js](#adminjs)
+ - [open()](#adminjs-open)
- [collection.js](#collectionjs)
+ - [open()](#collectionjs-open)
- [id()](#collectionjs-id)
- [find(), findItems(), findEach()](#collectionjs-find-finditems-findeach)
- [mock find() error](#collectionjs-find-finditems-findeach-mock-find-error)
@@ -7,16 +10,113 @@
- [findById()](#collectionjs-findbyid-updatebyid-removebyid-findbyid)
- [updateById()](#collectionjs-findbyid-updatebyid-removebyid-updatebyid)
- [removeById()](#collectionjs-findbyid-updatebyid-removebyid-removebyid)
+ - [cursor.js](#cursorjs)
+ - [new SkinCursor()](#cursorjs-new-skincursor)
+ - [open()](#cursorjs-open)
- [db.js](#dbjs)
- [bind()](#dbjs-bind)
- [gridfs()](#dbjs-gridfs)
- [open()](#dbjs-open)
- [close()](#dbjs-close)
- [ensureIndex()](#dbjs-ensureindex)
+ - [gridfs.js](#gridfsjs)
+ - [router.js](#routerjs)
+ - [server.js](#serverjs)
+ - [utils.js](#utilsjs)
<a name="" />
+<a name="adminjs" />
+# admin.js
+<a name="adminjs-open" />
+## open()
+should return admin.
+
+```js
+var skinAdmin = new SkinAdmin(skinDb);
+skinAdmin.open(function (err, admin) {
+ should.not.exist(err);
+ should.exist(admin);
+ should.exist(skinAdmin.admin);
+ skinAdmin.open(function (err, admin) {
+ should.not.exist(err);
+ should.exist(admin);
+ should.exist(skinAdmin.admin);
+ done();
+ });
+});
+```
+
+should return mock open() error.
+
+```js
+skinDb.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock open() error'));
+ });
+};
+var skinAdmin = new SkinAdmin(skinDb);
+skinAdmin.open(function (err, admin) {
+ should.exist(err);
+ err.should.have.property('message', 'mock open() error');
+ should.not.exist(admin);
+ should.not.exist(skinAdmin.admin);
+ done();
+});
+```
+
<a name="collectionjs" />
# collection.js
+<a name="collectionjs-open" />
+## open()
+should return a collection.
+
+```js
+var collection = new SkinCollection(skinDb, 'foo');
+collection.hint = 123;
+collection.open(function (err, coll) {
+ should.not.exist(err);
+ coll.should.have.property('name', 'mock collection');
+ collection.state.should.equal(constant.STATE_OPEN);
+ done();
+});
+```
+
+should return mock skinDb.open() error.
+
+```js
+skinDb.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock skinDb.open() error'));
+ });
+};
+var collection = new SkinCollection(skinDb, 'foo');
+collection.open(function (err, coll) {
+ should.exist(err);
+ err.should.have.property('message', 'mock skinDb.open() error');
+ should.not.exist(coll);
+ collection.state.should.equal(constant.STATE_CLOSE);
+ done();
+});
+```
+
+should return mock db.collection() error.
+
+```js
+skinDb.db.collection = function (name, callback) {
+ process.nextTick(function () {
+ callback(new Error('mock db.collection() error'));
+ });
+};
+var collection = new SkinCollection(skinDb, 'foo');
+collection.open(function (err, coll) {
+ should.exist(err);
+ should.not.exist(coll);
+ err.should.have.property('message', 'mock db.collection() error');
+ collection.state.should.equal(constant.STATE_CLOSE);
+ done();
+});
+```
+
<a name="collectionjs-id" />
## id()
should convert string id to ObjectID success.
@@ -236,6 +336,80 @@ db.article.findById(id, function (err, article) {
});
```
+<a name="cursorjs" />
+# cursor.js
+<a name="cursorjs-new-skincursor" />
+## new SkinCursor()
+should state is open when cursor exists.
+
+```js
+var cursor = new SkinCursor({}, {});
+cursor.should.have.property('state', constant.STATE_OPEN);
+```
+
+should state is close when cursor not exists.
+
+```js
+var cursor = new SkinCursor(null, {});
+cursor.should.have.property('state', constant.STATE_CLOSE);
+```
+
+<a name="cursorjs-open" />
+## open()
+should success when state is close.
+
+```js
+var cursor = new SkinCursor(null, collection);
+cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+ done();
+});
+```
+
+should success when state is openning.
+
+```js
+var cursor = new SkinCursor(null, collection);
+cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+});
+cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor');
+ done();
+});
+```
+
+should success when state is open.
+
+```js
+var cursor = new SkinCursor({name: 'mock cursor 2'}, collection);
+cursor.open(function (err, mockCursor) {
+ should.not.exist(err);
+ mockCursor.should.have.property('name', 'mock cursor 2');
+ done();
+});
+```
+
+should return mock error.
+
+```js
+collection.open = function (callback) {
+ process.nextTick(function () {
+ callback(new Error('mock collection.open() error'));
+ });
+};
+var cursor = new SkinCursor(null, collection);
+cursor.open(function (err, mockCursor) {
+ should.exist(err);
+ err.should.have.property('message', 'mock collection.open() error');
+ should.not.exist(mockCursor);
+ done();
+});
+```
+
<a name="dbjs" />
# db.js
<a name="dbjs-bind" />
Please sign in to comment.
Something went wrong with that request. Please try again.