From 657d4909e23f482eac3993269af74788dcd34d54 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Tue, 28 Apr 2015 19:52:29 +0200 Subject: [PATCH] aggiunti test per meteo --- scripts/meteo.coffee | 6 +++- test/meteo-test.coffee | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/meteo-test.coffee diff --git a/scripts/meteo.coffee b/scripts/meteo.coffee index cf530e7..00ca4ac 100644 --- a/scripts/meteo.coffee +++ b/scripts/meteo.coffee @@ -16,14 +16,18 @@ moment = require 'moment' module.exports = (robot) -> - robot.respond /che tempo(?: c'è| fa)?(?: a crema)?(?:\?)?/i, (res) -> + robot.respond /(?:che )?(?:tempo|meteo)(?: c'è| fa)?(?: a crema)?(?:\?)?/i, (res) -> url = 'http://api.openweathermap.org/data/2.5/weather?id=3177841&lang=it&units=metric' robot.http(url) .get() (err, r, body) -> + if err + return res.send 'Errore nel guardare il cielo.\n'+err try body = JSON.parse body catch e return res.send 'Errore nel guardare il cielo.' + unless body?.sys? and body?.main? and body?.weather?.push? + return res.send 'Errore nel guardare il cielo.\n'+JSON.stringify(body) alba = moment.unix(body.sys.sunrise).format('H:MM') tramonto = moment.unix(body.sys.sunset).format('H:MM') m = body.weather[0].description+' con '+body.main.humidity diff --git a/test/meteo-test.coffee b/test/meteo-test.coffee new file mode 100644 index 0000000..f0b9215 --- /dev/null +++ b/test/meteo-test.coffee @@ -0,0 +1,63 @@ +nock = require 'nock' +expect = require("chai").should() + +Asjon = require '../asjon-testing.coffee' +asjon = undefined + +payload = + sys: + sunrise: 12345 + sunset: 12345 + main: + humidity: 50 + temp: 20 + weather: [ description: 'ok' ] + + +describe 'modulo meteo', -> + before (done) -> + # Inizializzo robot + Asjon (assa) -> + asjon = assa + after asjon.after + afterEach asjon.clear + beforeEach nock.disableNetConnect + require('../scripts/meteo.coffee')(asjon.robot) + done() + + it 'dovrebbe rispondere quando interrogato', (done) -> + questions = [ + "asjon che tempo c'è a crema" + "asjon che tempo fa a crema?" + "asjon che tempo c'è" + "asjon che tempo fa?" + "asjon meteo a crema" + "asjon meteo" + ] + nock('http://api.openweathermap.org') + .get('/data/2.5/weather?id=3177841&lang=it&units=metric') + .times questions.length + .reply 200, payload + acc = 0 + asjon.receive (e,l) -> + acc++ + if acc is questions.length then done() + questions.map (q) -> asjon.send q + + it 'dovrebbe reagire correttamente a un errore nel payload', (done) -> + nock('http://api.openweathermap.org') + .get('/data/2.5/weather?id=3177841&lang=it&units=metric') + .reply 500, 'very error' + asjon.receive (e,l) -> + l.join().should.match /^Errore nel guardare il cielo.$/g + done() + asjon.send 'asjon meteo' + + it 'dovrebbe parsare correttamente i dati', (done) -> + nock('http://api.openweathermap.org') + .get('/data/2.5/weather?id=3177841&lang=it&units=metric') + .reply 200, payload + asjon.receive (e,l) -> + l.join().should.match /^Meteo per Crema: /g + done() + asjon.send 'asjon meteo'