add `lab` for unit testing; add tests for `lib.utils.isStringAnInt` (issue #309)

This commit is contained in:
Christopher Van 2014-12-19 15:44:22 -08:00
Родитель 5b4a9d61b5
Коммит 160b444e2d
6 изменённых файлов: 65 добавлений и 8 удалений

Просмотреть файл

@ -1,5 +1,5 @@
language: node_js
node_js:
- "0.11"
- "0.10"
before_script:
- cp ./settings_test.js.dist ./settings_test.js

Просмотреть файл

@ -84,6 +84,10 @@ To run tests:
npm test
To run tests without coverage and linting:
npm run lab
## Database

Просмотреть файл

@ -106,7 +106,7 @@ Game.objects.create = function (db, data) {
Game.objects._select = function (db, data, columns) {
return new Promise(function (resolve, reject) {
var query = (utils.isNumeric(data.idOrSlug) ?
var query = (utils.isStringAnInt(data.idOrSlug) ?
'SELECT ' + columns + ' FROM games WHERE id = $1 AND deleted = false' :
'SELECT ' + columns + ' FROM games WHERE slug = $1 AND deleted = false'
);
@ -140,7 +140,7 @@ Game.objects.remove = function (db, data) {
return Game.objects.exists(db, data).then(function () {
return new Promise(function (resolve, reject) {
var query = (utils.isNumeric(data.idOrSlug) ?
var query = (utils.isStringAnInt(data.idOrSlug) ?
'UPDATE games SET deleted = true WHERE id = $1' :
'UPDATE games SET deleted = true WHERE slug = $1'
);

Просмотреть файл

@ -5,12 +5,13 @@ var Promise = require('es6-promise').Promise;
module.exports.errors = require('./errors');
module.exports.isNumeric = function isNumeric(obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj);
module.exports.isStringAnInt = function (str) {
var num = Number(str);
return String(num) === str && num >= 0;
};
module.exports.stringifyObject = stringifyObject = function (obj) {
var stringifyObject = module.exports.stringifyObject = function (obj) {
return obj instanceof Object ? JSON.stringify(obj) : obj;
};

Просмотреть файл

@ -14,7 +14,9 @@
"gulp": "^3.8.7",
"gulp-jshint": "^1.8.4",
"jshint-stylish": "^0.4.0",
"nodemon": "^1.2.1"
"nodemon": "^1.2.1",
"code": "^1.2.1",
"lab": "^5.1.0"
},
"engines": {
"node": ">= 0.10.x",
@ -28,7 +30,10 @@
"scripts": {
"start": "node index.js",
"dev": "NODE_ENV=development GALAXY_API_SETTINGS=./settings_dev.js nodemon index.js",
"prod": "NODE_ENV=production GALAXY_API_SETTINGS=./settings_prod.js node index.js"
"prod": "NODE_ENV=production GALAXY_API_SETTINGS=./settings_prod.js node index.js",
"test": "./node_modules/lab/bin/lab -c -L",
"lab": "./node_modules/lab/bin/lab",
"test-cover": "./node_modules/lab/bin/lab -c -r html -o ./test/artifacts/coverage.html && open ./test/artifacts/coverage.html"
},
"version": "0.0.5"
}

47
test/lib/utils/index.js Normal file
Просмотреть файл

@ -0,0 +1,47 @@
var Lab = require('lab');
var Code = require('code');
var utils = require('../../../lib/utils');
var lab = exports.lab = Lab.script();
lab.experiment('utils.isStringAnInt', function () {
lab.test('returns true for zero', function (done) {
Code.expect(utils.isStringAnInt('0')).to.equal(true);
done();
});
lab.test('returns true for positive integers', function (done) {
Code.expect(utils.isStringAnInt('5')).to.equal(true);
done();
});
lab.test('returns true for negative integers', function (done) {
Code.expect(utils.isStringAnInt('5')).to.equal(true);
done();
});
lab.test('returns false for strings', function (done) {
Code.expect(utils.isStringAnInt('a')).to.equal(false);
done();
});
lab.test('returns false for strings with integers', function (done) {
Code.expect(utils.isStringAnInt('a5')).to.equal(false);
done();
});
lab.test('returns false for floats', function (done) {
Code.expect(utils.isStringAnInt('5.0')).to.equal(false);
done();
});
});