Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaell-lycan committed Feb 11, 2015
1 parent e5990f0 commit 5780625
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 74 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
console.log('Magic happens on port: ' + app.get('port'));
});

module.exports = app;

})();
70 changes: 70 additions & 0 deletions lib/Helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
(function (){
var cheerio = require('cheerio');

var Helper = {
json: [],
data: null,
$ : null,
dams : {}
};

Helper.getDamName = function (str) {
var name = str.split(/[./]+/)[1];
return Helper.dams[name];
};

Helper.buildJSON = function (cssSelector, key) {
Helper.data.find(cssSelector).each(function (i, elem) {
Helper.json[i].data.push({
key : key,
value : Helper.$(elem).next().text()
});
});
};

Helper.parserHTML = function (html) {
Helper.$ = cheerio.load(html);

Helper.$('#tabDados').filter(function () {
Helper.data = Helper.$(this);

// Fetch each images on context
Helper.data.find('img').each(function (i, elem) {
Helper.json[i] = {
name : Helper.getDamName(elem.attribs.src),
data : []
};
});

// Fetch each td with content "volume armazenado"
Helper.buildJSON('td:contains(volume armazenado)', 'volume armazenado');

// Fetch each td with content "pluviometria do dia"
Helper.buildJSON('td:contains(pluviometria do dia)', 'pluviometria do dia');

// Fetch each td with content "pluviometria acumulada no mês"
Helper.buildJSON('td:contains(pluviometria acumulada no mês)', 'pluviometria acumulada no mês');

// Fetch each td with content "média histórica do mês"
Helper.buildJSON('td:contains(média histórica do mês)', 'média histórica do mês');
});

return Helper.json;
};

Helper.buildData = function(date, token) {
date = date.split('-');

return {
"__VIEWSTATE": token.state,
"__EVENTVALIDATION": token.validation,
"Imagebutton1.x": 8,
"Imagebutton1.y": 6,
"cmbDia": parseInt(date[2], 10),
"cmbMes": parseInt(date[1], 10),
"cmbAno": parseInt(date[0], 10)
};
};

module.exports = Helper;
})();
85 changes: 13 additions & 72 deletions lib/Sabesp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,28 @@
'use strict';
var request = require('request'),
cheerio = require('cheerio'),
Promise = require('promise');
Promise = require('promise'),
Helper = require('./Helper');

// Our Sabesp url
var token;

var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx';

var dams = {
"sistemaCantareira": "Cantareira",
"sistemaAltoTiete": "Alto Tietê",
"sistemaGuarapiranga": "Guarapiranga",
"sistemaCotia": "Alto Cotia",
"sistemaRioGrande": "Rio Grande",
"sistemaRioClaro": "Rio Claro"
Helper.dams = {
'sistemaCantareira': 'Cantareira',
'sistemaAltoTiete': 'Alto Tietê',
'sistemaGuarapiranga': 'Guarapiranga',
'sistemaCotia': 'Alto Cotia',
'sistemaRioGrande': 'Rio Grande',
'sistemaRioClaro': 'Rio Claro'
};

function _getDamName(str) {
var name = str.split(/[./]+/)[1];
return dams[name];
}

function _buildJSON(json, data, $, cssSelector, key) {
data.find(cssSelector).each(function (i, elem) {
json[i].data.push({
key : key,
value : $(elem).next().text()
});
});
}

function _parserHTML(html) {
var json = [],
$ = cheerio.load(html);

$('#tabDados').filter(function () {
var data = $(this);

// Fetch each images on context
data.find('img').each(function (i, elem) {
json[i] = {
name : _getDamName(elem.attribs.src),
data : []
};
});

// Fetch each td with content "volume armazenado"
_buildJSON(json, data, $, 'td:contains(volume armazenado)', 'volume armazenado');

// Fetch each td with content "pluviometria do dia"
_buildJSON(json, data, $, 'td:contains(pluviometria do dia)', 'pluviometria do dia');

// Fetch each td with content "pluviometria acumulada no mês"
_buildJSON(json, data, $, 'td:contains(pluviometria acumulada no mês)', 'pluviometria acumulada no mês');

// Fetch each td with content "média histórica do mês"
_buildJSON(json, data, $, 'td:contains(média histórica do mês)', 'média histórica do mês');
});

return json;
}

function _buildData(date, token) {
date = date.split('-');

return {
"__VIEWSTATE": token.state,
"__EVENTVALIDATION": token.validation,
"Imagebutton1.x": 8,
"Imagebutton1.y": 6,
"cmbDia": parseInt(date[2], 10),
"cmbMes": parseInt(date[1], 10),
"cmbAno": parseInt(date[0], 10)
};
}

var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx';

var Sabesp = {};
Sabesp.fetch = function(date) {
if (date) {
return new Promise(function(resolve, reject) {
var data = _buildData(date, token);
var data = Helper.buildData(date, token);

request({
'url': url,
Expand All @@ -97,7 +38,7 @@
'jar': true,
'form': data,
}, function(error, response, html){
resolve(_parserHTML(html));
resolve(Helper.parserHTML(html));
}
);

Expand All @@ -111,7 +52,7 @@
if (error) {
reject(error);
} else {
resolve(_parserHTML(html));
resolve(Helper.parserHTML(html));
}
});
};
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"scripts": {
"start": "node index.js",
"test": "./node_modules/jshint/bin/jshint *.js lib/*.js"
"pre-test": "./node_modules/jshint/bin/jshint *.js lib/*.js",
"test": "./node_modules/istanbul/lib/cli.js cover --hook-run-in-context ./node_modules/mocha/bin/mocha tests/*"
},
"engines": {
"node": "0.10.x"
Expand All @@ -24,8 +25,12 @@
"request": "^2.53.0"
},
"devDependencies": {
"chai": "^1.10.0",
"istanbul": "^0.3.5",
"jshint": "^2.6.0",
"nodemon": "^1.3.6"
"mocha": "^2.1.0",
"nodemon": "^1.3.6",
"supertest": "^0.15.0"
},
"license": "MIT"
}
35 changes: 35 additions & 0 deletions tests/Helper_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var expect = require('chai').expect,
Helper = require('../lib/Helper');

describe('Helper', function () {
it('Translate image name from water system name', function(done) {
var odd = Helper.dams;
Helper.dams = {
'dummySystem' : 'Dummy'
};

expect(Helper.getDamName('imagens/dummySystem.jpg')).to.be.equal('Dummy');

Helper.dams = odd;
done();
});

it("Expect the data be the same of mock", function(done) {
var token = {
state : 'xpto',
validation : 'lorem'
},
mock = {
"__VIEWSTATE": token.state,
"__EVENTVALIDATION": token.validation,
"Imagebutton1.x": 8,
"Imagebutton1.y": 6,
"cmbDia": 1,
"cmbMes": 1,
"cmbAno": 2003
};

expect(Helper.buildData('2003-01-01', token)).to.eql(mock);
done();
});
});
18 changes: 18 additions & 0 deletions tests/Sabesp_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var request = require('supertest'),
app = require('../index'),
expect = require('chai').expect;

describe('Sabesp', function () {
it('returns today data', function(done) {
request(app)
.get('/')
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res) {
expect(res.body.length).to.be.equal(6);
expect(res.body[0].name).to.be.equal('Cantareira');

done();
});
});
});

0 comments on commit 5780625

Please sign in to comment.