tfw: tests and retry on first failure

master
mutantmonkey 2012-05-31 00:39:40 -07:00
parent 183609fdd1
commit 546b3113d5
2 changed files with 53 additions and 2 deletions

47
modules/test/test_tfw.py Normal file
View File

@ -0,0 +1,47 @@
"""
test_tfw.py - tests for the fucking weather module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
# add current working directory to path
import sys
sys.path.append('.')
import re
import unittest
from mock import MagicMock, Mock
from modules.tfw import tfw
class TestTfw(unittest.TestCase):
def setUp(self):
self.phenny = MagicMock()
#def test_badloc(self):
# input = Mock(group=lambda x: 'tu3jgoajgoahghqog')
# tfw(self.phenny, input)
#
# self.phenny.say.assert_called_once_with("UNKNOWN FUCKING LOCATION. Try another?")
def test_celsius(self):
input = Mock(group=lambda x: '24060')
tfw(self.phenny, input, celsius=True)
out = self.phenny.say.call_args[0][0]
m = re.match('^\d+°C‽ .* \- .*$', out, flags=re.UNICODE)
self.assertTrue(m)
def test_fahrenheit(self):
input = Mock(group=lambda x: '24060')
tfw(self.phenny, input, fahrenheit=True)
out = self.phenny.say.call_args[0][0]
m = re.match('^\d+°F‽ .* \- .*$', out, flags=re.UNICODE)
self.assertTrue(m)
def test_mev(self):
input = Mock(group=lambda x: '24060')
tfw(self.phenny, input)
out = self.phenny.say.call_args[0][0]
m = re.match('^[\d\.]+ meV‽ .* \- .*$', out, flags=re.UNICODE)
self.assertTrue(m)

View File

@ -27,8 +27,12 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
try:
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param))
except (HTTPError, IOError):
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return
# the fucking weather is fucking unstable, try again
try:
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param))
except (HTTPError, IOError):
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return
doc = lxml.html.fromstring(req)