Skip to content

Commit 36dc31c

Browse files
committed
Updated tests for to comply with use of context-manager in urllib requests
1 parent 98b8f8c commit 36dc31c

1 file changed

Lines changed: 120 additions & 1 deletion

File tree

hydra_agent/tests/test_querying.py

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55

66

77
class TestHandleData(unittest.TestCase):
8+
"""
9+
TestCase for HandleData Class
10+
"""
811
def setUp(self):
12+
"""Setting up HandleData object"""
913
self.handle_data = HandleData()
1014

1115
@patch('hydra_agent.querying_mechanism.json.loads', spec_set=True)
1216
@patch('hydra_agent.querying_mechanism.urllib.request.urlopen', spec_set=True)
1317
def test_load_data(self, request_mock, json_mock):
18+
"""
19+
Tests load_data method case without exceptions
20+
21+
Args:
22+
request_mock : MagicMock object from patching out urllib.request.urlopen function
23+
json_mock: MagicMock object from patching out json.loads function
24+
"""
25+
1426
# url passed to load_data as an argument
1527
url = "TestURL"
1628

1729
# mock to return byte code for response.read()
1830
intermediate_mock = MagicMock()
19-
intermediate_mock.read.return_value = b"TestResponse"
31+
intermediate_mock.__enter__.return_value.read.return_value = b"TestResponse"
2032

2133
# making sure that request doesn't raise an exception
2234
request_mock.return_value = intermediate_mock
@@ -31,6 +43,14 @@ def test_load_data(self, request_mock, json_mock):
3143
@patch('hydra_agent.querying_mechanism.logger')
3244
@patch('hydra_agent.querying_mechanism.urllib.request.urlopen', spec_set=True)
3345
def test_load_data_with_errors(self, request_mock, logger_mock, json_mock):
46+
"""
47+
Tests load_data method case with exception
48+
49+
Args:
50+
request_mock : MagicMock object from patching out urllib.request.urlopen function
51+
logger_mock : MagicMock object from patching out the logger object
52+
json_mock : MagicMock object from patching out the json.loads function
53+
"""
3454
# url passed to load_data as an argument
3555
url = "TestURL"
3656
# request raises an exception (ValueError can be replaced by URLError or HTTPError)
@@ -44,11 +64,23 @@ def test_load_data_with_errors(self, request_mock, logger_mock, json_mock):
4464

4565

4666
class TestEndpointQuery(unittest.TestCase):
67+
"""
68+
TestCase for EndpointQuery Class
69+
"""
70+
4771
@patch('hydra_agent.querying_mechanism.RedisProxy', autospec=True)
4872
def setUp(self, redis_mock):
73+
"""
74+
Setting up EndpointQuery object
75+
Args:
76+
redis_mock : MagicMock object from patching out RedisProxy
77+
"""
4978
self.endpoint_query = EndpointQuery()
5079

5180
def test_get_allEndpoints(self):
81+
"""
82+
Tests get_allEndpoints method
83+
"""
5284
# query to be passed as a param to get_allEndpoints
5385
query = "get endpoints"
5486

@@ -64,6 +96,9 @@ def test_get_allEndpoints(self):
6496
connection_mock.execute_command.assert_has_calls(calls)
6597

6698
def test_get_classEndpoints(self):
99+
"""
100+
Tests get_classEndpoints method
101+
"""
67102
# query to be passed as a param to get_classEndpoints
68103
query = "get classEndpoints"
69104

@@ -78,6 +113,9 @@ def test_get_classEndpoints(self):
78113
connection_mock.execute_command.assert_has_calls(calls)
79114

80115
def test_get_collectionEndpoints(self):
116+
"""
117+
Tests get_collectionEndpoints method
118+
"""
81119
# query to be passed as a param to get_collectionEndpoints
82120
query = "get collectionEndpoints"
83121

@@ -94,15 +132,29 @@ def test_get_collectionEndpoints(self):
94132

95133

96134
class TestCollectionmembersQuery(unittest.TestCase):
135+
"""
136+
TestCase for CollectionmembersQuery class
137+
"""
97138
@patch('hydra_agent.querying_mechanism.CollectionEndpoints', autospec=True)
98139
@patch('hydra_agent.querying_mechanism.RedisProxy', autospec=True)
99140
def setUp(self, redis_mock, collections_mock):
141+
"""
142+
Setting up CollectionmembersQuery object
143+
144+
Args:
145+
redis_mock : MagicMock object from patching out RedisProxy
146+
collections_mock: MagicMock object from patching out CollectionEndpoints
147+
"""
148+
100149
api_doc = MagicMock()
101150
url = MagicMock()
102151
graph = MagicMock()
103152
self.cmq = CollectionmembersQuery(api_doc, url, graph)
104153

105154
def test_data_from_server(self):
155+
"""
156+
Tests data_from_server method
157+
"""
106158
# endpoint param to be passed to data_from_server
107159
endpoint = "TestEndpoint"
108160

@@ -123,11 +175,20 @@ def test_data_from_server(self):
123175
connection_mock.execute_command.assert_has_calls(calls)
124176

125177
def smembers_mock_func(self, inp):
178+
"""
179+
SideEffect for testing get_members
180+
181+
Args:
182+
inp : Input to mock object's side effect equivalent to smembers method
183+
"""
126184
if inp == "fs:endpoints":
127185
return [b"TestEndpoint"]
128186

129187
def test_get_members_if(self):
130188
# query param to be passed to get_members
189+
"""
190+
Tests get_members method when if condition passes
191+
"""
131192
query = "TestEndpoint members"
132193
connection_mock = self.cmq.connection
133194

@@ -147,10 +208,19 @@ def test_get_members_if(self):
147208
connection_mock.sadd.assert_not_called()
148209

149210
def smembers_mock_func_else(self, inp):
211+
"""
212+
SideEffect for mock object used in testing get_members_method
213+
214+
Args:
215+
inp: Input to Mock object's side effect equivalent to smembers method
216+
"""
150217
if inp == "fs:endpoints":
151218
return [b"TestEndpoint"]
152219

153220
def test_get_members_else(self):
221+
"""
222+
Tests get_members method when if condition fails
223+
"""
154224
# query param to be passed to get_members
155225
query = "TestEndpoint members"
156226
connection_mock = self.cmq.connection
@@ -167,11 +237,23 @@ def test_get_members_else(self):
167237

168238

169239
class TestPropertiesQuery(unittest.TestCase):
240+
"""
241+
TestCase for PropertiesQuery Class
242+
"""
170243
@patch('hydra_agent.querying_mechanism.RedisProxy')
171244
def setUp(self, redis_mock):
245+
"""
246+
Setting up PropertiesQuery object
247+
248+
Args:
249+
redis_mock : MagicMock object from patching out RedisProxy
250+
"""
172251
self.properties_query = PropertiesQuery()
173252

174253
def test_get_classes_properties(self):
254+
"""
255+
Tests get_classes_properties method
256+
"""
175257
# query param for get_classes_properties
176258
query = "classClassEndpoint properties"
177259
connection_mock = self.properties_query.connection
@@ -183,6 +265,9 @@ def test_get_classes_properties(self):
183265
connection_mock.execute_command.assert_called_with('GRAPH.QUERY', 'apidoc', 'MATCH ( p:classes ) WHERE (p.type="ClassEndpoint") RETURN p.properties')
184266

185267
def test_get_collection_properties(self):
268+
"""
269+
Tests get_collection_properties method
270+
"""
186271
# query param for get_collection_properties
187272
query = "collectionEndpoint properties"
188273
connection_mock = self.properties_query.connection
@@ -194,6 +279,9 @@ def test_get_collection_properties(self):
194279
connection_mock.execute_command.assert_called_with('GRAPH.QUERY', 'apidoc', 'MATCH ( p:collection ) WHERE (p.type="collectionEndpoint") RETURN p.properties')
195280

196281
def test_members_properties(self):
282+
"""
283+
Tests get_members_properties method
284+
"""
197285
# query param for get_members_properties
198286
query = "testMember properties"
199287

@@ -206,6 +294,9 @@ def test_members_properties(self):
206294
connection_mock.execute_command.assert_called_with('GRAPH.QUERY', 'apidoc', 'MATCH ( p:testMember ) RETURN p.id,p.properties')
207295

208296
def test_object_properties(self):
297+
"""
298+
Tests get_object_property method
299+
"""
209300
# query for get_object_property
210301
query = "object/api/TestCollection/2 properties"
211302

@@ -219,15 +310,28 @@ def test_object_properties(self):
219310

220311

221312
class TestClassPropertiesValue(unittest.TestCase):
313+
"""
314+
TestCase for ClassPropertiesValue
315+
"""
222316
@patch('hydra_agent.querying_mechanism.ClassEndpoints', autospec=True)
223317
@patch('hydra_agent.querying_mechanism.RedisProxy')
224318
def setUp(self, redis_mock, classEndpoint_mock):
319+
"""
320+
Setting up ClassPropertiesValue object
321+
322+
Args:
323+
redis_mock : MagicMock object from patching out RedisProxy
324+
classEndpoint_mock : MagicMock object from patching out ClassEndpoint class
325+
"""
225326
api_doc = MagicMock()
226327
url = MagicMock()
227328
graph = MagicMock()
228329
self.cpv = ClassPropertiesValue(api_doc, url, graph)
229330

230331
def test_data_from_server(self):
332+
"""
333+
Tests data_from_server method
334+
"""
231335
# endpoint param for data_from_server
232336
endpoint = "TestEndpoint"
233337
clas_mock = self.cpv.clas
@@ -245,10 +349,19 @@ def test_data_from_server(self):
245349
RETURN p.property_value""")
246350

247351
def smembers_mock_func(self, inp):
352+
"""
353+
SideEffect for mock object used in testing get_property_value
354+
355+
Args:
356+
inp : Input to the SideEffect equivalent to smembers method
357+
"""
248358
if inp == "fs:endpoints":
249359
return [b"TestClass"]
250360

251361
def test_get_property_value(self):
362+
"""
363+
Tests get_property_value when if condition passes
364+
"""
252365
# query param for get_property_value
253366
query = "classTestClass property_value"
254367
connection_mock = self.cpv.connection
@@ -269,9 +382,15 @@ def test_get_property_value(self):
269382
connection_mock.sadd.assert_not_called()
270383

271384
def smembers_mock_func_else(self, inp):
385+
"""
386+
SideEffect for mock object used in testing get_property_value
387+
"""
272388
return []
273389

274390
def test_get_property_value_else(self):
391+
"""
392+
Tests get_property_value method when the if condition fails
393+
"""
275394
# query param for get_property_value
276395
query = "classTestClass property_value"
277396
connection_mock = self.cpv.connection

0 commit comments

Comments
 (0)