diff --git a/src/libs/db/query_exec.js b/src/libs/db/query_exec.js index 70456d8ef73868108d3db58b044508700884ebe9..1a2f7387574bc2f886550ef39f86f221c6a98e49 100644 --- a/src/libs/db/query_exec.js +++ b/src/libs/db/query_exec.js @@ -28,12 +28,14 @@ function execSqlQuery(sqlQuery, sqlQueryParams = []) { resolve(dbResult.data); }).catch((queryError) => { log.error(`SQL query execution error: ${queryError.message}`); + log.error(`SQL query: ${sqlQuery} with params: ${sqlQueryParams}`); // release resources allocated for the prepared statement dbQuery.release(); reject(new Error(queryError.message)); }); }).catch((prepError) => { log.error(`SQL prepared statement error: ${prepError.message}`); + log.error(`SQL query: ${sqlQuery} with params: ${sqlQueryParams}`); reject(new Error(prepError.message)); }); }); diff --git a/src/libs/routes/city.js b/src/libs/routes/city.js index 733da6f341cc37a7b6b792b8f5383076eae75067..d963287567363544f145ecef506dcf934f669767 100644 --- a/src/libs/routes/city.js +++ b/src/libs/routes/city.js @@ -12,27 +12,27 @@ const response = require(`${libs}/middlewares/response`); // Return all cities cityApp.get('/', (req, res, next) => { - req.sql.from('municipios'); + req.sql.from('municipio'); next(); }, query, response('city')); // Return a specific city by it's id cityApp.get('/:id', (req, res, next) => { - req.sql.from('municipios') - .where('pk_municipio_id = ?', parseInt(req.params.id, 10)); + req.sql.from('municipio') + .where('pk_cod_ibge = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); // Return a specific city by it's IBGE code cityApp.get('/ibge/:id', (req, res, next) => { - req.sql.from('municipios') - .where('codigo_ibge = ?', req.params.id); + req.sql.from('municipio') + .where('pk_cod_ibge = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); // Return all the cities from a specific state cityApp.get('/state/:id', (req, res, next) => { - req.sql.from('municipios') + req.sql.from('municipio') .where('fk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index bd0a0d74084dda48cdd0ecd5075e403857693f0e..3e878e7f23b2d038cf217147f19c28d34a246e4e 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -20,9 +20,9 @@ const parseParams = require(`${libs}/middlewares/parseParams`); // Complete range of the enrollments dataset. // Returns a tuple of start and ending years of the complete enrollments dataset. enrollmentApp.get('/year_range', (req, res, next) => { - req.sql.from('turmas') - .field('MIN(turmas.ano_censo)', 'start_year') - .field('MAX(turmas.ano_censo)', 'end_year'); + req.sql.from('turma') + .field('MIN(turma.ano_censo)', 'start_year') + .field('MAX(turma.ano_censo)', 'end_year'); next(); }, query, response('range')); @@ -38,7 +38,7 @@ enrollmentApp.get('/education_level', (req, res, next) => { // Returns all adm dependencies enrollmentApp.get('/adm_dependency', (req, res, next) => { - req.sql.from('dependencia_adms') + req.sql.from('dependencia_adm') .field('pk_dependencia_adm_id', 'id') .field('nome', 'name'); @@ -71,7 +71,7 @@ enrollmentApp.use('/', parseParams('filter', [ // Do the joins if(typeof req.filter.adm_dependency !== 'undefined' || typeof req.dims.adm_dependency !== 'undefined') { - req.sql.join('dependencia_adm', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); + req.sql.join('dependencia_adm', null, 'fk_dependencia_adm_id=dependencia_adm.pk_dependencia_adm_id'); } if(typeof req.filter.education_level !== 'undefined' @@ -95,11 +95,11 @@ enrollmentApp.use('/', parseParams('filter', [ } if(typeof req.dims.school !== 'undefined') { - req.sql.join('escola', null, 'fk_escola_id=escola.pk_escola_id'); + req.sql.join('escola', null, 'turma.cod_entidade=escola.cod_entidade'); } if(typeof req.dims.location !== 'undefined') { - req.sql.join('localizacao', null, 'id_localizacao=localizacao.pk_localizacao_id') + req.sql.join('localizacao', null, 'turma.id_localizacao=localizacao.pk_localizacao_id') } // Dimensions (add fields) @@ -129,9 +129,9 @@ enrollmentApp.use('/', parseParams('filter', [ } if(typeof req.dims.school !== 'undefined') { - req.sql.field('escola.nome_entidade', 'school_name') - .group('escola.nome_entidade') - .order('escola.nome_entidade'); + req.sql.field('escola.cod_entidade', 'school_name') + .group('escola.cod_entidade') + .order('escola.cod_entidade'); } if(typeof req.dims.adm_dependency !== 'undefined') { diff --git a/src/libs/routes/region.js b/src/libs/routes/region.js index 773ad347959049dd2137d96abb255a3bc809a2d4..0a8b65f860655e71e7aaf4f1b6211a37d481199c 100644 --- a/src/libs/routes/region.js +++ b/src/libs/routes/region.js @@ -12,13 +12,13 @@ const response = require(`${libs}/middlewares/response`); // Get all regions regionApp.get('/', (req, res, next) => { - req.sql.from('regioes'); + req.sql.from('regiao'); next(); }, query, response('region')); // Get a region by it's id regionApp.get('/:id', (req, res, next) => { - req.sql.from('regioes') + req.sql.from('regiao') .where('pk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('region')); diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index 6c92430b425d3b9fde8c682216c8f6a10f07b75f..4ae980e2a1715f4170c3881c76d2af49d7ecb8d3 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -17,43 +17,40 @@ const response = require(`${libs}/middlewares/response`); * Pense na cena do elevador de driver mas o elevador é uma bomba de fusão e demora mais que uma luta do DBz */ // schoolApp.get('/', (req, res, next) => { -// req.sql = squel.select().from('escolas') -// .field('pk_escola_id') -// .field('nome_entidade', 'name') +// req.sql = squel.select().from('escola') +// .field('cod_entidade') // .field('ano_censo', 'year') -// .field('fk_cod_estado') -// .field('fk_cod_municipio'); +// .field('fk_estado_id') +// .field('fk_municipio_id'); // next(); // }, query, response('school')); // Get a school by it's id schoolApp.get('/:id', (req, res, next) => { - req.sql.from('escolas') - .where('pk_escola_id = ?', parseInt(req.params.id, 10)); + req.sql.from('escola') + .where('escola.cod_entidade = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); // Get all schools from a state schoolApp.get('/state/:id', (req, res, next) => { - req.sql.from('escolas') - .field('pk_escola_id') - .field('nome_entidade') + req.sql.from('escola') + .field('escola.cod_entidade') .field('ano_censo') - .field('fk_cod_estado') - .field('fk_cod_municipio') - .where('fk_cod_estado = ?', parseInt(req.params.id, 10)); + .field('fk_estado_id') + .field('fk_municipio_id') + .where('fk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); // Get all schools from a city schoolApp.get('/city/:id', (req, res, next) => { - req.sql.from('escolas') - .field('pk_escola_id') - .field('nome_entidade') + req.sql.from('escola') + .field('escola.cod_entidade') .field('ano_censo') - .field('fk_cod_estado') - .field('fk_cod_municipio') - .where('fk_cod_municipio = ?', parseInt(req.params.id, 10)); + .field('fk_estado_id') + .field('fk_municipio_id') + .where('fk_municipio_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); diff --git a/src/libs/routes/state.js b/src/libs/routes/state.js index 75e1c0b7835901804ea78e459b1fe0050ec03631..8567ec05f7448fcbf3ebe00fefbc9d5e6e48de27 100644 --- a/src/libs/routes/state.js +++ b/src/libs/routes/state.js @@ -12,20 +12,20 @@ const response = require(`${libs}/middlewares/response`); // Get all states stateApp.get('/', (req, res, next) => { - req.sql.from('estados'); + req.sql.from('estado'); next(); }, query, response('state')); // Get a state stateApp.get('/:id', (req, res, next) => { - req.sql.from('estados') + req.sql.from('estado') .where('pk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state')); // Get all states from a region stateApp.get('/region/:id', (req, res, next) => { - req.sql.from('estados') + req.sql.from('estado') .where('fk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state')); diff --git a/src/test/test.js b/src/test/test.js index ddf9a79efe92d705d6774b713a03eadbf28b9a9f..8b55179e1556030ce9a90644bfbdac57624caef8 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -159,7 +159,7 @@ describe('request enrollments', () => { it('should list enrollments with valid dimensions and filters', (done) => { chai.request(server) - .get('/api/v1/enrollment?dims=region,state,education_level,school&filter=min_year:2013,max_year:2014,city:3287') + .get('/api/v1/enrollment?dims=region,state,education_level,school&filter=min_year:2013,max_year:2014,city:4106902') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -177,7 +177,7 @@ describe('request enrollments', () => { it('should list enrollments using all dimensions and filters', (done) => { chai.request(server) - .get('/api/v1/enrollment?dims=region,state,city,education_level,school,adm_dependency,location&filter=min_year:2013,max_year:2014,city:3287,adm_dependency:3,location:1,education_level:99') + .get('/api/v1/enrollment?dims=region,state,city,education_level,school,adm_dependency,location&filter=min_year:2013,max_year:2014,city:4106902,adm_dependency:3,location:1,education_level:99') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -286,42 +286,39 @@ describe('request cities', () => { 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('pk_cod_ibge'); res.body.result[0].should.have.property('nome'); - res.body.result[0].should.have.property('codigo_ibge'); + res.body.result[0].should.have.property('fk_estado_id'); done(); }); }); it('should list a city by id', (done) => { chai.request(server) - .get('/api/v1/city/1') + .get('/api/v1/city/4106902') .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('pk_cod_ibge'); 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(); }); }); it('should list a city by codigo_ibge', (done) => { chai.request(server) - .get('/api/v1/city/ibge/1200013') + .get('/api/v1/city/ibge/4106902') .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('pk_cod_ibge'); 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(); }); }); @@ -334,10 +331,9 @@ describe('request cities', () => { 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('pk_cod_ibge'); 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(); }) }) @@ -346,16 +342,14 @@ describe('request cities', () => { describe('request schools', () => { it('should list a school by id', (done) => { chai.request(server) - .get('/api/v1/school/185588') + .get('/api/v1/school/41000021') .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(); }); }); @@ -368,24 +362,22 @@ describe('request schools', () => { 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('cod_entidade'); 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') + .get('/api/v1/school/city/4106902') .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('cod_entidade'); res.body.result[0].should.have.property('ano_censo'); - res.body.result[0].should.have.property('nome_entidade'); done(); }) }) @@ -805,6 +797,9 @@ describe('Saves a user', () => { beforeEach(() => { User.remove({}, (err) => { + if(err) { + console.log('Error while purging: ' + err); + } console.log('Test collection purged'); }); });