Permalink
Browse files

fixed #56 error: Argument passed in must be a single String of 12 byt…

…es or a string of 24 hex characters; add more test case for conllection.js; add test coverage;
  • Loading branch information...
1 parent c919509 commit 2a4779b3d63fb2503aecc1784fa7f8cf6860c686 @fengmk2 fengmk2 committed Jun 23, 2012
Showing with 699 additions and 133 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +20 −0 Makefile
  4. +38 −5 Readme.md
  5. +2 −2 examples/config.js
  6. +45 −0 examples/replSetBenchmark.js
  7. +1 −1 index.js
  8. +80 −63 lib/mongoskin/collection.js
  9. +6 −3 lib/mongoskin/db.js
  10. +3 −4 package.json
  11. +281 −0 test/collection.js
  12. +0 −55 test/collection.test.js
  13. +2 −0 test/mocha.opts
  14. +216 −0 test_results.md
View
@@ -1 +1,3 @@
node_modules
+coverage.html
+lib-cov
View
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
View
@@ -0,0 +1,20 @@
+TESTS = $(shell find test -type f -name "*.js")
+TESTTIMEOUT = 5000
+REPORTER = spec
+PROJECT_DIR = $(shell pwd)
+
+test:
+ @npm install
+ @NODE_ENV=test ./node_modules/mocha/bin/mocha \
+ --reporter $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS)
+
+lib-cov:
+ @rm -rf ./$@
+ @jscoverage --encoding=utf-8 ./lib ./$@
+
+test-cov: lib-cov
+ @MONGOSKIN_COV=1 $(MAKE) test REPORTER=progress
+ @MONGOSKIN_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+ @$(MAKE) test REPORTER=markdown > test_results.md
+
+.PHONY: test test-cov lib-cov
View
@@ -1,11 +1,14 @@
-## This project is a wrapper of node-mongodb-native
+# mongoskin
-* node-mongodb-native document http://christkv.github.com/node-mongodb-native/
+[![Build Status](https://secure.travis-ci.org/kissjs/node-mongoskin.png)](http://travis-ci.org/kissjs/node-mongoskin)
-## How to validate input?
+This project is a wrapper of [node-mongodb-native](http://christkv.github.com/node-mongodb-native/).
+All api is same to mongodb-native, please see the [document](http://christkv.github.com/node-mongodb-native/) first.
-I wrote a middleware to validate post data, [node-iform](https://github.com/guileen/node-iform)
-base on [node-validator](https://github.com/chriso/node-validator)
+## 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)
<a name='index'>
@@ -569,6 +572,36 @@ All these methods will return the SkinCursor itself.
[Back to index](#index)
+## How to validate input?
+
+I wrote a middleware to validate post data, [node-iform](https://github.com/guileen/node-iform)
+base on [node-validator](https://github.com/chriso/node-validator)
+
+## Authors
+
+Below is the output from `git-summary`.
+
+ project: node-mongoskin
+ commits: 108
+ active : 52 days
+ files : 29
+ authors:
+ 49 Lin Gui 45.4%
+ 34 guilin 桂林 31.5%
+ 5 guilin 4.6%
+ 5 fengmk2 4.6%
+ 2 François de Metz 1.9%
+ 2 Paul Gebheim 1.9%
+ 2 Gui Lin 1.9%
+ 1 humanchimp 0.9%
+ 1 Aneil Mallavarapu 0.9%
+ 1 wmertens 0.9%
+ 1 Harvey McQueen 0.9%
+ 1 Joe Faber 0.9%
+ 1 Matt Perpick 0.9%
+ 1 Quang Van 0.9%
+ 1 Rakshit Menpara 0.9%
+ 1 Wout Mertens 0.9%
## License
View
@@ -1,5 +1,5 @@
var mongoskin = require('../lib/mongoskin/');
-require('myconsole').replace();
-
exports.db = mongoskin.db('localhost/test');
+
+mongoskin.db('localhost', { database: 'test' });
@@ -0,0 +1,45 @@
+
+var mongo = require('../');
+
+var conf = {
+ hosts: [
+ '127.0.0.1:27110/?auto_reconnect',
+ '127.0.0.1:27111/?auto_reconnect'
+ ],
+ dataDB: 'test'
+};
+
+var db = exports.db = mongo.db(conf.hosts, {
+ database: conf.dataDB
+});
+
+var noop = function() {};
+
+db.bind('user');
+// db.user.ensureIndex({ name: 1 }, { unique: true }, noop);
+// db.user.ensureIndex({ enable: 1 }, noop);
+// db.user.ensureIndex({ created_at: 1, enable: 1 }, noop);
+
+var counter = 0;
+setInterval(function () {
+ db.user.findItems({ name: 'name_' + counter }, function (err, items) {
+ if (err) {
+ console.error('findItems user error', err);
+ }
+ if (items) {
+ console.log('total: %d users', items.length);
+ }
+ });
+ db.user.insert({
+ name: 'name_' + counter,
+ createtime: new Date()
+ }, function(err, user) {
+ if (err) {
+ console.error('insert user error', err);
+ }
+ if (user && user[0]) {
+ console.log('new: %d %s', counter, user[0]._id);
+ }
+ });
+ counter++;
+}, 10);
View
@@ -1 +1 @@
-module.exports = require('./lib/mongoskin');
+module.exports = process.env.MONGOSKIN_COV ? require('./lib-cov/mongoskin') : require('./lib/mongoskin');
@@ -49,13 +49,15 @@ var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
this.state = STATE_CLOSE;
this.internalHint;
var that = this;
- this.__defineGetter__('hint', function() { return this.internalHint; });
- this.__defineSetter__('hint', function(value) {
- this.internalHint = value;
- this.open(function(err, collection) {
- collection.hint = value;
- that.internalHint = collection.hint;
- });
+ this.__defineGetter__('hint', function () {
+ return this.internalHint;
+ });
+ this.__defineSetter__('hint', function (value) {
+ this.internalHint = value;
+ this.open(function(err, collection) {
+ collection.hint = value;
+ that.internalHint = collection.hint;
+ });
});
this.emitter = new events.EventEmitter();
@@ -96,28 +98,28 @@ SkinCollection.prototype.open = function(fn) {
return fn(null, this.collection);
case STATE_OPENNING:
return this.emitter.once('open', fn);
- case STATE_CLOSE:
+ // case STATE_CLOSE:
default:
var that = this;
this.emitter.once('open', fn);
this.state = STATE_OPENNING;
this.skinDb.open(function(err, db) {
- if (err) {
+ if (err) {
+ that.state = STATE_CLOSE;
+ return that.emitter.emit('open', err, null);
+ }
+ that.skinDb.db.collection(that.collectionName, function (err, collection) {
+ if (collection) {
+ that.state = STATE_OPEN;
+ that.collection = collection;
+ if (that.hint) {
+ that.collection.hint = that.hit;
+ }
+ } else {
that.state = STATE_CLOSE;
- return that.emitter.emit('open', err, null);
}
- that.skinDb.db.collection(that.collectionName, function(err, collection) {
- if (collection) {
- that.state = STATE_OPEN;
- that.collection = collection;
- if (that.hint) {
- that.collection.hint = that.hit;
- }
- }else {
- that.state = STATE_CLOSE;
- }
- that.emitter.emit('open', err, collection);
- });
+ that.emitter.emit('open', err, collection);
+ });
});
}
};
@@ -134,94 +136,109 @@ SkinCollection.prototype.drop = function(fn) {
/**
* same args as find, but use Array as callback result but not use Cursor
*
- * findItems(args, function(err, items){});
+ * findItems(args, function (err, items) {});
*
* same as
*
- * find(args, function(err, cursor){cursor.toArray(err, items){}});
+ * find(args).toArray(function (err, items) {});
+ *
+ * or using `mongodb.collection.find()`
+ *
+ * find(args, function (err, cursor) {
+ * cursor.toArray(function (err, items) {
+ * });
+ * });
*
*/
-SkinCollection.prototype.findItems = function() {
- var args = __slice.call(arguments),
- fn = args[args.length - 1];
-
- args[args.length - 1] = function(err, cursor) {
+SkinCollection.prototype.findItems = function (query, options, callback) {
+ var args = __slice.call(arguments);
+ var fn = args[args.length - 1];
+ args[args.length - 1] = function (err, cursor) {
if (err) {
- fn(err);
- } else {
- cursor.toArray(fn);
+ return fn(err);
}
- }
-
- this._find.apply(this, args);
+ cursor.toArray(fn);
+ };
+ this.find.apply(this, args);
};
/**
- * find and cursor.each
+ * find and cursor.each(fn).
+ *
+ * @param {Object} [query]
+ * @param {Object} [options]
+ * @param {Function(err, item)} eachCallback
*/
-SkinCollection.prototype.findEach = function() {
- var args = __slice.call(arguments),
- fn = args[args.length - 1];
-
- args[args.length - 1] = function(err, cursor) {
+SkinCollection.prototype.findEach = function (query, options, eachCallback) {
+ var args = __slice.call(arguments);
+ var fn = args[args.length - 1];
+ args[args.length - 1] = function (err, cursor) {
if (err) {
- fn(err);
- } else {
- cursor.each(fn);
+ return fn(err);
}
- }
-
- this._find.apply(this, args);
+ cursor.each(fn);
+ };
+ this.find.apply(this, args);
};
/**
- * @deprecated use SkinDb.id instead
+ * @deprecated use `SkinDb.toId` instead
*/
SkinCollection.prototype.id = function(hex) {
return this.skinDb.toId(hex);
};
/**
- * use hex id as first argument, support ObjectID and String id
+ * Find one object by _id.
*
- * @param {String/ObjectID} id
- * @param {Function} callback
- * @return {Object} cursor
+ * @param {String|ObjectID|Number} id, doc primary key `_id`
+ * @param {Function(err, doc)} callback
* @api public
*/
-SkinCollection.prototype.findById = function() {
+SkinCollection.prototype.findById = function (id, callback) {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.findOne.apply(this, args);
};
/**
- * use hex id as first argument
+ * Update doc by _id.
+ * @param {String|ObjectID|Number} id, doc primary key `_id`
+ * @param {Function(err)} callback
+ * @api public
*/
-SkinCollection.prototype.updateById = function() {
+SkinCollection.prototype.updateById = function (id, callback) {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.update.apply(this, args);
};
/**
- * use hex id as first argument
+ * Remove doc by _id.
+ * @param {String|ObjectID|Number} id, doc primary key `_id`
+ * @param {Function(err)} callback
+ * @api public
*/
-SkinCollection.prototype.removeById = function() {
+SkinCollection.prototype.removeById = function (id, callback) {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.remove.apply(this, args);
};
/**
- * if last argument is not a function, then returns a SkinCursor
+ * Creates a cursor for a query that can be used to iterate over results from MongoDB.
+ *
+ * @param {Object} query
+ * @param {Object} options
+ * @param {Function(err, docs)} callback
+ * @return {SkinCursor} if last argument is not a function, then returns a SkinCursor
+ * @api public
*/
-SkinCollection.prototype.find = function() {
+SkinCollection.prototype.find = function (query, options, callback) {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
- if (args.length > 0 && typeof(args[args.length - 1]) === 'function') {
+ if (args.length > 0 && typeof args[args.length - 1] === 'function') {
this._find.apply(this, args);
- }else {
+ } else {
return new SkinCursor(null, this, args);
}
-};
-
+};
View
@@ -30,8 +30,11 @@ var SkinDb = exports.SkinDb = function(db, username, password) {
this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || db.bson_serializer.ObjectID /* <= 0.9.7 */;
};
-SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function(hex) {
- if(hex instanceof this.ObjectID) {
+SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) {
+ if (hex instanceof this.ObjectID) {
+ return hex;
+ }
+ if (!hex || hex.length !== 24) {
return hex;
}
return this.ObjectID.createFromHexString(hex);
@@ -118,7 +121,7 @@ SkinDb.prototype.collection = function(name) {
*/
SkinDb.prototype.gridfs = function() {
return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
-}
+};
/**
* bind additional method to SkinCollection
Oops, something went wrong.

0 comments on commit 2a4779b

Please sign in to comment.