Jason R. Coombs avatar Jason R. Coombs committed 656d897

Added check for internet connectivity when running tests and skips tests that require it.

Comments (0)

Files changed (2)

tests/conftest.py

+from __future__ import print_function
+
+import urllib2
+import functools
+
+import pytest
+
+def throws_exception(call, exceptions=[Exception]):
+	"""
+	Invoke the function and return True if it raises any of the
+	exceptions provided. Otherwise, return False.
+	"""
+	try:
+		call()
+	except tuple(exceptions):
+		return True
+	except Exception, e:
+		pass
+		#print("Unexpected exception", e)
+	return False
+
+def pytest_namespace():
+	has_internet = pytest.mark.skipif('not pytest.config.has_internet')
+	return vars()
+
+def pytest_configure(config):
+	open_google = functools.partial(urllib2.urlopen, 'http://www.google.com')
+	config.has_internet = not throws_exception(open_google, [Exception, urllib2.URLError])

tests/unit/test_commands.py

 import uuid
 import datetime
 
+import pytest
+
 from pmxbot import pmxbot
 from pmxbot import botbase
 
 		configfile = os.path.join(path, 'testconf.yaml')
 		pmxbot.run(configFile=configfile, start=False)
 		pmxbot.botbase.logger.message("logged", "testrunner", "some text")
-		
+
 	@classmethod
 	def teardown_class(self):
 		del botbase.logger
 		path = os.path.dirname(os.path.abspath(__file__))
 		os.remove(os.path.join(path, 'pmxbot.sqlite'))
 
+	@pytest.has_internet
 	def test_google(self):
 		"""
 		Basic google search for "pmxbot". Result must contain a link.
 		"""
 		res = pmxbot.google(c, e, "#test", "testrunner", "pmxbot")
 		print res
-		assert "http" in res 
+		assert "http" in res
 
+	@pytest.has_internet
 	def test_googlecalc_simple(self):
 		"""
 		Basic google calculator command - 1+1 must include 2 in results
 		print res
 		assert "2" in res
 
+	@pytest.has_internet
 	def test_googlecalc_complicated(self):
 		"""
 		More complicated google calculator command - 40 gallons in liters must
 		print res
 		assert "151.4" in res
 
+	@pytest.has_internet
 	def test_googlecalc_supercomplicated(self):
 		"""
 		Supercomplicated google calculator command - 502 hogsheads per mile in litres per km
 		pickle.dump(res, open('bleh.cp', 'wb'))
 		assert "388.9641" in res and "74" in res
 
+	@pytest.has_internet
 	def test_googlecalc_currency_usd_gbp(self):
 		"""
 		Test that google calculator for a currency conversion: 1 USD in GBP
 		"""
 		res = pmxbot.googlecalc(c, e, "#test", "testrunner", "1 USD in GBP")
 		print res
-		assert re.match(r"""1 (?:US|U\.S\.) dollars? = \d\.\d+ British pounds?(?: sterling)?""", res) 
-		
+		assert re.match(r"""1 (?:US|U\.S\.) dollars? = \d\.\d+ British pounds?(?: sterling)?""", res)
 
+	@pytest.has_internet
 	def test_googlecalc_currency_czk_euro(self):
 		"""
 		Test that google calculator for a currency conversion: 12 CZK in euros
 		"""
 		res = pmxbot.googlecalc(c, e, "#test", "testrunner", "12 CZK in euros")
 		print res
-		assert re.match(r"""12 Czech(?: Republic)? [Kk]orun(?:a|y)s? = \d\.\d+ [Ee]uros?""", res) 
-		
+		assert re.match(r"""12 Czech(?: Republic)? [Kk]orun(?:a|y)s? = \d\.\d+ [Ee]uros?""", res)
+
+	@pytest.has_internet
 	def test_time_one(self):
 		"""
 		Check the time in Washington, DC. Must include something that looks like a time XX:XX(AM/PM)
 			assert re.match(r"""^[0-9]{1,2}:[0-9]{2}(?:am|pm) """, line)
 		assert i == 1
 
+	@pytest.has_internet
 	def test_time_three(self):
 		"""
 		Check the time in three cities. Must include something that looks like
 			i += 1
 			assert re.match(r"""^[0-9]{1,2}:[0-9]{2}(?:am|pm) """, line)
 		assert i == 3
-	
+
+	@pytest.has_internet
 	def test_time_all(self):
 		"""
 		Check the time in "all" cities. Must include something that looks like
 			i += 1
 			assert re.match(r"""^[0-9]{1,2}:[0-9]{2}(?:am|pm) """, line)
 		assert i == 4
-			
+
 	def test_weather_one(self):
 		"""
 		Check the weather in Washington, DC. Must include something that looks like a weather XX:XX(AM/PM)
 		for line in res:
 			print line
 			assert re.match(r""".+\. Currently: (?:-)?[0-9]{1,3}F/(?:-)?[0-9]{1,2}C, .+\.\W+[A-z]{3}: (?:-)?[0-9]{1,3}F/(?:-)?[0-9]{1,2}C, """, line)
-			
+
 	def test_boo(self):
 		"""
 		Test "boo foo"
 		assert res == "/me slaps %s around a bit with a large trout" % subject
 		post = pmxbot.util.karma.lookup(subject)
 		assert post == pre - 1
-		
+
 	def test_keelhaul(self):
 		"""
 		Test "keelhaul foo"
 		assert res == "/me straps %s to a dirty rope, tosses 'em overboard and pulls with great speed. Yarrr!" % subject
 		post = pmxbot.util.karma.lookup(subject)
 		assert post == pre - 1
-		
+
 	def test_motivate(self):
 		"""
 		Test that motivate actually works.
 		assert res == "you're doing good work, %s!" % subject
 		post = pmxbot.util.karma.lookup(subject)
 		assert post == pre + 1
-		
-		
+
+
 	def test_motivate_with_spaces(self):
 		"""
 		Test that motivate strips beginning and ending whitespace
 		assert res == """you're "doing" "good" "work", %s!""" % subject
 		post = pmxbot.util.karma.lookup(subject)
 		assert post == pre - 1
-		
+
 	def test_add_quote(self):
 		"""
 		Try adding a quote
 		cursor.execute("select count(*) from quotes where library = 'pmx' and quote = ?", (quote,))
 		numquotes = cursor.fetchone()[0]
 		assert numquotes == 1
-		
+
 		res = pmxbot.quote(c, e, "#test", "testrunner", id)
 		assert res == "(1/1): %s" % quote
-		
+
 	def test_roll(self):
 		"""
 		Roll a die, both with no arguments and with some numbers
 		res = int(pmxbot.roll(c, e, "#test", "testrunner", "").split()[-1])
 		assert res >= 0 and res <= 100
 		n = 6668
-		
+
 		res = int(pmxbot.roll(c, e, "#test", "testrunner", "%s" % n).split()[-1])
 		assert res >= 0 and res <= n
-		
+
+	@pytest.has_internet
 	def test_ticker_goog(self):
 		"""
 		Get the current stock price of Google.
-		
+
 		GOOG at 4:00pm (ET): 484.81 (1.5%)
 		"""
 		res = pmxbot.ticker(c, e, "#test", "testrunner", "goog")
 		print res
 		assert re.match(r"""^GOOG at \d{1,2}:\d{2}(?:am|pm) \([A-z]{1,3}\): \d{2,4}.\d{1,4} \(\-?\d{1,3}.\d%\)$""", res), res
-		
+
+	@pytest.has_internet
 	def test_ticker_yougov(self):
 		"""
 		Get the current stock price of YouGov.
-		
+
 		YOU.L at 10:37am (ET): 39.40 (0.4%)
 		"""
 		res = pmxbot.ticker(c, e, "#test", "testrunner", "you.l")
 		print res
 		assert re.match(r"""^YOU.L at \d{1,2}:\d{2}(?:am|pm) \([A-z]{1,3}\): \d{1,4}.\d{2,3} \(\-?\d{1,3}.\d%\)$""", res), res
-		
+
+	@pytest.has_internet
 	def test_ticker_nasdaq(self):
 		"""
 		Get the current stock price of the NASDAQ.
 		res = pmxbot.ticker(c, e, "#test", "testrunner", "^ixic")
 		print res
 		assert re.match(r"""^\^IXIC at \d{1,2}:\d{2}(?:am|pm) \([A-z]{1,3}\): \d{4,5}.\d{2} \(\-?\d{1,3}.\d%\)$""", res), res
-		
+
 	def test_pick_or(self):
 		"""
 		Test the pick command with a simple or expression
 		res = pmxbot.pick(c, e, "#test", "testrunner", "fire or acid")
 		assert logical_xor("fire" in res, "acid" in res)
 		assert " or " not in res
-		
+
 	def test_pick_or_intro(self):
 		"""
 		Test the pick command with an intro and a simple "or" expression
 		"""
 		res = pmxbot.pick(c, e, "#test", "testrunner", "fire, acid")
 		assert logical_xor("fire" in res, "acid" in res)
-		
+
 	def test_pick_comma_intro(self):
 		"""
 		Test the pick command with an intro followed by two options separted by commas
 		res = pmxbot.pick(c, e, "#test", "testrunner", "how would you like to die, pmxbot: fire, acid")
 		assert logical_xor("fire" in res, "acid" in res)
 		assert "die" not in res and "pmxbot" not in res
-		
+
 	def test_pick_comma_or_intro(self):
 		"""
 		Test the pick command with an intro followed by options with commands and ors
 		"""
 		res = pmxbot.lunch(c, e, "#test", "testrunner", "PA")
 		assert res in ["Pasta?", "Thaiphoon", "Pluto's", "Penninsula Creamery", "Kan Zeman"]
-	
+
 	def test_karma_check_self_blank(self):
 		"""
 		Determine your own, blank, karma.
 		id = str(uuid.uuid4())[:15]
 		res = pmxbot.karma(c, e, "#test", id, "")
 		assert re.match(r"^%s has 0 karmas$" % id, res)
-	
+
 	def test_karma_check_other_blank(self):
 		"""
 		Determine some else's blank/new karma.
 		id = str(uuid.uuid4())
 		res = pmxbot.karma(c, e, "#test", "testrunner", id)
 		assert re.match("^%s has 0 karmas$" % id, res)
-	
+
 	def test_karma_set_and_check(self):
 		"""
 		Take a new entity, give it some karma, check that it has more
 		res = pmxbot.karma(c, e, "#test", "testrunner", "%s--" %id)
 		res = pmxbot.karma(c, e, "#test", "testrunner", id)
 		assert re.match(r"^%s has 2 karmas$" % id, res)
-		
-		
+
+
 	def test_karma_set_and_check_with_space(self):
 		"""
 		Take a new entity that has a space in it's name, give it some karma, check that it has more
 		res = pmxbot.karma(c, e, "#test", "testrunner", "%s--" %id)
 		res = pmxbot.karma(c, e, "#test", "testrunner", id)
 		assert re.match(r"^%s has 2 karmas$" % id, res)
-	
+
 	def test_karma_randomchange(self):
 		"""
 		Take a new entity that has a space in it's name, give it some karma, check that it has more
 		res = pmxbot.calc(c, e, "#test", "testrunner", "2+2")
 		print res
 		assert res == "4"
-		
+
 	def test_calc_complex(self):
 		"""
 		Test the built-in python calculator with a more complicated formula
 		res = pmxbot.calc(c, e, "#test", "testrunner", "((((781**2)*5)/92835.3)+4)**0.5")
 		print res
 		assert res.startswith("6.070566")
-		
+
+	@pytest.has_internet
 	def test_define_keyboard(self):
 		"""
 		Test the wikipedia dictionary with the word keyboard.
 		assert isinstance(res, unicode)
 		assert res == "Wikipedia says: Enter (data) by means of a keyboard."
 
+	@pytest.has_internet
 	def test_define_irc(self):
 		"""
 		Test the wikipedia dictionary with the word IRC.
 		assert isinstance(res, unicode)
 		assert res == ("Wikipedia says: Internet Relay Chat (IRC) is a form of real-time Internet text messaging (chat) or synchronous conferencing. ....  ")
 
+	@pytest.has_internet
 	def test_urb_irc(self):
 		"""
 		Test the urban dictionary with the word IRC.
 		res = pmxbot.urbandefit(c, e, "#test", "testrunner", "irc")
 		assert res == "Urban Dictionary says IRC: Abbreviation for Internet Relay Chat. A multiplayer notepad."
 
+	@pytest.has_internet
 	def test_acronym_irc(self):
 		"""
 		Test acronym finder with the word IRC.
 		res = pmxbot.acit(c, e, "#test", "testrunner", "irc")
 		assert "Internet Relay Chat" in res
 		assert "|" in res
-		
+
 	def test_progress(self):
 		"""
 		Test the progress bar
 		print res
 		assert res != ""
 
+	@pytest.has_internet
 	def test_paste_newuser(self):
 		"""
 		Test the pastebin with an unknown user
 		print res
 		assert res == "hmm.. I didn't find a recent paste of yours, %s. Checkout http://a.libpa.st/" % person
 
+	@pytest.has_internet
 	def test_paste_real_user(self):
 		"""
 		Test the pastebin with a valid user with an existing paste
 		"""
-		person = 'vbSptH3ByfQQ6h' 
+		person = 'vbSptH3ByfQQ6h'
 		res = pmxbot.paste(c, e, '#test', person, '')
 		assert res == "http://a.libpa.st/40a4345a-4e4b-40d8-ad06-c0a22a26b282"
 
 		print res
 		assert res == "Quiet bitching is useless, foo'. Do something about it."
 
+	@pytest.has_internet
 	def test_translate(self):
 		"""
 		The translate function should be able to translate a simple string.
 		invalid_query = 'sp|en que no desea la nueva pregunta'
 		res = pmxbot.translate(c, e, '#test', 'testrunner', query)
 
+	@pytest.has_internet
 	def test_excuse(self):
 		pmxbot.excuse(c, e, '#test', 'testrunner', '')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.