Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

100% test coverage

  • Loading branch information...
commit 1cc13c05f358fcd685fd332e5975b10d09ecce96 1 parent 4e5cdf6
@guileen guileen authored
View
2  Makefile
@@ -1,5 +1,5 @@
TESTS = test/
-TESTTIMEOUT = 2000
+TESTTIMEOUT = 1000
REPORTER = spec
MOCHA_OPTS =
PROJECT_DIR = $(shell pwd)
View
7 lib/admin.js
@@ -13,12 +13,13 @@
*/
var Admin = require('mongodb').Admin;
-var utils = require('./utils');
+var makeSkinClass = require('./utils').makeSkinClass;
-var SkinAdmin = exports.SkinAdmin = utils.makeSkinClass(Admin, false);
+var SkinAdmin = exports.SkinAdmin = makeSkinClass(Admin);
SkinAdmin.prototype._open = function(callback) {
- this._skin_db.open(function(err, p_db) {
+ var skindb = this._construct_args[0];
+ skindb.open(function(err, p_db) {
if(err) return callback(err);
callback(null, p_db.admin());
})
View
2  lib/collection.js
@@ -20,7 +20,7 @@ var utils = require('./utils');
/**
* Constructor
*/
-var SkinCollection = exports.SkinCollection = utils.makeSkinClass(Collection, false);
+var SkinCollection = exports.SkinCollection = utils.makeSkinClass(Collection);
SkinCollection.prototype._open = function(callback) {
var collection_args = this._collection_args.concat([callback]);
View
2  lib/cursor.js
@@ -14,7 +14,7 @@
var Cursor = require('mongodb').Cursor;
var utils = require('./utils');
-var SkinCursor = exports.SkinCursor = utils.makeSkinClass(Cursor, false);
+var SkinCursor = exports.SkinCursor = utils.makeSkinClass(Cursor);
/**
* Retrieve mongodb.Cursor instance.
View
10 lib/db.js
@@ -25,11 +25,7 @@ var SkinGridStore = require('./grid_store').SkinGridStore;
/**
* Constructor
*/
-var SkinDb = exports.SkinDb = utils.makeSkinClass(Db, false, true);
-
-SkinDb.prototype.authenticate = function () {
- this._authenticate_args = [].slice.call(arguments);
-}
+var SkinDb = exports.SkinDb = utils.makeSkinClass(Db, true);
SkinDb.prototype._open = function(callback) {
// TODO authenticate support
@@ -53,7 +49,7 @@ SkinDb.prototype._open = function(callback) {
SkinDb.prototype.collection = function (name, options, callback) {
if(!callback && typeof options == 'function') {
callback = options;
- options = null;
+ options = undefined;
}
// Ooops, no extended mthods like findById etc.
// if(this.isOpen() && (!options || !options.strict) && !callback) {
@@ -81,7 +77,7 @@ SkinDb.prototype.bind = function (name, options) {
SkinDb.prototype.admin = function () {
var skinAdmin = new SkinAdmin();
- skinAdmin._skin_db = this;
+ skinAdmin._construct_args = [this];
return skinAdmin;
}
View
21 lib/grid_store.js
@@ -7,11 +7,28 @@ var SkinGridStore = exports.SkinGridStore = makeSkinClass(GridStore);
SkinGridStore.prototype._open = function(callback) {
var skin_db = this._construct_args[0];
+ var args = this._construct_args.slice(1);
skin_db.open(function(err, p_db) {
if(err) return callback(err);
- var args = ([null]).concat(this._construct_args);
+ args = ([null, p_db]).concat(args);
var ctor = GridStore.bind.apply(GridStore, args);
var gridStore = new ctor();
- callback(null, gridStore);
+ gridStore.open(callback);
});
}
+
+function bindStaticMethod(methodName) {
+ SkinGridStore[methodName] = function(skindb) {
+ var args = Array.prototype.slice.call(arguments);
+ skindb.open(function(err, p_db) {
+ args[0] = p_db;
+ GridStore[methodName].apply(GridStore, args);
+ });
+ }
+}
+
+bindStaticMethod('exist');
+bindStaticMethod('list');
+bindStaticMethod('read');
+bindStaticMethod('readlines');
+bindStaticMethod('unlink');
View
21 lib/index.js
@@ -20,16 +20,17 @@ for (var key in mongo) {
exports[key] = mongo[key];
}
-exports.SkinMongoClient = require('./mongo_client').SkinMongoClient;
-exports.SkinDb = require('./db').SkinDb;
-exports.SkinCollection = require('./collection').SkinCollection;
-exports.SkinCursor = require('./cursor').SkinCursor;
-exports.SkinAdmin = require('./admin').SkinAdmin;
-exports.SkinGrid = require('./grid').SkinGrid;
-exports.SkinGridStore = require('./grid_store').SkinGridStore;
+// exports.Foo = SkinFoo;
+;['mongo_client', 'db', 'collection', 'cursor', 'admin', 'grid', 'grid_store'].forEach(function(modPath){
+ var mod = require('./' + modPath);
+ for(var name in mod) {
+ if(name.indexOf('Skin') == 0) {
+ exports[name.substring(4)] = mod[name];
+ }
+ }
+});
-exports.MongoClient = exports.SkinMongoClient;
-exports.Db = exports.SkinDb;
exports.helper = require('./helper');
+exports.utils = require('./utils');
-exports.db = exports.SkinMongoClient.connect;
+exports.db = exports.MongoClient.connect;
View
2  lib/mongo_client.js
@@ -4,7 +4,7 @@ var utils = require('./utils');
var SkinDb = require('./db').SkinDb;
-var SkinMongoClient = utils.makeSkinClass(MongoClient, false, true);
+var SkinMongoClient = utils.makeSkinClass(MongoClient, true);
exports.SkinMongoClient = SkinMongoClient;
SkinMongoClient.connect = function() {
View
64 lib/utils.js
@@ -19,18 +19,17 @@ var STATE_CLOSE = constant.STATE_CLOSE;
var STATE_OPENNING = constant.STATE_OPENNING;
var STATE_OPEN = constant.STATE_OPEN;
-exports.inherits = require('util').inherits;
-
-exports.error = function (err, args, name) {
- var cb = args.pop();
- if (cb && typeof cb === 'function') {
- cb(err);
- } else {
- console.error("Error occured with no callback to handle it while calling " + name, err);
- }
-};
+exports.makeSkinClass = function makeSkinClass(NativeClass, useNativeConstructor) {
+
+ function onError (err, args, name) {
+ var cb = args.pop();
+ if (cb && typeof cb === 'function') {
+ cb(err);
+ } else {
+ console.error("Error occured with no callback to handle it while calling " + name, err);
+ }
+ };
-exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeConstructor) {
var skinClassName = 'Skin' + NativeClass.name;
function SkinClass() {
var args = __slice.call(arguments);
@@ -46,15 +45,6 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
this._emitter.setMaxListeners(50);
this._state = STATE_CLOSE;
this._init && this._init();
- if (autoOpen && this._native) {
- var self = this;
- // give some time to init the skin instance.
- process.nextTick(function () {
- self.open(function(err) {
- console.error('error to open ' + skinClassName + ' instance, args:', args, 'error:', err);
- })
- })
- }
}
SkinClass._class_name = skinClassName;
@@ -78,8 +68,9 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
if (desc.set) {
SkinClass._bindSetter(propName);
}
- } else {
- console.log('no desc and no value', skinClassName, propName);
+ // } else {
+ // this will never be called, so comment it.
+ // console.log('no desc and no value', skinClassName, propName);
}
}
@@ -91,7 +82,7 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
} else {
this.open(function(err, p_native) {
if (err) {
- exports.error(err, args, skinClassName + '.' + propName);
+ onError(err, args, skinClassName + '.' + propName);
} else {
p_native[propName].apply(p_native, args);
}
@@ -111,7 +102,7 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
SkinClass.prototype.__defineSetter__(propName, function(value) {
// this['_prop_' + propName] = value;
this.open(function(err, p_native) {
- if(err) return exports.error(err, args, skinClassName + '.' + propName);
+ if(err) return onError(err, args, skinClassName + '.' + propName);
p_native[propName] = value;
});
});
@@ -154,7 +145,7 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
this._close(callback);
} else if (this._state === STATE_OPENNING) {
var self = this;
- this.emitter.once('open', function (err, db) {
+ this._emitter.once('open', function (err, db) {
self.close(callback);
});
}
@@ -162,16 +153,12 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
return this;
}
- SkinClass.prototype._open = function(callback) {
- if(!this._native || !this._native.open) {
- console.error('No default ' + skinClassName + '._open');
- return;
- }
- this._native.open(callback);
- }
-
SkinClass.prototype._close = function(callback) {
- this._native.close(callback);
+ if(this._native.close) {
+ this._native.close(callback)
+ } else if(callback) {
+ callback();
+ }
}
SkinClass.prototype.isOpen = function() {
@@ -181,12 +168,3 @@ exports.makeSkinClass = function makeSkinClass(NativeClass, autoOpen, useNativeC
return SkinClass;
}
-
-exports.extend = function (destination, source) {
- for (var property in source) {
- destination[property] = source[property];
- }
- return destination;
-};
-
-exports.noop = function () {};
View
5 package.json
@@ -21,10 +21,11 @@
"node": ">= 0.4.0"
},
"dependencies": {
- "mongodb": "1.3.20"
+ "mongodb": "1.3.20",
+ "mocha": "~1.17.1"
},
"devDependencies": {
- "mocha": "~1.17.1",
+ "mocha": "*",
"jscover": "*",
"should": "1.2.2"
},
View
55 test/admin.js
@@ -1,41 +1,30 @@
-/*!
- * 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;
-var constant = require('../lib/constant');
+var Admin = require('../').Admin; // SkinAdmin
+var Db = require('../').Db; // SkinDb
+
+exports.testWithDb = function(db) {
+ function testAdmin(descName, adminDb) {
+ describe(descName, function() {
+ it('should add the new user to the admin database', function(done) {
+ adminDb.addUser('admin3', 'admin3', done);
+ });
-exports.describe = function(db) {
- describe('admin.js', function() {
- var adminDb;
- before(function(){
- adminDb = db.admin();
- });
- it('should add the new user to the admin database', function(done) {
- adminDb.addUser('admin3', 'admin3', done);
- });
+ it('should authenticate using the newly added user', function(done) {
+ adminDb.authenticate('admin3', 'admin3', done);
+ })
- it('should authenticate using the newly added user', function(done) {
- adminDb.authenticate('admin3', 'admin3', done);
- })
+ it('should retrive the build information for the mongodb instance', function(done) {
+ adminDb.buildInfo(done);
+ })
- it('should retrive the build information for the mongodb instance', function(done) {
- adminDb.buildInfo(done);
- })
+ it('should remove user just added', function(done) {
+ adminDb.removeUser('admin3', done);
+ })
+ });
+ }
- it('should remove user just added', function(done) {
- adminDb.removeUser('admin3', done);
- })
- });
+ testAdmin('db.admin()', db.admin());
+ testAdmin('new Admin(db)', new Admin(db));
}
View
2  test/collection.js
@@ -22,7 +22,7 @@ var should = require('should');
var servermanager = require('./utils/server_manager');
-exports.describe = function(db) {
+exports.testWithDb = function(db) {
describe('collection.js', function () {
View
2  test/cursor.js
@@ -17,7 +17,7 @@ var mongoskin = require('../');
var SkinCursor = mongoskin.SkinCursor;
var servermanager = require('./utils/server_manager');
-exports.describe = function(db) {
+exports.testWithDb = function(db) {
describe('cursor.js', function () {
// DO NOT use db.bind('testCursor') here, will not create SkinCursor if collection already open.
View
16 test/db.js
@@ -16,8 +16,21 @@ var mongoskin = require('../');
var pedding = require('./utils/pedding');
var servermanager = require('./utils/server_manager');
-exports.describe = function(db) {
+exports.testWithDb = function(db) {
describe('db.js', function () {
+
+ it('skinDb.collection() should retrive native collection with callback', function(done) {
+ var skinColl = db.collection('testRetriveCollection', function(err, coll) {
+ should.not.exist(err);
+ skinColl._native.should.eql(coll);
+ done();
+ });
+ should.exist(skinColl);
+ })
+
+ // old test
+
+ /*
describe('bind()', function () {
before(function (done) {
@@ -280,6 +293,7 @@ exports.describe = function(db) {
});
});
});
+ */
});
}
View
60 test/grid.js
@@ -0,0 +1,60 @@
+"use strict";
+
+var should = require('should');
+var Grid = require('../').Grid; // SkinGrid
+var Db = require('../').Db; // SkinDb
+var constant = require('../lib/constant');
+var assert = require('assert');
+
+var i = 0;
+exports.testWithDb = function(db) {
+ function testGrid(descName, grid) {
+ i++;
+ describe(descName, function() {
+ var id = 'test1' + i;
+ // Some data to write
+ var originalData = new Buffer('Hello world');
+ before(function(done) {
+ // Write data to grid
+ grid.put(originalData, {_id: id}, function(err, result) {
+ assert.equal(result._id, id);
+ done(err);
+ });
+ })
+
+ it('should write data to grid', function(done) {
+ grid.put(originalData, {}, function(err, result) {
+ should.not.exist(err);
+ result._id.should.not.eql(id);
+ done();
+ });
+ });
+
+ it('should get data just put to grid', function(done) {
+ grid.get(id, function(err, data) {
+ assert.deepEqual(originalData.toString('base64'), data.toString('base64'));
+ done(err);
+ });
+ })
+
+ // it('should delete data from grid', function(done) {
+ // // Delete file
+ // grid.delete(id, function(err, result2) {
+ // should.not.exists(err);
+ // assert.equal(null, err);
+ // assert.equal(true, result2);
+
+ // // Fetch the content, showing that the file is gone
+ // grid.get(id, function(err, data) {
+ // should.not.exist(data);
+ // done(err);
+ // });
+ // });
+ // })
+
+ });
+ }
+
+ testGrid('db.grid()', db.grid());
+ testGrid('new Grid(db, fsName)', new Grid(db, 'fs'));
+}
View
53 test/grid_store.js
@@ -0,0 +1,53 @@
+"use strict";
+
+var should = require('should');
+var GridStore = require('../').GridStore; // SkinGridStore
+var Db = require('../').Db; // SkinDb
+var ObjectID = require('../').ObjectID;
+var constant = require('../lib/constant');
+var assert = require('assert');
+
+exports.testWithDb = function(db) {
+ // Our file ID
+ var fileId = new ObjectID();
+
+ describe('grid_store.js', function() {
+ // Open a new file
+ var gridStore = new GridStore(db, fileId, 'w');
+ var originData = 'Hello world';
+ it('should write data to file', function(done) {
+ // Write a text string
+ gridStore.write(originData, function(err) {
+ gridStore.close(done);
+ });
+ });
+
+ it('should read file', function (done) {
+ // use mongoskin style to create gridStore
+ db.gridStore(fileId, 'r').read(function(err, data) {
+ should.not.exist(err);
+ data.toString().should.equal(originData);
+ done();
+ })
+ });
+
+ it('should execute GridStore static methods', function(done) {
+ GridStore.exist(db, fileId, done)
+ })
+
+ // it('should unlink the file', function(done) {
+ // // reuse previous write mode gridStore;
+ // gridStore.unlink(function(err, result) {
+ // assert.equal(null, err);
+
+ // // Verify that the file no longer exists
+ // GridStore.exist(db, fileId, function(err, result) {
+ // assert.equal(null, err);
+ // assert.equal(false, result);
+ // done(err);
+ // });
+ // });
+ // })
+
+ })
+};
View
34 test/runner.js
@@ -12,20 +12,24 @@ var repl_db = new Db('repl_db', new ReplSetServers([
new Server('127.0.0.1', 27017)
]), {w:0, native_parser: true});
+function testDb(caseName, db) {
+ describe(caseName, function() {
+ ;[
+ require('./db'),
+ require('./collection'),
+ require('./cursor'),
+ require('./admin'),
+ require('./grid'),
+ require('./grid_store')
+ ].forEach(function(mod) {
+ mod.testWithDb(db);
+ })
-;[['connect_db', connect_db],['new_db', new_db]].forEach(function(dbdesc) {
- var db = dbdesc[1];
- describe(dbdesc[0], function() {
- ;[
- require('./collection'),
- require('./cursor'),
- require('./admin')
- ].forEach(function(mod) {
- mod.describe(db);
- })
+ after(function (done) {
+ db.close(done);
+ });
+ })
+}
- after(function (done) {
- db.close(done);
- });
- })
-})
+testDb('connect_db', connect_db);
+testDb('new_db', new_db);
View
110 test/utils.js
@@ -12,10 +12,116 @@
* Module dependencies.
*/
+var utils = require('../').utils;
var should = require('should');
-describe('utils.js', function () {
+describe('makeSkinClass', function () {
+
+ function Foo() {
+ this.isOpen = false;
+ }
+
+ Foo.prototype.chain = function() {
+ return this;
+ }
+
+ Foo.prototype.get = function(arg, callback) {
+ if(!this.isOpen) return callback(new Error('not open'));
+ callback(null, arg);
+ }
+
+ Foo.prototype.makeError = function(code, callback) {
+ var err = new Error('Error:' + code);
+ err.code = code;
+ callback(err);
+ }
+
+ var SkinFoo = utils.makeSkinClass(Foo);
+ SkinFoo._bindSetter('isOpen');
+ SkinFoo._bindGetter('isOpen');
+ SkinFoo.prototype._open = function(callback) {
+ var foo = new Foo();
+ var self = this;
+ setTimeout(function() {
+ if(self.willOpenError) {
+ var err = new Error();
+ err.code = 'ERROPEN';
+ return callback(err)
+ } else {
+ foo.isOpen = true;
+ callback(null, foo);
+ }
+ }, 10);
+ }
+
+ var skinFoo;
+ before(function() {
+
+ skinFoo = new SkinFoo();
+
+ })
+
+ describe('SkinClass', function() {
+ it('should call native method', function(done) {
+ skinFoo.get('echo', function(err, echo) {
+ should.not.exists(err);
+ echo.should.eql('echo');
+ done();
+ });
+ });
+
+ it('should callback error if error occused', function(done) {
+ skinFoo.makeError('123', function(err) {
+ err.code.should.eql('123');
+ done();
+ })
+ })
+
+ it('should chain operations', function(done) {
+ skinFoo.chain().chain().chain().get('echo', done);
+ })
+
+ it('should log open error if no callback', function(done) {
+ var errFoo = new SkinFoo();
+ errFoo.willOpenError = true;
+ errFoo.chain();
+ setTimeout(done, 15);
+ });
+
+ it('should callback open error in chain callback', function(done) {
+ var errFoo = new SkinFoo();
+ errFoo.willOpenError = true;
+ errFoo.chain().chain().chain().get(function(err) {
+ err.code.should.eql('ERROPEN');
+ done();
+ });
+ })
+
+ it('should get native property after open', function(done) {
+ skinFoo.isOpen.should.be.true;
+ done();
+ })
+
+ it('should set native property before open', function(done) {
+ var foo = new SkinFoo();
+ foo.isOpen = 'abc';
+ foo.open(function(err, p_foo) {
+ should.not.exists(err);
+ p_foo.isOpen.should.eql('abc');
+ done();
+ })
+ })
+
+ it('should close just while openning', function(done) {
+ var foo = new SkinFoo();
+ foo.chain().close(done);
+ })
+
+ it('should call close even closing or closed', function(done) {
+ var foo = new SkinFoo();
+ foo.chain().close().close(done);
+ })
+ })
-
});
View
432 test_results.md
@@ -1,7 +1,7 @@
-Already up-to-date.
# TOC
- [helper.id()](#helperid)
- [connect_db](#connect_db)
+ - [db.js](#connect_db-dbjs)
- [collection.js](#connect_db-collectionjs)
- [find(), findItems(), findEach()](#connect_db-collectionjs-find-finditems-findeach)
- [mock find() error](#connect_db-collectionjs-find-finditems-findeach-mock-find-error)
@@ -10,8 +10,13 @@ Already up-to-date.
- [updateById()](#connect_db-collectionjs-findbyid-updatebyid-removebyid-updatebyid)
- [removeById()](#connect_db-collectionjs-findbyid-updatebyid-removebyid-removebyid)
- [cursor.js](#connect_db-cursorjs)
- - [sort(), limit(), skip(), toArray(), count(), explain()](#connect_db-cursorjs-sort-limit-skip-toarray-count-explain)
+ - [db.admin()](#connect_db-dbadmin)
+ - [new Admin(db)](#connect_db-new-admindb)
+ - [db.grid()](#connect_db-dbgrid)
+ - [new Grid(db, fsName)](#connect_db-new-griddb-fsname)
+ - [grid_store.js](#connect_db-grid_storejs)
- [new_db](#new_db)
+ - [db.js](#new_db-dbjs)
- [collection.js](#new_db-collectionjs)
- [find(), findItems(), findEach()](#new_db-collectionjs-find-finditems-findeach)
- [mock find() error](#new_db-collectionjs-find-finditems-findeach-mock-find-error)
@@ -20,8 +25,13 @@ Already up-to-date.
- [updateById()](#new_db-collectionjs-findbyid-updatebyid-removebyid-updatebyid)
- [removeById()](#new_db-collectionjs-findbyid-updatebyid-removebyid-removebyid)
- [cursor.js](#new_db-cursorjs)
- - [sort(), limit(), skip(), toArray(), count(), explain()](#new_db-cursorjs-sort-limit-skip-toarray-count-explain)
- - [utils.js](#utilsjs)
+ - [db.admin()](#new_db-dbadmin)
+ - [new Admin(db)](#new_db-new-admindb)
+ - [db.grid()](#new_db-dbgrid)
+ - [new Grid(db, fsName)](#new_db-new-griddb-fsname)
+ - [grid_store.js](#new_db-grid_storejs)
+ - [makeSkinClass](#makeskinclass)
+ - [SkinClass](#makeskinclass-skinclass)
<a name=""></a>
<a name="helperid"></a>
@@ -50,8 +60,32 @@ ids.forEach(function (id) {
<a name="connect_db"></a>
# connect_db
+<a name="connect_db-dbjs"></a>
+## db.js
+skinDb.collection() should retrive native collection with callback.
+
+```js
+var skinColl = db.collection('testRetriveCollection', function(err, coll) {
+ should.not.exist(err);
+ skinColl._native.should.eql(coll);
+ done();
+});
+should.exist(skinColl);
+```
+
<a name="connect_db-collectionjs"></a>
## collection.js
+should retrive native cursor.
+
+```js
+db.collection('test_collection').find(function(err, cursor) {
+ should.not.exists(err);
+ cursor.toArray.should.be.instanceof(Function);
+ should.not.exists(cursor.open);
+ done();
+});
+```
+
<a name="connect_db-collectionjs-find-finditems-findeach"></a>
### find(), findItems(), findEach()
should find().toArray() return 100 comments.
@@ -261,12 +295,20 @@ db.article.removeById(id, function (err, success) {
<a name="connect_db-cursorjs"></a>
## cursor.js
-<a name="connect_db-cursorjs-sort-limit-skip-toarray-count-explain"></a>
-### sort(), limit(), skip(), toArray(), count(), explain()
+should call toArray().
+
+```js
+db.collection('testCursor').find().toArray(function(err, items) {
+ should.not.exist(err);
+ items.should.be.instanceof(Array).with.length(100);
+ done();
+});
+```
+
should cursor.skip(10).limit(10).toArray() return 10 rows.
```js
-db.testCursor.find().skip(10).limit(10).toArray(function (err, rows) {
+db.collection('testCursor').find().skip(10).limit(10).toArray(function (err, rows) {
should.not.exist(err);
should.exist(rows);
rows.should.be.instanceof(Array).with.length(10);
@@ -279,7 +321,7 @@ db.testCursor.find().skip(10).limit(10).toArray(function (err, rows) {
should cursor.sort({index: -1}).skip(20).limit(10).toArray() return 10 rows.
```js
-db.testCursor.find().sort({index: -1}).skip(20).limit(10).toArray(function (err, rows) {
+db.collection('testCursor').find().sort({index: -1}).skip(20).limit(10).toArray(function (err, rows) {
should.not.exist(err);
should.exist(rows);
rows.should.be.instanceof(Array).with.length(10);
@@ -292,7 +334,7 @@ db.testCursor.find().sort({index: -1}).skip(20).limit(10).toArray(function (err,
should cursor.count() return 100.
```js
-db.testCursor.find().count(function (err, count) {
+db.collection('testCursor').find().count(function (err, count) {
should.not.exist(err);
count.should.equal(100);
done();
@@ -302,7 +344,7 @@ db.testCursor.find().count(function (err, count) {
should cursor.explain() return 100.
```js
-db.testCursor.find({index: {$gt: 50}}).explain(function (err, result) {
+db.collection('testCursor').find({index: {$gt: 50}}).explain(function (err, result) {
should.not.exist(err);
result.should.have.property('cursor', 'BasicCursor');
result.should.have.property('nscanned', 100);
@@ -312,10 +354,156 @@ db.testCursor.find({index: {$gt: 50}}).explain(function (err, result) {
});
```
+<a name="connect_db-dbadmin"></a>
+## db.admin()
+should add the new user to the admin database.
+
+```js
+adminDb.addUser('admin3', 'admin3', done);
+```
+
+should authenticate using the newly added user.
+
+```js
+adminDb.authenticate('admin3', 'admin3', done);
+```
+
+should retrive the build information for the mongodb instance.
+
+```js
+adminDb.buildInfo(done);
+```
+
+should remove user just added.
+
+```js
+adminDb.removeUser('admin3', done);
+```
+
+<a name="connect_db-new-admindb"></a>
+## new Admin(db)
+should add the new user to the admin database.
+
+```js
+adminDb.addUser('admin3', 'admin3', done);
+```
+
+should authenticate using the newly added user.
+
+```js
+adminDb.authenticate('admin3', 'admin3', done);
+```
+
+should retrive the build information for the mongodb instance.
+
+```js
+adminDb.buildInfo(done);
+```
+
+should remove user just added.
+
+```js
+adminDb.removeUser('admin3', done);
+```
+
+<a name="connect_db-dbgrid"></a>
+## db.grid()
+should write data to grid.
+
+```js
+grid.put(originalData, {}, function(err, result) {
+ should.not.exist(err);
+ result._id.should.not.eql(id);
+ done();
+});
+```
+
+should get data just put to grid.
+
+```js
+grid.get(id, function(err, data) {
+ assert.deepEqual(originalData.toString('base64'), data.toString('base64'));
+ done(err);
+});
+```
+
+<a name="connect_db-new-griddb-fsname"></a>
+## new Grid(db, fsName)
+should write data to grid.
+
+```js
+grid.put(originalData, {}, function(err, result) {
+ should.not.exist(err);
+ result._id.should.not.eql(id);
+ done();
+});
+```
+
+should get data just put to grid.
+
+```js
+grid.get(id, function(err, data) {
+ assert.deepEqual(originalData.toString('base64'), data.toString('base64'));
+ done(err);
+});
+```
+
+<a name="connect_db-grid_storejs"></a>
+## grid_store.js
+should write data to file.
+
+```js
+// Write a text string
+gridStore.write(originData, function(err) {
+ gridStore.close(done);
+});
+```
+
+should read file.
+
+```js
+// use mongoskin style to create gridStore
+db.gridStore(fileId, 'r').read(function(err, data) {
+ should.not.exist(err);
+ data.toString().should.equal(originData);
+ done();
+})
+```
+
+should execute GridStore static methods.
+
+```js
+GridStore.exist(db, fileId, done)
+```
+
<a name="new_db"></a>
# new_db
+<a name="new_db-dbjs"></a>
+## db.js
+skinDb.collection() should retrive native collection with callback.
+
+```js
+var skinColl = db.collection('testRetriveCollection', function(err, coll) {
+ should.not.exist(err);
+ skinColl._native.should.eql(coll);
+ done();
+});
+should.exist(skinColl);
+```
+
<a name="new_db-collectionjs"></a>
## collection.js
+should retrive native cursor.
+
+```js
+db.collection('test_collection').find(function(err, cursor) {
+ should.not.exists(err);
+ cursor.toArray.should.be.instanceof(Function);
+ should.not.exists(cursor.open);
+ done();
+});
+```
+
<a name="new_db-collectionjs-find-finditems-findeach"></a>
### find(), findItems(), findEach()
should find().toArray() return 100 comments.
@@ -525,12 +713,20 @@ db.article.removeById(id, function (err, success) {
<a name="new_db-cursorjs"></a>
## cursor.js
-<a name="new_db-cursorjs-sort-limit-skip-toarray-count-explain"></a>
-### sort(), limit(), skip(), toArray(), count(), explain()
+should call toArray().
+
+```js
+db.collection('testCursor').find().toArray(function(err, items) {
+ should.not.exist(err);
+ items.should.be.instanceof(Array).with.length(100);
+ done();
+});
+```
+
should cursor.skip(10).limit(10).toArray() return 10 rows.
```js
-db.testCursor.find().skip(10).limit(10).toArray(function (err, rows) {
+db.collection('testCursor').find().skip(10).limit(10).toArray(function (err, rows) {
should.not.exist(err);
should.exist(rows);
rows.should.be.instanceof(Array).with.length(10);
@@ -543,7 +739,7 @@ db.testCursor.find().skip(10).limit(10).toArray(function (err, rows) {
should cursor.sort({index: -1}).skip(20).limit(10).toArray() return 10 rows.
```js
-db.testCursor.find().sort({index: -1}).skip(20).limit(10).toArray(function (err, rows) {
+db.collection('testCursor').find().sort({index: -1}).skip(20).limit(10).toArray(function (err, rows) {
should.not.exist(err);
should.exist(rows);
rows.should.be.instanceof(Array).with.length(10);
@@ -556,7 +752,7 @@ db.testCursor.find().sort({index: -1}).skip(20).limit(10).toArray(function (err,
should cursor.count() return 100.
```js
-db.testCursor.find().count(function (err, count) {
+db.collection('testCursor').find().count(function (err, count) {
should.not.exist(err);
count.should.equal(100);
done();
@@ -566,7 +762,7 @@ db.testCursor.find().count(function (err, count) {
should cursor.explain() return 100.
```js
-db.testCursor.find({index: {$gt: 50}}).explain(function (err, result) {
+db.collection('testCursor').find({index: {$gt: 50}}).explain(function (err, result) {
should.not.exist(err);
result.should.have.property('cursor', 'BasicCursor');
result.should.have.property('nscanned', 100);
@@ -576,3 +772,207 @@ db.testCursor.find({index: {$gt: 50}}).explain(function (err, result) {
});
```
+<a name="new_db-dbadmin"></a>
+## db.admin()
+should add the new user to the admin database.
+
+```js
+adminDb.addUser('admin3', 'admin3', done);
+```
+
+should authenticate using the newly added user.
+
+```js
+adminDb.authenticate('admin3', 'admin3', done);
+```
+
+should retrive the build information for the mongodb instance.
+
+```js
+adminDb.buildInfo(done);
+```
+
+should remove user just added.
+
+```js
+adminDb.removeUser('admin3', done);
+```
+
+<a name="new_db-new-admindb"></a>
+## new Admin(db)
+should add the new user to the admin database.
+
+```js
+adminDb.addUser('admin3', 'admin3', done);
+```
+
+should authenticate using the newly added user.
+
+```js
+adminDb.authenticate('admin3', 'admin3', done);
+```
+
+should retrive the build information for the mongodb instance.
+
+```js
+adminDb.buildInfo(done);
+```
+
+should remove user just added.
+
+```js
+adminDb.removeUser('admin3', done);
+```
+
+<a name="new_db-dbgrid"></a>
+## db.grid()
+should write data to grid.
+
+```js
+grid.put(originalData, {}, function(err, result) {
+ should.not.exist(err);
+ result._id.should.not.eql(id);
+ done();
+});
+```
+
+should get data just put to grid.
+
+```js
+grid.get(id, function(err, data) {
+ assert.deepEqual(originalData.toString('base64'), data.toString('base64'));
+ done(err);
+});
+```
+
+<a name="new_db-new-griddb-fsname"></a>
+## new Grid(db, fsName)
+should write data to grid.
+
+```js
+grid.put(originalData, {}, function(err, result) {
+ should.not.exist(err);
+ result._id.should.not.eql(id);
+ done();
+});
+```
+
+should get data just put to grid.
+
+```js
+grid.get(id, function(err, data) {
+ assert.deepEqual(originalData.toString('base64'), data.toString('base64'));
+ done(err);
+});
+```
+
+<a name="new_db-grid_storejs"></a>
+## grid_store.js
+should write data to file.
+
+```js
+// Write a text string
+gridStore.write(originData, function(err) {
+ gridStore.close(done);
+});
+```
+
+should read file.
+
+```js
+// use mongoskin style to create gridStore
+db.gridStore(fileId, 'r').read(function(err, data) {
+ should.not.exist(err);
+ data.toString().should.equal(originData);
+ done();
+})
+```
+
+should execute GridStore static methods.
+
+```js
+GridStore.exist(db, fileId, done)
+```
+
+<a name="makeskinclass"></a>
+# makeSkinClass
+<a name="makeskinclass-skinclass"></a>
+## SkinClass
+should call native method.
+
+```js
+skinFoo.get('echo', function(err, echo) {
+ should.not.exists(err);
+ echo.should.eql('echo');
+ done();
+});
+```
+
+should callback error if error occused.
+
+```js
+skinFoo.makeError('123', function(err) {
+ err.code.should.eql('123');
+ done();
+})
+```
+
+should chain operations.
+
+```js
+skinFoo.chain().chain().chain().get('echo', done);
+```
+
+should log open error if no callback.
+
+```js
+var errFoo = new SkinFoo();
+errFoo.willOpenError = true;
+errFoo.chain();
+setTimeout(done, 15);
+```
+
+should callback open error in chain callback.
+
+```js
+var errFoo = new SkinFoo();
+errFoo.willOpenError = true;
+errFoo.chain().chain().chain().get(function(err) {
+ err.code.should.eql('ERROPEN');
+ done();
+});
+```
+
+should get native property after open.
+
+```js
+skinFoo.isOpen.should.be.true;
+done();
+```
+
+should set native property before open.
+
+```js
+var foo = new SkinFoo();
+foo.isOpen = 'abc';
+foo.open(function(err, p_foo) {
+ should.not.exists(err);
+ p_foo.isOpen.should.eql('abc');
+ done();
+})
+```
+
+should close just while openning.
+
+```js
+var foo = new SkinFoo();
+foo.chain().close(done);
+```
+
+should call close even closing or closed.
+
+```js
+var foo = new SkinFoo();
+foo.chain().close().close(done);
+```
+
Please sign in to comment.
Something went wrong with that request. Please try again.