Skip to content

Commit fb2c00e

Browse files
committed
refactor more tests
1 parent 4e244d9 commit fb2c00e

File tree

3 files changed

+65
-87
lines changed

3 files changed

+65
-87
lines changed

tests/base_test.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,23 @@ class BaseTestCase(unittest.TestCase):
110110

111111
_http_get_handlers = {}
112112
_http_post_handlers = {}
113+
_http_delete_handlers = {}
113114

114115
@classmethod
115116
def httpPostHandlers(cls, *args, **kwargs):
116-
return BaseTestCase._http_post_handlers[args[0]]
117+
try:
118+
handler = BaseTestCase._http_post_handlers[args[0]]
119+
except KeyError:
120+
raise Exception(
121+
"Handler not found for POST {}.\nDid you register a handler with BaseTestCase.registerHttpGetHandler?".format(
122+
args[0]
123+
)
124+
)
125+
126+
if isinstance(handler, Exception):
127+
raise handler
128+
else:
129+
return handler
117130

118131
@classmethod
119132
def httpGetHandlers(cls, *args, **kwargs):
@@ -131,6 +144,22 @@ def httpGetHandlers(cls, *args, **kwargs):
131144
else:
132145
return handler
133146

147+
@classmethod
148+
def httpDeleteHandlers(cls, *args, **kwargs):
149+
try:
150+
handler = BaseTestCase._http_delete_handlers[args[0]]
151+
except KeyError:
152+
raise Exception(
153+
"Handler not found for DELETE {}.\nDid you register a handler with BaseTestCase.registerHttpDeleteHandler?".format(
154+
args[0]
155+
)
156+
)
157+
158+
if isinstance(handler, Exception):
159+
raise handler
160+
else:
161+
return handler
162+
134163
@classmethod
135164
def registerHttpPostHandler(cls, url, response):
136165
BaseTestCase._http_post_handlers[url] = response
@@ -139,6 +168,10 @@ def registerHttpPostHandler(cls, url, response):
139168
def registerHttpGetHandler(cls, url, response):
140169
BaseTestCase._http_get_handlers[url] = response
141170

171+
@classmethod
172+
def registerHttpDeleteHandler(cls, url, response):
173+
BaseTestCase._http_delete_handlers[url] = response
174+
142175
@classmethod
143176
def setUpClass(cls):
144177
# Register the login handler

tests/catalog_test.py

+18-66
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@
151151
),
152152
)
153153

154+
BaseTestCase.registerHttpGetHandler(
155+
url="https://127.0.0.1:8080/api/v1/catalog/102",
156+
response=MockResponse(
157+
json_data={"garbage"}, status_code=200, headers=dict(),
158+
),
159+
)
160+
154161
BaseTestCase.registerHttpGetHandler(
155162
url="https://127.0.0.1:8080/api/v1/catalog",
156163
response=MockResponse(
@@ -591,66 +598,8 @@ def test_list_with_query_and_text_output(self, mock_post, mock_get):
591598

592599

593600
class TestCLIGet(BaseTestCase):
594-
def mocked_requests_get(*args, **kwargs):
595-
if args[0] == "https://127.0.0.1:8080/api/v1/catalog/100":
596-
return MockResponse(
597-
json_data={
598-
"_links": {
599-
"self": {"href": "/api/v1/catalog/100"},
600-
"feed": [
601-
{
602-
"href": (
603-
"https://s3.amazonaws.com/bluedata-catalog/"
604-
"bundles/catalog/external/docker/EPIC-5.0/"
605-
"feeds/feed.json"
606-
),
607-
"name": (
608-
"BlueData EPIC-5.0 catalog feed for docker"
609-
),
610-
}
611-
],
612-
},
613-
"id": "/api/v1/catalog/100",
614-
"distro_id": "bluedata/spark240juphub7xssl",
615-
"label": {
616-
"name": "Spark240",
617-
"description": ("The description"),
618-
},
619-
"version": "2.8",
620-
"timestamp": 0,
621-
"isdebug": False,
622-
"osclass": ["centos"],
623-
"logo": {
624-
"checksum": "1471eb59356066ed4a06130566764ea6",
625-
"url": (
626-
"http://10.1.0.53/catalog/logos/"
627-
"bluedata-spark240juphub7xssl-2.8"
628-
),
629-
},
630-
"documentation": {
631-
"checksum": "52f53f1b2845463b9e370d17fb80bea6",
632-
"mimetype": "text/markdown",
633-
"file": (
634-
"/opt/bluedata/catalog/documentation/"
635-
"bluedata-spark240juphub7xssl-2.8"
636-
),
637-
},
638-
"state": "initialized",
639-
"state_info": "",
640-
},
641-
status_code=200,
642-
headers=dict(),
643-
)
644-
if args[0] == "https://127.0.0.1:8080/api/v1/catalog/101":
645-
raise APIItemNotFoundException(
646-
message="catalog not found with id: " + "/api/v1/catalog/101",
647-
request_method="get",
648-
request_url=args[0],
649-
)
650-
raise RuntimeError("Unhandle GET request: " + args[0])
651-
652601
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
653-
@patch("requests.get", side_effect=mocked_requests_get)
602+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
654603
def test_get_output_is_valid_yaml(self, mock_post, mock_get):
655604

656605
self.maxDiff = None
@@ -665,7 +614,7 @@ def test_get_output_is_valid_yaml(self, mock_post, mock_get):
665614
self.fail("Output should be valid yaml")
666615

667616
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
668-
@patch("requests.get", side_effect=mocked_requests_get)
617+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
669618
def test_get_yaml_output_is_valid(self, mock_post, mock_get):
670619

671620
self.maxDiff = None
@@ -695,7 +644,7 @@ def test_get_yaml_output_is_valid(self, mock_post, mock_get):
695644
id: /api/v1/catalog/100
696645
isdebug: false
697646
label:
698-
description: The description
647+
description: Spark240 multirole with Jupyter Notebook, Jupyterhub with SSL and gateway node
699648
name: Spark240
700649
logo:
701650
checksum: 1471eb59356066ed4a06130566764ea6
@@ -715,7 +664,7 @@ def test_get_yaml_output_is_valid(self, mock_post, mock_get):
715664
)
716665

717666
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
718-
@patch("requests.get", side_effect=mocked_requests_get)
667+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
719668
def test_get_json_output(self, mock_post, mock_get):
720669

721670
self.maxDiff = None
@@ -746,7 +695,10 @@ def test_get_json_output(self, mock_post, mock_get):
746695
"distro_id": "bluedata/spark240juphub7xssl",
747696
"label": {
748697
"name": "Spark240",
749-
"description": "The description",
698+
"description": (
699+
"Spark240 multirole with Jupyter Notebook, Jupyterhub"
700+
" with SSL and gateway node"
701+
),
750702
},
751703
"state": "initialized",
752704
"version": "2.8",
@@ -763,7 +715,7 @@ def test_get_json_output(self, mock_post, mock_get):
763715
)
764716

765717
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
766-
@patch("requests.get", side_effect=mocked_requests_get)
718+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
767719
def test_get_output_with_invalid_catalog_id(self, mock_post, mock_get):
768720

769721
with self.assertRaises(SystemExit) as cm:
@@ -794,12 +746,12 @@ def mocked_requests_garbage_data(*args, **kwargs):
794746
raise RuntimeError("Unhandle GET request: " + args[0])
795747

796748
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
797-
@patch("requests.get", side_effect=mocked_requests_garbage_data)
749+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
798750
def test_get_output_with_unknown_exception(self, mock_post, mock_get):
799751

800752
with self.assertRaises(SystemExit) as cm:
801753
hpecp = self.cli.CLI()
802-
hpecp.catalog.get("/api/v1/catalog/101")
754+
hpecp.catalog.get("/api/v1/catalog/102")
803755

804756
self.assertEqual(cm.exception.code, 1)
805757

tests/user_test.py

+13-20
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@
112112
),
113113
)
114114

115+
BaseTestCase.registerHttpPostHandler(
116+
url="https://127.0.0.1:8080/api/v1/user/999",
117+
response=MockResponse(
118+
text_data="Not found.",
119+
json_data={},
120+
status_code=404,
121+
raise_for_status_flag=True,
122+
headers={},
123+
),
124+
)
125+
115126

116127
class TestUsers(TestCase):
117128
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
@@ -147,27 +158,9 @@ def test_get_users(self, mock_get, mock_post):
147158

148159

149160
class TestDeleteUser(TestCase):
150-
151-
# TODO: refactor me to BaseTestCase http handler
152-
# pylint: disable=no-method-argument
153-
def mocked_requests_delete(*args, **kwargs):
154-
if args[0] == "https://127.0.0.1:8080/api/v1/user/999":
155-
return MockResponse(
156-
text_data="Not found.",
157-
json_data={},
158-
status_code=404,
159-
raise_for_status_flag=True,
160-
headers={},
161-
)
162-
if args[0] == "https://127.0.0.1:8080/api/v1/user/123":
163-
return MockResponse(json_data={}, status_code=200, headers={},)
164-
raise RuntimeError("Unhandle GET request: " + args[0])
165-
166-
# delete() does a get() request to check the worker has 'purpose':'proxy'
167-
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
168-
@patch("requests.delete", side_effect=mocked_requests_delete)
161+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
169162
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
170-
def test_delete_user(self, mock_get, mock_post, mock_delete):
163+
def test_delete_user(self, mock_post, mock_delete):
171164
with self.assertRaisesRegexp(
172165
AssertionError, "'id' must be provided and must be a str",
173166
):

0 commit comments

Comments
 (0)