fix tests on meteo module

This commit is contained in:
Enrico Fasoli 2015-08-27 23:08:36 +00:00
parent 8b707d0045
commit 8ddce46efc
2 changed files with 32 additions and 16 deletions

View File

@ -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

View File

@ -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'