Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

767 lines (669 sloc) 19.092 kB

Already up-to-date.

TOC

admin.js

open()

should return admin.

var skinAdmin = new SkinAdmin(skinDb);
skinAdmin.state.should.equal(constant.STATE_CLOSE);
skinAdmin.open(function (err, admin) {
  should.not.exist(err);
  should.exist(admin);
  should.exist(skinAdmin.admin);
  skinAdmin.state.should.equal(constant.STATE_OPEN);
}).open(function (err, admin) {
  skinAdmin.open(function (err, admin) {
    should.not.exist(err);
    should.exist(admin);
    should.exist(skinAdmin.admin);
    skinAdmin.state.should.equal(constant.STATE_OPEN);
    done();
  });
});
skinAdmin.state.should.equal(constant.STATE_OPENNING);

should return mock open() error.

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();
});

collection.js

normal

open()

should return a collection.

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.

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.

skinDb.db.collection = function (name, options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = null;
  }
  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();
});

id()

should convert string id to ObjectID success.

var id = '4ec4b2b9f44a927223000001';
id = db.testcollection.id(id);
id.should.be.instanceof(db.testcollection.ObjectID);
id = db.testcollection.id(id);
id.should.be.instanceof(db.testcollection.ObjectID);
id = '4ec4b2b9f44a927223000foo';
id = db.testcollection.id(id);
id.should.be.instanceof(db.testcollection.ObjectID);

should return source id when id length !== 24.

var ids = [123, '4ec4b2b9f44a92722300000', 'abc', '4ec4b2b9f44a927223000f00123123'];
ids.forEach(function (id) {
  db.testcollection.id(id).should.equal(id);
});

find(), findItems(), findEach()

should find().toArray() return 100 comments.

db.comment.find().toArray(function (err, rows) {
  should.not.exist(err);
  rows.should.be.instanceof(Array).with.length(100);
  done();
});

should findItems(fn) all comments.

db.comment.findItems(function (err, comments) {
  should.not.exist(err);
  should.exist(comments);
  comments.should.be.instanceof(Array).with.length(100);
  done();
});

should findItems({} fn) all comments.

db.comment.findItems(function (err, comments) {
  should.not.exist(err);
  should.exist(comments);
  comments.should.be.instanceof(Array).with.length(100);
  done();
});

should findItems({limit: 10}) query wrong return top 0 comments.

db.comment.findItems({limit: 10}, function (err, comments) {
  should.not.exist(err);
  comments.should.be.instanceof(Array).with.length(0);
  done();
});

should findItems({}, {limit: 10}) return top 10 comments.

db.comment.findItems({}, {limit: 10}, function (err, comments) {
  should.not.exist(err);
  comments.should.be.instanceof(Array).with.length(10);
  done();
});

should findEach(fn) call fn 100 times.

var count = 0;
db.comment.findEach(function (err, comment) {
  should.not.exist(err);
  if (!comment) {
    count.should.equal(100);
    return done();
  }
  count++;
});

should findEach({}, {limit: 20}, fn) call fn 20 times.

var count = 0;
db.comment.findEach({}, {limit: 20}, function (err, comment) {
  should.not.exist(err);
  if (!comment) {
    count.should.equal(20);
    return done();
  }
  count++;
});

mock find() error

should findItems() error.

db.comment.findItems(function (err, docs) {
  should.exist(err);
  err.should.be.instanceof(Error).with.have.property('message', 'mock find() error');
  should.not.exist(docs);
  done();
});

should findEach() error.

db.comment.findEach(function (err, docs) {
  should.exist(err);
  err.should.be.instanceof(Error).with.have.property('message', 'mock find() error');
  should.not.exist(docs);
  done();
});

findById(), updateById(), removeById()

findById()

should find one object by ObjectID.

db.article.findById(articleId, function (err, article) {
  should.not.exist(err);
  should.exist(article);
  article.should.have.property('_id').with.instanceof(db.ObjectID);
  article.should.have.property('created_at').with.instanceof(Date);
  article.should.have.property('title').with.include(now.toString());
  article.created_at.toString().should.equal(now.toString());
  done();
});

should find one object by String id.

db.article.findById(articleId.toString(), function (err, article) {
  should.not.exist(err);
  should.exist(article);
  article.should.have.property('_id').with.instanceof(db.ObjectID);
  article.should.have.property('created_at').with.instanceof(Date);
  article.should.have.property('title').with.include(now.toString());
  article.created_at.toString().should.equal(now.toString());
  done();
});

should not find when id not exists.

db.article.findById('foo', function (err, article) {
  should.not.exist(err);
  should.not.exist(article);
  done();
});

updateById()

should update obj by id.

var updatedTime = new Date();
var doc = {
  $set: {
    title: 'new title ' + updatedTime,
    updated_at: updatedTime
  }
};
db.article.updateById(articleId.toString(), doc, function (err, article) {
  should.not.exist(err);
  should.not.exist(article);
  db.article.findById(articleId, function (err, article) {
    should.not.exist(err);
    should.exist(article);
    article.should.have.property('title', 'new title ' + updatedTime);
    article.should.have.property('updated_at').with.instanceof(Date);
    article.updated_at.toString().should.equal(updatedTime.toString());
    done();
  });
});

removeById()

should remove obj by id.

var id = articleId.toString();
db.article.findById(id, function (err, article) {
  should.not.exist(err);
  should.exist(article);
  db.article.removeById(id, function (err, article) {
    should.not.exist(err);
    should.not.exist(article);
    db.article.findById(id, function (err, article) {
      should.not.exist(err);
      should.not.exist(article);
      done();
    });
  });
});

cursor.js

normal

new SkinCursor()

should state is open when cursor exists.

var cursor = new SkinCursor({}, {});
cursor.should.have.property('state', constant.STATE_OPEN);

should state is close when cursor not exists.

var cursor = new SkinCursor(null, {});
cursor.should.have.property('state', constant.STATE_CLOSE);

open()

should success when state is close.

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.

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.

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.

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();
});

sort(), limit(), skip(), toArray(), count(), explain()

should cursor.skip(10).limit(10).toArray() return 10 rows.

db.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);
  rows[0].name.should.equal('name 10');
  rows[9].name.should.equal('name 19');
  done();
});

should cursor.sort({index: -1}).skip(20).limit(10).toArray() return 10 rows.

db.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);
  rows[0].name.should.equal('name 79');
  rows[9].name.should.equal('name 70');
  done();
});

should cursor.count() return 100.

db.testCursor.find().count(function (err, count) {
  should.not.exist(err);
  count.should.equal(100);
  done();
});

should cursor.explain() return 100.

db.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);
  result.should.have.property('nscannedObjects', 100);
  result.should.have.property('n', 49);
  done();
});

db.js

normal

bind()

should throw error when collection name wrong.

var wrongNames = ['', null, 123, '    ', '\n   \t   ', undefined, 0, 1, new Date(), {}, []];
wrongNames.forEach(function (name) {
  (function () {
    db.bind(name);
  }).should.throw('Must provide collection name to bind.');
});

should throw error when options is not object.

(function () {
  db.bind('foo', function () {});
}).should.throw('the args[1] should be object, but is `function () {}`');

should add helper methods to collection.

db.bind('testCollection', {
  totalCount: function (calllback) {
    this.count(calllback);
  }
});
db.should.have.property('testCollection').with.have.property('totalCount').with.be.a('function');
db.testCollection.totalCount(function (err, total) {
  should.not.exist(err);
  total.should.equal(0);
  done();
});

should add options and helper methods to collection.

db.bind('testExistsCollection', {safe: true}, {
  totalCount: function (calllback) {
    this.count(calllback);
  }
});
db.should.have.property('testExistsCollection').with.have.property('totalCount').with.be.a('function');
db.testExistsCollection.insert({name: 'item2'}, function (err, row) {
  should.not.exist(err);
  should.exist(row);
  db.testExistsCollection.findItems(function (err, rows) {
    should.not.exist(err);
    rows.should.length(2);
    rows[0].name.should.equal('item1');
    rows[1].name.should.equal('item2');
    db.testExistsCollection.totalCount(function (err, total) {
      should.not.exist(err);
      total.should.equal(2);
      done();
    });
  });
});

should throw error when bind collection not exists in safe mode.

db.bind('notExistsCollection', {safe: true});
db.notExistsCollection.count(function (err, count) {
  should.exist(err);
  err.should.have.property('message', 'Collection notExistsCollection does not exist. Currently in strict mode.');
  should.not.exist(count);
  done();
});

gridfs()

should start gridfs store.

db.gridfs();
db.should.have.property('skinGridStore');

open()

should open a database connection.

var db1 = mongoskin.db(servers, options);
db1.state.should.equal(0);
db1.open(function (err) {
  should.not.exist(err);
  db1.state.should.equal(2);
}).open(function (err) {
  should.not.exist(err);
  db1.state.should.equal(2);
  done();
});
db1.state.should.equal(1);

should open a database connection with user auth fail.

var db2 = mongoskin.db(authfailServers, authfailOptions);
done = pedding(2, done);
db2.state.should.equal(constant.STATE_CLOSE);
db2.open(function (err, db) {
  should.exist(err);
  err.should.have.property('message', 'auth fails');
  err.should.have.property('name', 'MongoError');
  should.not.exist(db);
  db2.state.should.equal(constant.STATE_CLOSE);
  // open again
  db2.open(function (err, db) {
    should.exist(err);
    err.should.have.property('message', 'auth fails');
    err.should.have.property('name', 'MongoError');
    should.not.exist(db);
    db2.state.should.equal(constant.STATE_CLOSE);
    db2.open(function (err, db) {
      should.exist(err);
      err.should.have.property('message', 'auth fails');
      err.should.have.property('name', 'MongoError');
      should.not.exist(db);
      db2.state.should.equal(constant.STATE_CLOSE);
      done();
    });
  });
});
db2.state.should.equal(constant.STATE_OPENNING);
db2.open(function (err, db) {
  should.exist(err);
  err.should.have.property('message', 'auth fails');
  err.should.have.property('name', 'MongoError');
  should.not.exist(db);
  done();
});

should open 100 times ok.

var db3 = mongoskin.db(servers, options);
done = pedding(100, done);
for (var i = 0; i < 100; i++) {
  db3.open(function (err, db) {
    should.not.exist(err);
    should.exist(db);
    done();
  });
}

should open timeout when connect the blackhole.

var db;
if (isReplicaset) {
  db = mongoskin.db(['127.0.0.1:' + blackholePort, '127.0.0.1:' + blackholePort], 
    options, {socketOptions: {timeout: 500}});
} else {
  var ops = {};
  for (var k in options) {
    ops[k] = options[k];
  }
  ops.socketOptions = {timeout: 500};
  db = mongoskin.db('127.0.0.1:' + blackholePort, ops);
}
db.open(function (err, db) {
  should.exist(err);
  if (isReplicaset) {
    // replicaset should not timeout error
    err.should.have.property('message', 'no primary server found in set');
  } else {
    err.should.have.property('err', 'connection to [127.0.0.1:24008] timed out');
  }
  should.not.exist(db);
  done();
});

close()

should close a database connection.

var dbClose = mongoskin.db(servers, options);
dbClose.state.should.equal(0);
dbClose.close(function (err) {
  dbClose.state.should.equal(0);
  should.not.exist(err);
}).open(function (err) {
  dbClose.state.should.equal(2);
  should.not.exist(err);
}).close(function (err) {
  dbClose.state.should.equal(0);
  should.not.exist(err);
  done();
});
dbClose.state.should.equal(1);

should close 100 times ok.

var db3 = mongoskin.db(servers, options);
done = pedding(100, done);
db.open();
for (var i = 0; i < 100; i++) {
  db3.close(function (err) {
    should.not.exist(err);
    done();
  });
}

ensureIndex()

should index infos is empty.

var barDb = mongoskin.db(servers, options);
barDb.indexInformation('not-exists', function (err, result) {
  should.not.exist(err);
  should.exist(result);
  result.should.eql({});
  done();
});

should get index infos error.

var barDb = mongoskin.db(authfailServers, authfailOptions);
barDb.indexInformation('not-exists', function (err, result) {
  should.exist(err);
  should.not.exist(result);
  done();
});

should create title:1 index success.

db.ensureIndex('foo', {title: 1}, function (err, result) {
  should.not.exist(err);
  should.exist(result);
  result.should.equal('title_1');
  done();
});

should create title:-1 index success.

db.ensureIndex('foo', {title: -1}, function (err, result) {
  should.not.exist(err);
  should.exist(result);
  result.should.equal('title_-1');
  done();
});

should get all index infos.

db.indexInformation('foo', function (err, result) {
  should.not.exist(err);
  should.exist(result);
  result.should.eql({
    _id_: [ [ '_id', 1 ] ],
    title_1: [ [ 'title', 1 ] ],
    'title_-1': [ [ 'title', -1 ] ] 
  });
  done();
});

process exit, stop mongod...

Jump to Line
Something went wrong with that request. Please try again.