From 291103800e66bcc85c23feff4c43067500ab39cd Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Thu, 23 Mar 2017 11:08:01 -0300
Subject: [PATCH] :green_heart: Fix SQL and tests for /spatial

---
 src/libs/routes/spatial.js | 10 ++++----
 src/test/spatial.js        | 47 +++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/src/libs/routes/spatial.js b/src/libs/routes/spatial.js
index 58473b0e..2cc6e839 100644
--- a/src/libs/routes/spatial.js
+++ b/src/libs/routes/spatial.js
@@ -303,8 +303,8 @@ spatialApp.get('/educational', rqf.parse(), rqf.build(), (req, res, next) => {
         .field('COALESCE(COUNT(matricula.id), 0)', 'total')
         .field('matricula.ano_censo', 'census_year')
         .field('dependencia_adm.nome', 'adm_dependency_name')
-        .from('matricula')
         .from('dependencia_adm')
+        .from('matricula')
         .where('matricula.dependencia_adm_id=dependencia_adm.id')
         .where('matricula.tipo <= 3')
         .where(`matricula.ano_censo IN (${schoolClassYearQry})`)
@@ -317,8 +317,8 @@ spatialApp.get('/educational', rqf.parse(), rqf.build(), (req, res, next) => {
         .field('COALESCE(COUNT(matricula.id), 0)', 'total')
         .field('matricula.ano_censo', 'census_year')
         .field('etapa_ensino.desc_etapa', 'school_level_name')
-        .from('matricula')
         .from('etapa_ensino')
+        .from('matricula')
         .where('matricula.etapa_ensino_id=etapa_ensino.id')
         .where('matricula.tipo <= 3')
         .where(`matricula.ano_censo IN (${schoolClassYearQry})`)
@@ -341,18 +341,18 @@ spatialApp.get('/educational', rqf.parse(), rqf.build(), (req, res, next) => {
     });
 }, response('spatial'));
 
-spatialApp.get('/educational/school_level', (req, res, next) => {
+spatialApp.get('/educational/school_level', rqf.parse(), rqf.build(), (req, res, next) => {
     const enrollmentsPerSchoolLevelYearQry = squel.select()
         .field('MAX(matricula.ano_censo)', 'census_year')
         .from('matricula');
 
-    const enrollmentsPerSchoolLevelQry = squel.select()
+    const enrollmentsPerSchoolLevelQry = req.sql.clone()
         .field('COALESCE(COUNT(matricula.id), 0)', 'total')
         .field('matricula.ano_censo', 'census_year')
         .field('matricula.serie_ano_id', 'school_year')
         .field('etapa_ensino.desc_etapa', 'school_level')
-        .from('matricula')
         .from('etapa_ensino')
+        .from('matricula')
         .where(`matricula.ano_censo IN (${enrollmentsPerSchoolLevelYearQry.toString()})`)
         .where('matricula.etapa_ensino_id = etapa_ensino.id')
         .where('matricula.tipo <= 3')
diff --git a/src/test/spatial.js b/src/test/spatial.js
index b0e53083..b4422e79 100644
--- a/src/test/spatial.js
+++ b/src/test/spatial.js
@@ -24,7 +24,7 @@ const server = require(`${libs}/app`);
 
 chai.use(chaiHttp);
 
-const testTimeout = 5000;
+const testTimeout = 60000;
 
 describe('test spatial', () => {
     it('should return the expected response format for sociodemographic data for the whole country', (done) => {
@@ -209,11 +209,10 @@ describe('test spatial', () => {
                 res.body.result.should.have.property('enrollment_per_school_level');
                 res.body.result.enrollment_per_school_level.should.be.a('array');
                 // test response attributes for school
-                res.body.result.school.should.a('array');
                 res.body.result.school.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -221,7 +220,7 @@ describe('test spatial', () => {
                 res.body.result.school_per_location.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -238,7 +237,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('adm_dependency');
+                    row.should.have.property('adm_dependency_name');
                 });
                 // test response attributes for enrollment_per_school_level
                 res.body.result.enrollment_per_school_level.forEach((row) => {
@@ -246,7 +245,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('school_level');
+                    row.should.have.property('school_level_name');
                 });
                 done();
             });
@@ -254,7 +253,7 @@ describe('test spatial', () => {
 
     it('should return the expected response format for educational data for a country region', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/region/1')
+            .get('/api/v1/spatial/educational?filter=region:1')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
@@ -278,7 +277,7 @@ describe('test spatial', () => {
                 res.body.result.school.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -286,7 +285,7 @@ describe('test spatial', () => {
                 res.body.result.school_per_location.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -303,7 +302,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('adm_dependency');
+                    row.should.have.property('adm_dependency_name');
                 });
                 // test response attributes for enrollment_per_school_level
                 res.body.result.enrollment_per_school_level.forEach((row) => {
@@ -311,7 +310,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('school_level');
+                    row.should.have.property('school_level_name');
                 });
                 done();
             });
@@ -319,7 +318,7 @@ describe('test spatial', () => {
 
     it('should return the expected response format for educational data for a country state', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/state/42')
+            .get('/api/v1/spatial/educational?filter=state:42')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
@@ -343,7 +342,7 @@ describe('test spatial', () => {
                 res.body.result.school.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -351,7 +350,7 @@ describe('test spatial', () => {
                 res.body.result.school_per_location.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -368,7 +367,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('adm_dependency');
+                    row.should.have.property('adm_dependency_name');
                 });
                 // test response attributes for enrollment_per_school_level
                 res.body.result.enrollment_per_school_level.forEach((row) => {
@@ -376,7 +375,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('school_level');
+                    row.should.have.property('school_level_name');
                 });
                 done();
             });
@@ -384,7 +383,7 @@ describe('test spatial', () => {
 
     it('should return the expected response format for educational data for a country city', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/city/4106902')
+            .get('/api/v1/spatial/educational?filter=city:4106902')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
@@ -408,7 +407,7 @@ describe('test spatial', () => {
                 res.body.result.school.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -416,7 +415,7 @@ describe('test spatial', () => {
                 res.body.result.school_per_location.forEach((row) => {
                     row.should.be.a('object');
                     row.should.have.property('name');
-                    row.should.have.property('location');
+                    row.should.have.property('location_name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
                 });
@@ -433,7 +432,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('adm_dependency');
+                    row.should.have.property('adm_dependency_name');
                 });
                 // test response attributes for enrollment_per_school_level
                 res.body.result.enrollment_per_school_level.forEach((row) => {
@@ -441,7 +440,7 @@ describe('test spatial', () => {
                     row.should.have.property('name');
                     row.should.have.property('total');
                     row.should.have.property('census_year');
-                    row.should.have.property('school_level');
+                    row.should.have.property('school_level_name');
                 });
                 done();
             });
@@ -478,7 +477,7 @@ describe('test spatial', () => {
 
     it('should return the correct format of enrollments per school level for a region', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/school_level/region/1')
+            .get('/api/v1/spatial/educational/school_level?filter=region:1')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
@@ -507,7 +506,7 @@ describe('test spatial', () => {
 
     it('should return the correct format of enrollments per school level for a state', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/school_level/state/42')
+            .get('/api/v1/spatial/educational/school_level?filter=state:42')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
@@ -536,7 +535,7 @@ describe('test spatial', () => {
 
     it('should return the correct format of enrollments per school level for a city', (done) => {
         chai.request(server)
-            .get('/api/v1/spatial/educational/school_level/state/4106902')
+            .get('/api/v1/spatial/educational/school_level?filter=city:4106902')
             .end((err, res) => {
                 res.should.have.status(200);
                 // test response format
-- 
GitLab