more tests

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 77894915eb
commit fd0043f36a

@ -161,8 +161,11 @@ mongoModule.createIndices = function (callback) {
mongoModule.checkCompatibility = function (callback) {
var mongoPkg = require('mongodb/package.json');
mongoModule.checkCompatibilityVersion(mongoPkg.version, callback);
};
if (semver.lt(mongoPkg.version, '2.0.0')) {
mongoModule.checkCompatibilityVersion = function (version, callback) {
if (semver.lt(version, '2.0.0')) {
return callback(new Error('The `mongodb` package is out-of-date, please run `./nodebb setup` again.'));
}
@ -173,6 +176,8 @@ mongoModule.info = function (db, callback) {
if (!db) {
return callback();
}
async.waterfall([
function (next) {
async.parallel({
serverStatus: function (next) {
db.command({ serverStatus: 1 }, next);
@ -181,19 +186,11 @@ mongoModule.info = function (db, callback) {
db.command({ dbStats: 1 }, next);
},
listCollections: function (next) {
db.listCollections().toArray(function (err, items) {
if (err) {
return next(err);
}
async.map(items, function (collection, next) {
db.collection(collection.name).stats(next);
getCollectionStats(db, next);
},
}, next);
});
},
}, function (err, results) {
if (err) {
return callback(err);
}
function (results, next) {
var stats = results.stats;
var scale = 1024 * 1024 * 1024;
@ -229,10 +226,25 @@ mongoModule.info = function (db, callback) {
stats.uptime = results.serverStatus.uptime;
stats.mongo = true;
callback(null, stats);
});
next(null, stats);
},
], callback);
};
mongoModule.close = function () {
db.close();
function getCollectionStats(db, callback) {
async.waterfall([
function (next) {
db.listCollections().toArray(next);
},
function (items, next) {
async.map(items, function (collection, next) {
db.collection(collection.name).stats(next);
}, next);
},
], callback);
}
mongoModule.close = function (callback) {
callback = callback || function () {};
db.close(callback);
};

@ -1,6 +1,7 @@
'use strict';
var _ = require('underscore');
var async = require('async');
var winston = require('winston');
var nconf = require('nconf');
var semver = require('semver');
@ -71,10 +72,6 @@ redisModule.connect = function (options) {
var redis_socket_or_host = nconf.get('redis:host');
var cxn;
if (!redis) {
redis = require('redis');
}
options = options || {};
if (nconf.get('redis:password')) {
@ -101,10 +98,10 @@ redisModule.connect = function (options) {
}
var dbIdx = parseInt(nconf.get('redis:database'), 10);
if (dbIdx) {
cxn.select(dbIdx, function (error) {
if (error) {
winston.error('NodeBB could not connect to your Redis database. Redis returned the following error: ' + error.message);
if (dbIdx >= 0) {
cxn.select(dbIdx, function (err) {
if (err) {
winston.error('NodeBB could not connect to your Redis database. Redis returned the following error: ' + err.message);
process.exit();
}
});
@ -118,32 +115,37 @@ redisModule.createIndices = function (callback) {
};
redisModule.checkCompatibility = function (callback) {
redisModule.info(redisModule.client, function (err, info) {
if (err) {
return callback(err);
}
async.waterfall([
function (next) {
redisModule.info(redisModule.client, next);
},
function (info, next) {
redisModule.checkCompatibilityVersion(info.redis_version, next);
},
], callback);
};
if (semver.lt(info.redis_version, '2.8.9')) {
redisModule.checkCompatibilityVersion = function (version, callback) {
if (semver.lt(version, '2.8.9')) {
return callback(new Error('Your Redis version is not new enough to support NodeBB, please upgrade Redis to v2.8.9 or higher.'));
}
callback();
});
};
redisModule.close = function () {
redisClient.quit();
redisModule.close = function (callback) {
callback = callback || function () {};
redisClient.quit(callback);
};
redisModule.info = function (cxn, callback) {
if (!cxn) {
return callback();
}
cxn.info(function (err, data) {
if (err) {
return callback(err);
}
async.waterfall([
function (next) {
cxn.info(next);
},
function (data, next) {
var lines = data.toString().split('\r\n').sort();
var redisData = {};
lines.forEach(function (line) {
@ -156,8 +158,9 @@ redisModule.info = function (cxn, callback) {
redisData.raw = JSON.stringify(redisData, null, 4);
redisData.redis = true;
callback(null, redisData);
});
next(null, redisData);
},
], callback);
};
redisModule.helpers = redisModule.helpers || {};

@ -2,6 +2,7 @@
var assert = require('assert');
var nconf = require('nconf');
var db = require('./mocks/databasemock');
@ -12,6 +13,7 @@ describe('Test database', function () {
});
});
describe('info', function () {
it('should return info about database', function (done) {
db.info(db.client, function (err, info) {
assert.ifError(err);
@ -20,6 +22,37 @@ describe('Test database', function () {
});
});
it('should not error and return null if client is falsy', function (done) {
db.info(null, function (err, info) {
assert.ifError(err);
assert.equal(info, null);
done();
});
});
});
describe('checkCompatibility', function () {
it('should not throw', function (done) {
db.checkCompatibility(done);
});
it('should return error with a too low version', function (done) {
var dbName = nconf.get('database');
if (dbName === 'redis') {
db.checkCompatibilityVersion('2.4.0', function (err) {
assert.equal(err.message, 'Your Redis version is not new enough to support NodeBB, please upgrade Redis to v2.8.9 or higher.');
done();
});
} else if (dbName === 'mongo') {
db.checkCompatibilityVersion('1.8.0', function (err) {
assert.equal(err.message, 'The `mongodb` package is out-of-date, please run `./nodebb setup` again.');
done();
});
}
});
});
require('./database/keys');
require('./database/list');
require('./database/sets');

@ -9,11 +9,18 @@ describe('List methods', function () {
describe('listAppend()', function () {
it('should append to a list', function (done) {
db.listAppend('testList1', 5, function (err) {
assert.equal(err, null);
assert.ifError(err);
assert.equal(arguments.length, 1);
done();
});
});
it('should not add anyhing if key is falsy', function (done) {
db.listAppend(null, 3, function (err) {
assert.ifError(err);
done();
});
});
});
describe('listPrepend()', function () {
@ -38,6 +45,13 @@ describe('List methods', function () {
done();
});
});
it('should not add anyhing if key is falsy', function (done) {
db.listPrepend(null, 3, function (err) {
assert.ifError(err);
done();
});
});
});
describe('getListRange()', function () {
@ -83,6 +97,14 @@ describe('List methods', function () {
done();
});
});
it('should not get anyhing if key is falsy', function (done) {
db.getListRange(null, 0, -1, function (err, data) {
assert.ifError(err);
assert.equal(data, undefined);
done();
});
});
});
describe('listRemoveLast()', function () {
@ -105,6 +127,13 @@ describe('List methods', function () {
done();
});
});
it('should not remove anyhing if key is falsy', function (done) {
db.listRemoveLast(null, function (err) {
assert.ifError(err);
done();
});
});
});
describe('listRemoveAll()', function () {
@ -132,6 +161,13 @@ describe('List methods', function () {
});
});
});
it('should not remove anyhing if key is falsy', function (done) {
db.listRemoveAll(null, 3, function (err) {
assert.ifError(err);
done();
});
});
});
describe('listTrim()', function () {
@ -156,6 +192,13 @@ describe('List methods', function () {
});
});
});
it('should not add anyhing if key is falsy', function (done) {
db.listTrim(null, 0, 3, function (err) {
assert.ifError(err);
done();
});
});
});

@ -81,6 +81,10 @@
var db = require('../../src/database');
after(function (done) {
db.close(done);
});
before(function (done) {
this.timeout(30000);
var meta;
@ -91,6 +95,9 @@
function (next) {
db.emptydb(next);
},
function (next) {
db.createIndices(next);
},
function (next) {
winston.info('test_database flushed');
meta = require('../../src/meta');

Loading…
Cancel
Save