Use TestCase.assertRaises() (#782)

Now that we no longer support Python 2.6, we can use the assertRaises()
method of TestCase as a context manager, tiding up a large amount of
technical debt.
This commit is contained in:
Steve Kowalik
2018-08-15 14:54:57 +08:00
committed by Wan Liuyang
parent b91dee8df6
commit cf05688359
4 changed files with 55 additions and 92 deletions
+45 -66
View File
@@ -39,87 +39,66 @@ import Framework
atMostPython2 = sys.hexversion < 0x03000000
class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we do not use self.assertRaises with only one argument
class Exceptions(Framework.TestCase):
def testInvalidInput(self):
raised = False
try:
with self.assertRaises(github.GithubException) as raisedexp:
self.g.get_user().create_key("Bad key", "xxx")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 422)
self.assertEqual(
exception.data,
{
"errors": [
{
"code": "custom",
"field": "key",
"message": "key is invalid. It must begin with 'ssh-rsa' or 'ssh-dss'. Check that you're copying the public half of the key",
"resource": "PublicKey"
}
],
"message": "Validation Failed"
}
)
self.assertTrue(raised)
self.assertEqual(raisedexp.exception.status, 422)
self.assertEqual(
raisedexp.exception.data, {
"errors": [
{
"code": "custom",
"field": "key",
"message": "key is invalid. It must begin with 'ssh-rsa' or 'ssh-dss'. Check that you're copying the public half of the key",
"resource": "PublicKey"
}
],
"message": "Validation Failed"
}
)
def testNonJsonDataReturnedByGithub(self):
# Replay data was forged according to https://github.com/jacquev6/PyGithub/pull/182
raised = False
try:
with self.assertRaises(github.GithubException) as raisedexp:
self.g.get_user("jacquev6")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 503)
self.assertEqual(
exception.data,
{
"data": "<html><body><h1>503 Service Unavailable</h1>No server is available to handle this request.</body></html>",
}
)
self.assertTrue(raised)
self.assertEqual(raisedexp.exception.status, 503)
self.assertEqual(
raisedexp.exception.data,
{
"data": "<html><body><h1>503 Service Unavailable</h1>No server is available to handle this request.</body></html>",
}
)
def testUnknownObject(self):
raised = False
try:
with self.assertRaises(github.GithubException) as raisedexp:
self.g.get_user().get_repo("Xxx")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertEqual(exception.data, {"message": "Not Found"})
if atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
self.assertEqual(raisedexp.exception.status, 404)
self.assertEqual(raisedexp.exception.data, {"message": "Not Found"})
if atMostPython2:
self.assertEqual(str(raisedexp.exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(raisedexp.exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
def testUnknownUser(self):
raised = False
try:
with self.assertRaises(github.GithubException) as raisedexp:
self.g.get_user("ThisUserShouldReallyNotExist")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertEqual(exception.data, {"message": "Not Found"})
if atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
self.assertEqual(raisedexp.exception.status, 404)
self.assertEqual(raisedexp.exception.data, {"message": "Not Found"})
if atMostPython2:
self.assertEqual(str(raisedexp.exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(raisedexp.exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
def testBadAuthentication(self):
raised = False
try:
with self.assertRaises(github.GithubException) as raisedexp:
github.Github("BadUser", "BadPassword").get_user().login
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 401)
self.assertEqual(exception.data, {"message": "Bad credentials"})
if atMostPython2:
self.assertEqual(str(exception), "401 {u'message': u'Bad credentials'}")
else:
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
self.assertEqual(raisedexp.exception.status, 401)
self.assertEqual(raisedexp.exception.data, {"message": "Bad credentials"})
if atMostPython2:
self.assertEqual(str(raisedexp.exception), "401 {u'message': u'Bad credentials'}")
else:
self.assertEqual(str(raisedexp.exception), "401 {'message': 'Bad credentials'}") # pragma no cover (Covered with Python 3)
def testExceptionPickling(self):
pickle.loads(pickle.dumps(github.GithubException('foo', 'bar')))