diff --git a/package.json b/package.json index 10de3b239ff73326e72ed905ed3767b3a734809d..87749f103ccbd79144687ffb214de8be9c802a1e 100644 --- a/package.json +++ b/package.json @@ -32,5 +32,9 @@ "mongodb": "^2.2.5", "osprey": "^0.3.2", "raml2html": "^2.4.0" + }, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^3.0.2" } } diff --git a/test/global.js b/test/global.js new file mode 100644 index 0000000000000000000000000000000000000000..b3bc09cac34de9bb936e4c9c1eaf891549114fe6 --- /dev/null +++ b/test/global.js @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana + * + * This file is part of blendb. + * + * blendb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * blendb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with blendb. If not, see <http://www.gnu.org/licenses/>. + */ + +'use strict'; + +process.env.NODE_ENV = 'test'; + +global.expect = require('chai').expect; + +// Add the ./src directory to require's search path to facilitate import +// modules later on (avoiding the require('../../../../module') problem). +require('app-module-path').addPath(__dirname + '/../src'); diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000000000000000000000000000000000000..150fdfab63872536b78cb253d8be6af0c4a66fee --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,9 @@ +--require ./test/global.js +--reporter spec +--ui bdd +--recursive +--colors +--timeout 60000 +--slow 300 +--check-leaks +--globals expect diff --git a/test/util/hash.spec.js b/test/util/hash.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..2de7ca6e2187a77a07bb68ab91597c006cb8ee8d --- /dev/null +++ b/test/util/hash.spec.js @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana + * + * This file is part of blendb. + * + * blendb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * blendb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with blendb. If not, see <http://www.gnu.org/licenses/>. + */ + +'use strict'; + +/* globals expect */ +/* jshint -W024 */ +/* jshint expr:true, maxlen:false */ +/* jscs:disable maximumLineLength */ + +const hash = require('util/hash'); + +describe('hash utility library', () => { + it('should generate a sha1 hash for a collection of objects', () => { + let h = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + + expect(h).to.be.a('string'); + expect(h).to.not.be.empty; + }); + + it('should generate the same hash for the same input', () => { + let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + let h2 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + + expect(h1).to.be.a('string'); + expect(h2).to.be.a('string'); + expect(h1).to.be.equal(h2); + }); + + it('should generate the same hash for different order of input', () => { + let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + let h2 = hash.sha1('test', ['list', 'of', 'things'], { obj: 'test' }); + + expect(h1).to.be.a('string'); + expect(h2).to.be.a('string'); + expect(h1).to.be.equal(h2); + }); + + it('should not generate the same hash for distinct input', () => { + let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + let h2 = hash.sha1('test', { obj: 'test', x: true }, + ['list', 'of', 'things']); + + expect(h1).to.be.a('string'); + expect(h2).to.be.a('string'); + expect(h1).to.not.be.equal(h2); + }); + + it('should not generate the same hash for different order in deep lists', () => { + let h1 = hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + let h2 = hash.sha1('test', { obj: 'test' }, ['of', 'list', 'things']); + + expect(h1).to.be.a('string'); + expect(h2).to.be.a('string'); + expect(h1).to.not.be.equal(h2); + }); +});