diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 23c45f1081113185c3e4ff13cd29b97916d4d58f..04cdb1f9f46441a3617636eea51bf6403f1e2ab9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,8 +19,7 @@ run_tests: - mv config.json.example config.json - sed -i -e 's/false/true/g' config.json - gulp build - - cd build/ - - mocha + - gulp test tags: - node cache: diff --git a/README.md b/README.md index 38e66ae5a87abc71c2e4e8711f4f06b42aa98f6d..74ecd6234f14689c66dc397dbf7af2845ca1e76a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # SIMCAQ +[](https://gitlab.c3sl.ufpr.br/simcaq/simcaq-node/commits/development) +[](https://gitlab.c3sl.ufpr.br/simcaq/simcaq-node/commits/development) # Dependencies @@ -16,9 +18,9 @@ Previous versions of Node.js do not support ECMAScript6, it is recommended to us > nvm use v4.5.0 -4) Install babel and gulp globally +4) Install the global dependencies -> npm install --global gulp gulp-cli babel babel-cli babel-core babel-register mocha gulp-mocha gulp-eslint +> npm install --global gulp gulp-cli babel babel-cli babel-core babel-register mocha gulp-mocha gulp-eslint istanbul 5) Install project dependencies diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 10fd314abbdc370592336e4bf0222270c33536dc..b2758b0a6abe55329d30fb9658e0547e5fcb150d 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,3 +1,5 @@ +require('babel-core/register'); + const fs = require('fs'); const gulp = require('gulp'); @@ -8,6 +10,8 @@ const eslint = require('gulp-eslint'); const mocha = require('gulp-mocha'); +const istanbul = require('gulp-istanbul'); + const nodemon = require('gulp-nodemon'); const Cache = require('gulp-file-cache'); @@ -61,14 +65,26 @@ gulp.task('docco', () => { gulp.task('doc', ['docco']); -gulp.task('test', () => { +gulp.task('pre-test', () => { + return gulp.src(['build/**/*.js', '!build/{test,test/**}']) + .pipe(istanbul()) + .pipe(istanbul.hookRequire()); +}); + +gulp.task('test', ['pre-test'], () => { process.chdir('build'); gulp.src('test/test.js', {read: false}) .pipe(mocha()) - .once('error', () => { + .pipe(istanbul.writeReports()) + .pipe(istanbul.enforceThresholds({ + thresholds: { + global: 80 + } + })) + .on('error', () => { process.exit(1); }) - .once('end', () => { + .on('end', () => { process.exit(); }); }); diff --git a/package.json b/package.json index ed17027d11134108f1acaa338f236a05fa40478b..acc29cd2e3d6ff9a605fde13cfaa6774243b6307 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "babel-register": "^6.14.0", "babelify": "^7.3.0", "browserify": "^13.1.0", + "chai-xml": "^0.3.1", "docdash": "^0.4.0", "eslint": "^3.3.1", "eslint-config-airbnb": "^10.0.1", @@ -52,6 +53,7 @@ "gulp-eslint": "^3.0.1", "gulp-file-cache": "0.0.1", "gulp-function": "^1.3.6", + "gulp-istanbul": "^1.1.1", "gulp-jsdoc3": "^0.3.0", "gulp-mocha": "^3.0.1", "gulp-nodemon": "^2.1.0", diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index b6f434ce0d47c873995975287765fe3bd3f890ff..9fe6e903bdbe9bc10b89181866a920792b832533 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -48,11 +48,6 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => { next(); }, query, response('adm_dependency')); -enrollmentApp.get('/data', (req, res, next) => { - req.sql = squel.select().from('turmas'); - next(); -}, query, response('data')); - // Parse the filters and dimensions parameter in the query enrollmentApp.use('/', parseParams('filter', [ 'min_year', diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index 8662d945f4f39989bc318709e7beab9802a3bfb8..6c92430b425d3b9fde8c682216c8f6a10f07b75f 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -37,8 +37,8 @@ schoolApp.get('/:id', (req, res, next) => { schoolApp.get('/state/:id', (req, res, next) => { req.sql.from('escolas') .field('pk_escola_id') - .field('nome_entidade', 'name') - .field('ano_censo', 'year') + .field('nome_entidade') + .field('ano_censo') .field('fk_cod_estado') .field('fk_cod_municipio') .where('fk_cod_estado = ?', parseInt(req.params.id, 10)); @@ -49,8 +49,8 @@ schoolApp.get('/state/:id', (req, res, next) => { schoolApp.get('/city/:id', (req, res, next) => { req.sql.from('escolas') .field('pk_escola_id') - .field('nome_entidade', 'name') - .field('ano_censo', 'year') + .field('nome_entidade') + .field('ano_censo') .field('fk_cod_estado') .field('fk_cod_municipio') .where('fk_cod_municipio = ?', parseInt(req.params.id, 10)); diff --git a/src/server.js b/src/server.js index f18adf4916a38c192e4ab986a5fd17c5254fe206..0eef8dfa5fb4764522f0b1c0be02da239603d347 100644 --- a/src/server.js +++ b/src/server.js @@ -18,3 +18,6 @@ app.set('ip', process.env.IP || config.get('ip') || '127.0.0.1'); const server = app.listen(app.get('port'), () => { log.info(`Express server listening on port ${server.address().port}`); }); + +// For testing +module.exports = server; diff --git a/src/test/test.js b/src/test/test.js index f06f837ef405c00dd4bc5bdb5f5c812f722c6ccb..066b7b77c9651dbde50b97cd6ab730c78053d27b 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -1,9 +1,15 @@ +process.env.NODE_ENV = 'test'; + const chai = require('chai'); const dirtyChai = require('dirty-chai'); chai.use(dirtyChai); +const chaiXml = require('chai-xml'); + +chai.use(chaiXml); + const chaiHttp = require('chai-http'); const assert = chai.assert; @@ -18,7 +24,62 @@ const server = require(`${libs}/app`); chai.use(chaiHttp); +describe('API is running', () => { + it('should respond it\'s running', (done) => { + chai.request(server) + .get('/api/v1') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('msg'); + done(); + }) + }); +}); + describe('request enrollments', () => { + it('should list the year range', (done) => { + chai.request(server) + .get('/api/v1/enrollment/year_range') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('start_year'); + res.body.result[0].should.have.property('end_year'); + done(); + }); + }); + + it('should list the education level', (done) => { + chai.request(server) + .get('/api/v1/enrollment/education_level') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('id'); + res.body.result[0].should.have.property('name'); + done(); + }); + }); + + it('should list the administrative dependencies', (done) => { + chai.request(server) + .get('/api/v1/enrollment/adm_dependency ') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('id'); + res.body.result[0].should.have.property('name'); + done(); + }); + }); + it('should list enrollments', (done) => { chai.request(server) .get('/api/v1/enrollment') @@ -32,6 +93,84 @@ describe('request enrollments', () => { done(); }); }); + + it('should list enrollments with valid filters', (done) => { + chai.request(server) + .get('/api/v1/enrollment?filter=min_year:2010,state:41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('name'); + res.body.result[0].should.have.property('total'); + done(); + }); + }); + + it('should list enrollments with invalid filters', (done) => { + chai.request(server) + .get('/api/v1/enrollment?filter=foo:2010,bar:41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('name'); + res.body.result[0].should.have.property('total'); + done(); + }); + }); + + it('should list enrollments with valid dimensions', (done) => { + chai.request(server) + .get('/api/v1/enrollment?dims=region,state,adm_dependency_id,location_id&filter=min_year:2014,region:4') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('region_name'); + res.body.result[0].should.have.property('state_name'); + res.body.result[0].should.have.property('adm_dependency_name'); + res.body.result[0].should.have.property('total'); + done(); + }); + }); + + it('should list enrollments with invalid dimensions', (done) => { + chai.request(server) + .get('/api/v1/enrollment?dims=foo,bar') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('name'); + res.body.result[0].should.have.property('total'); + done(); + }); + }); + + it('should list enrollments with valid dimensions and filters', (done) => { + chai.request(server) + .get('/api/v1/enrollment?dims=region,state,education_level_id,school&filter=min_year:2013,max_year:2014,city:3287') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('region_name'); + res.body.result[0].should.have.property('state_name'); + res.body.result[0].should.have.property('school_name'); + res.body.result[0].should.have.property('education_level'); + res.body.result[0].should.have.property('total'); + res.body.result[0].should.have.property('year'); + done(); + }); + }); + + }); describe('request regions', () => { @@ -161,4 +300,99 @@ describe('request cities', () => { done(); }); }); + + it('should list all cities from a state', (done) => { + chai.request(server) + .get('/api/v1/city/state/41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('pk_municipio_id'); + res.body.result[0].should.have.property('fk_estado_id'); + res.body.result[0].should.have.property('nome'); + res.body.result[0].should.have.property('codigo_ibge'); + done(); + }) + }) +}); + +describe('request schools', () => { + it('should list a school by id', (done) => { + chai.request(server) + .get('/api/v1/school/185588') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('pk_escola_id'); + res.body.result[0].should.have.property('ano_censo'); + res.body.result[0].should.have.property('cod_entidade'); + res.body.result[0].should.have.property('nome_entidade'); + done(); + }); + }); + + it('should list all schools from a state', (done) => { + chai.request(server) + .get('/api/v1/school/state/41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('pk_escola_id'); + res.body.result[0].should.have.property('ano_censo'); + res.body.result[0].should.have.property('nome_entidade'); + done(); + }); + }); + + it('should list all schools from a city', (done) => { + chai.request(server) + .get('/api/v1/school/city/3287') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('pk_escola_id'); + res.body.result[0].should.have.property('ano_censo'); + res.body.result[0].should.have.property('nome_entidade'); + done(); + }) + }) +}); + +describe('test response', () => { + it('should list all regions in json', (done) => { + chai.request(server) + .get('/api/v1/region') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + done(); + }); + }); + + it('should list all regions in xml', (done) => { + chai.request(server) + .get('/api/v1/region?format=xml') + .end((err, res) => { + res.should.have.status(200); + res.should.be.xml; + done(); + }); + }); + + it('should list all regions in csv', (done) => { + chai.request(server) + .get('/api/v1/region?format=csv') + .end((err, res) => { + res.should.have.status(200); + done(); + }); + }); });