From 546b3113d5395a929e888e5e046f704ca5d3d003 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Thu, 31 May 2012 00:39:40 -0700 Subject: [PATCH] tfw: tests and retry on first failure --- modules/test/test_tfw.py | 47 ++++++++++++++++++++++++++++++++++++++++ modules/tfw.py | 8 +++++-- 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 modules/test/test_tfw.py diff --git a/modules/test/test_tfw.py b/modules/test/test_tfw.py new file mode 100644 index 0000000..0973bc6 --- /dev/null +++ b/modules/test/test_tfw.py @@ -0,0 +1,47 @@ +""" +test_tfw.py - tests for the fucking weather module +author: mutantmonkey +""" + +# 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) diff --git a/modules/tfw.py b/modules/tfw.py index b33d8d2..68b5fce 100644 --- a/modules/tfw.py +++ b/modules/tfw.py @@ -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)