Skip to content

Commit 91e518e

Browse files
committed
Replace network decorator with with_connectivity_check
Network decorator was ignoring test failures due to API changes in external services.
1 parent bf477dc commit 91e518e

File tree

1 file changed

+28
-109
lines changed

1 file changed

+28
-109
lines changed

pandas/util/testing.py

Lines changed: 28 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -971,106 +971,6 @@ def dec(f):
971971
_network_error_classes = IOError, httplib.HTTPException
972972

973973

974-
@optional_args
975-
def network(t, raise_on_error=_RAISE_NETWORK_ERROR_DEFAULT,
976-
error_classes=_network_error_classes, num_runs=2):
977-
"""
978-
Label a test as requiring network connection and skip test if it encounters a ``URLError``.
979-
980-
In some cases it is not possible to assume network presence (e.g. Debian
981-
build hosts).
982-
983-
You can pass an optional ``raise_on_error`` argument to the decorator, in
984-
which case it will always raise an error even if it's not a subclass of
985-
``error_classes``.
986-
987-
Parameters
988-
----------
989-
t : callable
990-
The test requiring network connectivity.
991-
raise_on_error : bool, optional
992-
If True, never catches errors.
993-
error_classes : tuple, optional
994-
error classes to ignore. If not in ``error_classes``, raises the error.
995-
defaults to URLError. Be careful about changing the error classes here,
996-
it may result in undefined behavior.
997-
num_runs : int, optional
998-
Number of times to run test. If fails on last try, will raise. Default
999-
is 2 runs.
1000-
1001-
Returns
1002-
-------
1003-
t : callable
1004-
The decorated test `t`.
1005-
1006-
Examples
1007-
--------
1008-
A test can be decorated as requiring network like this::
1009-
1010-
>>> from pandas.util.testing import network
1011-
>>> from pandas.io.common import urlopen
1012-
>>> import nose
1013-
>>> @network
1014-
... def test_network():
1015-
... with urlopen("rabbit://bonanza.com") as f:
1016-
... pass
1017-
...
1018-
>>> try:
1019-
... test_network()
1020-
... except nose.SkipTest:
1021-
... print("SKIPPING!")
1022-
...
1023-
SKIPPING!
1024-
1025-
Alternatively, you can use set ``raise_on_error`` in order to get
1026-
the error to bubble up, e.g.::
1027-
1028-
>>> @network(raise_on_error=True)
1029-
... def test_network():
1030-
... with urlopen("complaint://deadparrot.com") as f:
1031-
... pass
1032-
...
1033-
>>> test_network()
1034-
Traceback (most recent call last):
1035-
...
1036-
URLError: <urlopen error unknown url type: complaint>
1037-
1038-
And use ``nosetests -a '!network'`` to exclude running tests requiring
1039-
network connectivity. ``_RAISE_NETWORK_ERROR_DEFAULT`` in
1040-
``pandas/util/testing.py`` sets the default behavior (currently False).
1041-
"""
1042-
from nose import SkipTest
1043-
1044-
if num_runs < 1:
1045-
raise ValueError("Must set at least 1 run")
1046-
t.network = True
1047-
1048-
@wraps(t)
1049-
def network_wrapper(*args, **kwargs):
1050-
if raise_on_error:
1051-
return t(*args, **kwargs)
1052-
else:
1053-
runs = 0
1054-
1055-
for _ in range(num_runs):
1056-
try:
1057-
try:
1058-
return t(*args, **kwargs)
1059-
except error_classes as e:
1060-
raise SkipTest("Skipping test %s" % e)
1061-
except SkipTest:
1062-
raise
1063-
except Exception as e:
1064-
if runs < num_runs - 1:
1065-
print("Failed: %r" % e)
1066-
else:
1067-
raise
1068-
1069-
runs += 1
1070-
1071-
return network_wrapper
1072-
1073-
1074974
def can_connect(url, error_classes=_network_error_classes):
1075975
"""Try to connect to the given url. True if succeeds, False if IOError
1076976
raised
@@ -1096,10 +996,9 @@ def can_connect(url, error_classes=_network_error_classes):
1096996

1097997

1098998
@optional_args
1099-
def with_connectivity_check(t, url="http://www.google.com",
1100-
raise_on_error=_RAISE_NETWORK_ERROR_DEFAULT,
1101-
check_before_test=False,
1102-
error_classes=_network_error_classes):
999+
def network(t, url="http://www.google.com",
1000+
raise_on_error=_RAISE_NETWORK_ERROR_DEFAULT,
1001+
check_before_test=False, error_classes=_network_error_classes):
11031002
"""
11041003
Label a test as requiring network connection and, if an error is
11051004
encountered, only raise if it does not find a network connection.
@@ -1138,25 +1037,42 @@ def with_connectivity_check(t, url="http://www.google.com",
11381037
Example
11391038
-------
11401039
1141-
In this example, you see how it will raise the error if it can connect to
1142-
the url::
1143-
>>> @with_connectivity_check("http://www.yahoo.com")
1040+
Tests decorated with @network will fail if it's possible to make a network
1041+
connection to another URL (defaults to google.com)::
1042+
1043+
>>> from pandas.util.testing import network
1044+
>>> from pandas.io.common import urlopen
1045+
>>> @network
1046+
... def test_network():
1047+
... with urlopen("rabbit://bonanza.com"):
1048+
... pass
1049+
Traceback
1050+
...
1051+
URLError: <urlopen error unknown url type: rabit>
1052+
1053+
You can specify alternative URLs::
1054+
1055+
>>> @network("http://www.yahoo.com")
11441056
... def test_something_with_yahoo():
11451057
... raise IOError("Failure Message")
11461058
>>> test_something_with_yahoo()
11471059
Traceback (most recent call last):
11481060
...
11491061
IOError: Failure Message
11501062
1151-
I you set check_before_test, it will check the url first and not run the test on failure::
1152-
>>> @with_connectivity_check("failing://url.blaher", check_before_test=True)
1063+
If you set check_before_test, it will check the url first and not run the
1064+
test on failure::
1065+
1066+
>>> @network("failing://url.blaher", check_before_test=True)
11531067
... def test_something():
11541068
... print("I ran!")
11551069
... raise ValueError("Failure")
11561070
>>> test_something()
11571071
Traceback (most recent call last):
11581072
...
11591073
SkipTest
1074+
1075+
Errors not related to networking will always be raised.
11601076
"""
11611077
from nose import SkipTest
11621078
t.network = True
@@ -1178,6 +1094,9 @@ def wrapper(*args, **kwargs):
11781094
return wrapper
11791095

11801096

1097+
with_connectivity_check = network
1098+
1099+
11811100
class SimpleMock(object):
11821101

11831102
"""

0 commit comments

Comments
 (0)