Skip to content

Commit 6b20255

Browse files
committed
add tests for BaseAPI non-json error handling
Signed-off-by: Marques Johansson <[email protected]>
1 parent cb78bcc commit 6b20255

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

packet/baseapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from packet import __version__
99

1010

11-
class Error(Exception): # pragma: no cover
11+
class Error(Exception):
1212
"""Base exception class for this module"""
1313

1414
def __init__(self, msg, cause=None):
@@ -96,7 +96,7 @@ def call_api(self, method, type="GET", params=None): # noqa
9696
resp = requests.delete(url, headers=headers)
9797
elif type == "PATCH":
9898
resp = requests.patch(url, headers=headers, data=json.dumps(params))
99-
else: # pragma: no cover
99+
else:
100100
raise Error(
101101
"method type not recognized as one of GET, POST, DELETE or PATCH: %s"
102102
% type

test/test_baseapi.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ def test_cause(self):
2020
self.assertIn(error.cause, cause)
2121

2222

23+
class BaseAPITest(unittest.TestCase):
24+
def setUp(self):
25+
self.auth_token = "fake_auth_token"
26+
self.consumer_token = "fake_consumer_token"
27+
self.end_point = "api.packet.net"
28+
self._user_agent_prefix = "fake_user_agent"
29+
30+
def test_init_all(self):
31+
base = packet.baseapi.BaseAPI(
32+
self.auth_token, self.consumer_token, self._user_agent_prefix
33+
)
34+
self.assertEqual(base.end_point, self.end_point)
35+
self.assertEqual(base.auth_token, self.auth_token)
36+
self.assertEqual(base.consumer_token, self.consumer_token)
37+
self.assertEqual(base._user_agent_prefix, self._user_agent_prefix)
38+
39+
def test_call_api_with_end_point(self):
40+
base = packet.baseapi.BaseAPI(
41+
self.auth_token, self.consumer_token, self._user_agent_prefix
42+
)
43+
44+
if int(sys.version[0]) == 3:
45+
self.assertRaisesRegex(
46+
packet.Error,
47+
"method type not recognized as one of",
48+
base.call_api,
49+
"fake_path",
50+
"bad_method",
51+
)
52+
53+
2354
class ResponseErrorTest(unittest.TestCase):
2455
def setUp(self):
2556
self.resp500 = obj({"status_code": 500})
@@ -31,6 +62,10 @@ def test_init_empty(self):
3162
error = packet.ResponseError(self.resp500, None, None)
3263
self.assertIn("empty", str(error))
3364

65+
def test_init_string(self):
66+
error = packet.ResponseError(self.resp500, "whoops", None)
67+
self.assertIn("whoops", str(error))
68+
3469
def test_init_error(self):
3570
error = packet.ResponseError(self.resp500, self.errBoom, self.exception)
3671
self.assertIn("Error 500: boom", str(error))

0 commit comments

Comments
 (0)