Skip to content

Instantly share code, notes, and snippets.

@audiodude
Created December 10, 2022 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audiodude/d9f417f572f5438108f268ab28ca3859 to your computer and use it in GitHub Desktop.
Save audiodude/d9f417f572f5438108f268ab28ca3859 to your computer and use it in GitHub Desktop.
import requests
from wp1.exceptions import Wp1RetryableSelectionError, Wp1FatalSelectionError
<SNIP>
try:
r = requests.post('https://query.wikidata.org/sparql',
headers={'User-Agent': WP1_USER_AGENT},
data={
'query': modified_query,
'format': 'json',
})
except requests.Timeout as e:
raise Wp1RetryableSelectionError(
'The request to Wikidata timed out') from e
except requests.RequestException as e:
raise Wp1RetryableSelectionError('Could not connect to Wikidata') from e
try:
r.raise_for_status()
except requests.HTTPError as e:
raise Wp1FataSelectionError(
f'Wikidata sent back a non-200 status code: {r.status_code}') from e
======================================================================
ERROR: test_build_server_error (wp1.selection.models.sparql_test.SparqlBuilderTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 117, in build
r.raise_for_status()
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1092, in __call__
return self._mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1096, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1151, in _execute_mock_call
raise effect
requests.exceptions.HTTPError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1336, in patched
return func(*newargs, **newkeywargs)
File "/home/tmoney/code/wp1/wp1/selection/models/sparql_test.py", line 147, in test_build_server_error
actual = self.builder.build('text/tab-separated-values',
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 118, in build
except requests.HTTPError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
======================================================================
ERROR: test_build_server_request_exception (wp1.selection.models.sparql_test.SparqlBuilderTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 117, in build
r.raise_for_status()
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1092, in __call__
return self._mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1096, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1151, in _execute_mock_call
raise effect
requests.exceptions.RequestException
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1336, in patched
return func(*newargs, **newkeywargs)
File "/home/tmoney/code/wp1/wp1/selection/models/sparql_test.py", line 171, in test_build_server_request_exception
actual = self.builder.build('text/tab-separated-values',
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 118, in build
except requests.HTTPError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
======================================================================
ERROR: test_build_server_timeout (wp1.selection.models.sparql_test.SparqlBuilderTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 117, in build
r.raise_for_status()
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1092, in __call__
return self._mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1096, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1151, in _execute_mock_call
raise effect
requests.exceptions.Timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tmoney/.asdf/installs/python/3.9.15/lib/python3.9/unittest/mock.py", line 1336, in patched
return func(*newargs, **newkeywargs)
File "/home/tmoney/code/wp1/wp1/selection/models/sparql_test.py", line 159, in test_build_server_timeout
actual = self.builder.build('text/tab-separated-values',
File "/home/tmoney/code/wp1/wp1/selection/models/sparql.py", line 118, in build
except requests.HTTPError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
----------------------------------------------------------------------
Ran 14 tests in 10.488s
FAILED (errors=3)
class SparqlBuilderTest(BaseWpOneDbTest):
<SNIP>
@patch('wp1.selection.models.sparql.requests')
def test_build_server_error(self, mock_requests):
response = MagicMock()
mock_requests.post.return_value = response
response.raise_for_status.side_effect = requests.HTTPError()
with self.assertRaises(Wp1FatalSelectionError):
actual = self.builder.build('text/tab-separated-values',
project='en.wikipedia.org',
query=self.cats_uk_us_after_1950,
queryVariable='cat')
@patch('wp1.selection.models.sparql.requests')
def test_build_server_timeout(self, mock_requests):
response = MagicMock()
mock_requests.post.return_value = response
response.raise_for_status.side_effect = requests.Timeout
with self.assertRaises(Wp1FatalSelectionError):
actual = self.builder.build('text/tab-separated-values',
project='en.wikipedia.org',
query=self.cats_uk_us_after_1950,
queryVariable='cat')
@patch('wp1.selection.models.sparql.requests')
def test_build_server_request_exception(self, mock_requests):
response = MagicMock()
mock_requests.post.return_value = response
response.raise_for_status.side_effect = requests.RequestException
with self.assertRaises(Wp1FatalSelectionError):
actual = self.builder.build('text/tab-separated-values',
project='en.wikipedia.org',
query=self.cats_uk_us_after_1950,
queryVariable='cat')
@audiodude
Copy link
Author

Same thing happens using requests.exceptions.HTTPError in both test and code.

@audiodude
Copy link
Author

Okay the problem is that since I'm patching wp1.selection.models.sparql.requests in the test, requests.exceptions just returns MagicMock and is not actually imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment