From 8ddce46efc735089304cab2bd36044f0be51fe79 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Thu, 27 Aug 2015 23:08:36 +0000 Subject: [PATCH] fix tests on meteo module --- scripts/meteo.coffee | 14 ++++++++++---- test/meteo-test.coffee | 34 ++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/scripts/meteo.coffee b/scripts/meteo.coffee index 7605ebd..7023605 100644 --- a/scripts/meteo.coffee +++ b/scripts/meteo.coffee @@ -26,15 +26,21 @@ module.exports = (robot) -> robot.respond /(?:(?:d(?:a|i)mmi il )?meteo(?: per (.+))?|che tempo (?:fa|c'è)(?: (?:a|in) (.+))?)\??$/i, (res) -> find_location = (ip, callback) -> robot.http(url1 + ip).get() (err, r, body) -> - city = (JSON.parse body).city - return res.send if err or not city? - console.log 'found ' + city + try + city = JSON.parse(body).city + catch e + # Fallback + callback 'Crema' + return + if err or !city? or city is undefined + # Fallback + callback 'Crema' + return callback city send_weather = (city) -> robot.http(url2 + city).get() (err, r, body) -> return res.send "#{err2}\n #{err}" if err - try body = JSON.parse body catch e diff --git a/test/meteo-test.coffee b/test/meteo-test.coffee index f0b9215..50b5641 100644 --- a/test/meteo-test.coffee +++ b/test/meteo-test.coffee @@ -4,7 +4,11 @@ expect = require("chai").should() Asjon = require '../asjon-testing.coffee' asjon = undefined +loc_payload = + city: "Milano" + payload = + name: loc_payload.city sys: sunrise: 12345 sunset: 12345 @@ -13,7 +17,6 @@ payload = temp: 20 weather: [ description: 'ok' ] - describe 'modulo meteo', -> before (done) -> # Inizializzo robot @@ -29,15 +32,17 @@ describe 'modulo meteo', -> questions = [ "asjon che tempo c'è a crema" "asjon che tempo fa a crema?" - "asjon che tempo c'è" + "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') + .get('/data/2.5/weather?lang=it&units=metric&q='+loc_payload.city) .times questions.length .reply 200, payload + nock('http://ip-api.com') + .get('/json') + .times 2 + .reply 200, loc_payload acc = 0 asjon.receive (e,l) -> acc++ @@ -46,18 +51,23 @@ describe 'modulo meteo', -> 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') + .get('/data/2.5/weather?lang=it&units=metric&q='+loc_payload.city) .reply 500, 'very error' asjon.receive (e,l) -> - l.join().should.match /^Errore nel guardare il cielo.$/g + l.join().should.match /^errore nel guardare il cielo/g done() asjon.send 'asjon meteo' - it 'dovrebbe parsare correttamente i dati', (done) -> + it 'dovrebbe trovare la posizione correttamente', (done) -> nock('http://api.openweathermap.org') - .get('/data/2.5/weather?id=3177841&lang=it&units=metric') + .get('/data/2.5/weather?lang=it&units=metric&q='+loc_payload.city) .reply 200, payload - asjon.receive (e,l) -> - l.join().should.match /^Meteo per Crema: /g - done() + nock('http://ip-api.com') + .get('/json/') + .times 1 + .reply 200, loc_payload + asjon.receive (e,l) -> + l.join().split(' ')[2].should.equal loc_payload.city+":\n" + l.join().should.match /^meteo per /g + done() asjon.send 'asjon meteo'