Skip to content

Commit eb8ccce

Browse files
committed
add unittests for WCS, fix error messages and docstrings, release 2.2.0
1 parent 0297292 commit eb8ccce

File tree

6 files changed

+435
-114
lines changed

6 files changed

+435
-114
lines changed

test/connection/test_connection.py

+91-110
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def test__get_request_header(self, mock_refresh_authentication):
175175

176176
@patch("weaviate.connect.connection.requests")
177177
@patch("weaviate.connect.connection.get_epoch_time")
178-
def test__refresh_authentication(self, mock_get_epoch_time, mock_requests):
178+
@patch("weaviate.connect.connection.Connection._set_bearer")
179+
def test__refresh_authentication(self, mock_set_bearer, mock_get_epoch_time, mock_requests):
179180
"""
180181
Test the `_refresh_authentication` method.
181182
"""
@@ -190,10 +191,64 @@ def test__refresh_authentication(self, mock_get_epoch_time, mock_requests):
190191
self.check_connection_attributes(connection) # after the `_refresh_authentication` call
191192
mock_get_epoch_time.assert_called()
192193
mock_requests.get.assert_not_called()
194+
mock_set_bearer.assert_not_called()
193195

194196
# error messages
195197
data_error_message = "Cannot connect to weaviate."
196198
data_status_code_error_message = "Cannot authenticate http status not ok."
199+
200+
mock_get_epoch_time.return_value = 200
201+
get_kwargs = {
202+
'headers': {"content-type": "application/json"},
203+
'timeout': (30, 45)
204+
}
205+
# test the expired connection
206+
## requests.get exception (get data)
207+
connection = Connection('test_url', auth_client_secret=None)
208+
mock_requests.get.configure_mock(side_effect=RequestException('Test!'))
209+
with self.assertRaises(AuthenticationFailedException) as error:
210+
connection._refresh_authentication()
211+
212+
check_error_message(self, error, data_error_message)
213+
mock_requests.get.assert_called_with("test_url/v1/.well-known/openid-configuration", **get_kwargs)
214+
mock_set_bearer.assert_not_called()
215+
216+
## bad status_code (get data)
217+
mock_get_epoch_time.reset_mock() # reset mock.called
218+
### reset 'requests' mock because it is called in the `__init__`
219+
mock_requests.get.reset_mock(side_effect=True, return_value=True)
220+
connection = Connection('test_url', auth_client_secret=None)
221+
mock_requests.get.return_value = Mock(status_code=404)
222+
with self.assertRaises(AuthenticationFailedException) as error:
223+
connection._refresh_authentication()
224+
check_error_message(self, error, data_status_code_error_message)
225+
mock_requests.get.assert_called_with("test_url/v1/.well-known/openid-configuration", **get_kwargs)
226+
mock_set_bearer.assert_not_called()
227+
228+
# valid call
229+
mock_get_epoch_time.reset_mock() # reset mock.called
230+
## reset 'requests' mock because it is called in the `__init__`
231+
mock_requests.get.reset_mock(side_effect=True, return_value=True)
232+
connection = Connection('test_url', auth_client_secret=None)
233+
mock_requests.get.return_value = Mock(**{'status_code': 200, 'json.return_value': {'clientId': 'Test1!', 'href': 'Test2!'}})
234+
connection._refresh_authentication()
235+
mock_requests.get.assert_called_with("test_url/v1/.well-known/openid-configuration", **get_kwargs)
236+
mock_set_bearer.assert_called_with(client_id='Test1!', href='Test2!')
237+
238+
@patch("weaviate.connect.connection.requests")
239+
@patch("weaviate.connect.connection.Connection._refresh_authentication")
240+
def test__set_bearer(self, mock_refresh_authentication, mock_requests):
241+
"""
242+
Test the `_set_bearer` method.
243+
"""
244+
245+
get_kwargs = {
246+
'headers': {"content-type": "application/json"},
247+
'timeout': (30, 45)
248+
}
249+
mock_refresh_authentication.return_value = None
250+
251+
# error messages
197252
add_info_error_message = ("Can't connect to the third party authentication service. "
198253
"Check that it is running.")
199254
add_info_status_code_error_message = "Status not OK in connection to the third party authentication service."
@@ -203,11 +258,8 @@ def test__refresh_authentication(self, mock_get_epoch_time, mock_requests):
203258
"and URLs correct?")
204259
oauth_status_code_error_message = "Authtentication access denied. Are the credentials correct?"
205260

206-
# test the expired connection
207-
mock_get_epoch_time.return_value = 200
208-
209261
# helper function
210-
def helper_before_call(*args, **kwargs):
262+
def helper_before_call(**kwargs):
211263
"""
212264
initialize mock objects and connection before testing th exception.
213265
@@ -217,18 +269,14 @@ def helper_before_call(*args, **kwargs):
217269
Connection.
218270
"""
219271

220-
mock_second_get_call.called = False
221-
mock_get_epoch_time.reset_mock() # reset mock.called
222272
# reset 'requests' mock because it is called in the `__init__`
223273
mock_requests.get.reset_mock(side_effect=True, return_value=True)
224-
connection = Connection(*args, auth_client_secret=kwargs.get("auth_client_secret", None))
225-
mock_requests.get.configure_mock(**kwargs['get'])
226-
if 'post' in kwargs:
227-
mock_requests.post.reset_mock(side_effect=True, return_value=True)
228-
mock_requests.post.configure_mock(**kwargs['post'])
274+
mock_requests.post.reset_mock(side_effect=True, return_value=True)
275+
connection = Connection(kwargs['url'], auth_client_secret=kwargs.get("auth_client_secret", None))
276+
mock_requests.configure_mock(**kwargs['requests'])
229277
self.check_connection_attributes(
230278
connection,
231-
url=kwargs.get("url", 'test_url/v1'),
279+
url=kwargs.get("url", 'test_url') + '/v1',
232280
timeout_config=kwargs.get("timeout_config", (2, 20)),
233281
auth_expires=kwargs.get("auth_expires", 0),
234282
auth_bearer=kwargs.get("auth_bearer", 0),
@@ -258,123 +306,66 @@ def helper_after_call(message, *args, **kwargs):
258306
auth_client_secret=kwargs.get("auth_client_secret", None),
259307
is_authentication_required=kwargs.get("is_authentication_required", False),
260308
)
261-
mock_get_epoch_time.assert_called()
262309
if 'get' in kwargs:
263310
mock_requests.get.assert_called_with(*kwargs['get_args'], **kwargs['get']) # only last call of this method
264311
if 'post' in kwargs:
265312
mock_requests.post.assert_called_with(*kwargs['post_args'], **kwargs['post']) # only last call of this method
266313

267-
def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
268-
"""
269-
Mock different results from requests.get
270-
NOTE: Use >>mock_second_get_call.called = False before calling/using this function.
271-
It is used in the `helper_before_call`.
272-
"""
273-
274-
if mock_second_get_call.called:
275-
return second_call_func()
276-
mock_second_get_call.called = True
277-
return first_call_func() # first call of request.get
278-
279-
# requests.get exception (get data)
280-
connection = helper_before_call('test_url', get={'side_effect' :RequestException('Test!')})
281-
with self.assertRaises(AuthenticationFailedException) as error:
282-
connection._refresh_authentication()
283-
get_kwargs = {
284-
'headers': {"content-type": "application/json"},
285-
'timeout': (30, 45)
286-
}
287-
helper_after_call(data_error_message, get_args=["test_url/v1/.well-known/openid-configuration"], get=get_kwargs)
288-
289-
# bad status_code (get data)
290-
connection = helper_before_call('test_url', get={'status_code': 404})
291-
with self.assertRaises(AuthenticationFailedException) as error:
292-
connection._refresh_authentication()
293-
get_kwargs = {
294-
'headers': {"content-type": "application/json"},
295-
'timeout': (30, 45)
296-
}
297-
helper_after_call(data_status_code_error_message, get_args=["test_url/v1/.well-known/openid-configuration"], get=get_kwargs)
298-
299314
# requests.get exception (get additional info)
300-
response = Mock(status_code=200)
301-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
302-
first_call_behaviour = lambda: response
303-
second_call_behaviour = lambda: exec('raise RequestException("Test!")')
304315
connection = helper_before_call(
305-
'test_url',
306-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)}
316+
url='test_url',
317+
requests={'get.side_effect': RequestException("Test!")}
307318
)
308319
with self.assertRaises(AuthenticationFailedException) as error:
309-
connection._refresh_authentication()
310-
get_kwargs = {
311-
'headers': {"content-type": "application/json"},
312-
'timeout': (30, 45)
313-
}
320+
connection._set_bearer('test_id', 'test_href')
314321
helper_after_call(add_info_error_message, get_args=["test_href"], get=get_kwargs)
315322

316323
# bad status_code (get additional info)
317-
response = Mock(status_code=200)
318-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
319-
first_call_behaviour = lambda: response
320-
second_call_behaviour = lambda: Mock(status_code=204)
321324
connection = helper_before_call(
322-
'test_url',
323-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)}
325+
url='test_url',
326+
requests={'get.return_value': Mock(status_code=204)}
324327
)
325328
with self.assertRaises(AuthenticationFailedException) as error:
326-
connection._refresh_authentication()
327-
get_kwargs = {
328-
'headers': {"content-type": "application/json"},
329-
'timeout': (30, 45)
330-
}
329+
connection._set_bearer('test_id', 'test_href')
331330
helper_after_call(add_info_status_code_error_message, get_args=["test_href"], get=get_kwargs)
332331

333332
# client_credentials error
334-
response = Mock(status_code=200)
335-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
336-
first_call_behaviour = lambda: response
337333
request_third_part = Mock(status_code=200)
338334
request_third_part.json.return_value = {'grant_types_supported': {'Test_key': 'Test_value'}}
339-
second_call_behaviour = lambda: request_third_part
340335
connection = helper_before_call(
341-
'test_url',
342-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)}
336+
url='test_url',
337+
requests={'get.return_value': request_third_part}
343338
)
344339
with self.assertRaises(AuthenticationFailedException) as error:
345-
connection._refresh_authentication()
340+
connection._set_bearer('test_id', 'test_href')
346341
get_kwargs = {
347342
'headers': {"content-type": "application/json"},
348343
'timeout': (30, 45)
349344
}
350345
helper_after_call(credentials_error_message, get_args=["test_href"], get=get_kwargs)
351346

352347
# OAuth error
353-
response = Mock(status_code=200)
354-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
355-
first_call_behaviour = lambda: response
356348
request_third_part = Mock(status_code=200)
357349
request_third_part.json.return_value = {
358350
'grant_types_supported': {'client_credentials': 'Test_cred!'},
359351
'token_endpoint': 'Test'}
360-
second_call_behaviour = lambda: request_third_part
361-
mock_auth = Mock()
362-
mock_auth.get_credentials.return_value = {'test_key': 'Value'}
352+
mock_auth = Mock(**{'get_credentials.return_value': {'test_key': 'Value'}})
363353
connection = helper_before_call(
364-
'test_url',
354+
url='test_url',
365355
auth_client_secret=mock_auth,
366-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)},
367-
post={'side_effect': RequestException('Test')}
368-
)
356+
requests={
357+
'get.return_value': request_third_part,
358+
'post.side_effect': RequestException('Test')}
359+
)
369360
with self.assertRaises(AuthenticationFailedException) as error:
370-
connection._refresh_authentication()
361+
connection._set_bearer('test_id', 'test_href')
371362
get_kwargs = {
372363
'headers': {"content-type": "application/json"},
373364
'timeout': (30, 45)
374365
}
375366
get_args = ["test_href"]
376367
post_kwargs = {'timeout': (30, 45)}
377-
post_args = ["Test", {'client_id': 'Test!ID', 'test_key': 'Value'}]
368+
post_args = ["Test", {'client_id': 'test_id', 'test_key': 'Value'}]
378369
helper_after_call(
379370
oauth_error_message,
380371
get=get_kwargs,
@@ -385,24 +376,20 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
385376
)
386377

387378
# OAuth status_code error
388-
response = Mock(status_code=200)
389-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
390-
first_call_behaviour = lambda: response
391379
request_third_part = Mock(status_code=200)
392380
request_third_part.json.return_value = {
393381
'grant_types_supported': {'client_credentials': 'Test_cred!'},
394382
'token_endpoint': 'Test'}
395-
second_call_behaviour = lambda: request_third_part
396-
mock_auth = Mock()
397-
mock_auth.get_credentials.return_value = {'test_key': 'Value'}
383+
mock_auth = Mock(**{'get_credentials.return_value': {'test_key': 'Value'}})
398384
connection = helper_before_call(
399-
'test_url',
385+
url='test_url',
400386
auth_client_secret=mock_auth,
401-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)},
402-
post={'return_value': Mock(status_code=401)}
403-
)
387+
requests={
388+
'get.return_value': request_third_part,
389+
'post.return_value': Mock(status_code=401)}
390+
)
404391
with self.assertRaises(AuthenticationFailedException) as error:
405-
connection._refresh_authentication()
392+
connection._set_bearer('Test!ID', 'test_href')
406393
get_kwargs = {
407394
'headers': {"content-type": "application/json"},
408395
'timeout': (30, 45)
@@ -420,28 +407,24 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
420407
)
421408

422409
# valid call
423-
response = Mock(status_code=200)
424-
response.json.return_value = {'clientId': 'Test!ID', 'href': "test_href"}
425-
first_call_behaviour = lambda: response
426410
request_third_part = Mock(status_code=200)
427411
request_third_part.json.return_value = {
428412
'grant_types_supported': {'client_credentials': 'Test_cred!'},
429413
'token_endpoint': 'Test'}
430-
second_call_behaviour = lambda: request_third_part
431-
mock_auth = Mock()
432-
mock_auth.get_credentials.return_value = {'test_key': 'Value'}
414+
mock_auth = Mock(**{'get_credentials.return_value': {'test_key': 'Value'}})
433415
mock_post_response = Mock(status_code=400)
434416
mock_post_response.json.return_value = {
435417
'access_token': 'TestBearer!',
436418
'expires_in': 1234
437419
}
438420
connection = helper_before_call(
439-
'test_url',
421+
url='test_url',
440422
auth_client_secret=mock_auth,
441-
get={'side_effect': lambda *args, **kwargs: mock_second_get_call(first_call_behaviour, second_call_behaviour)},
442-
post={'return_value': mock_post_response}
423+
requests={
424+
'get.return_value': request_third_part,
425+
'post.return_value': mock_post_response}
443426
)
444-
connection._refresh_authentication()
427+
connection._set_bearer('Test!ID', 'test_href')
445428
get_kwargs = {
446429
'headers': {"content-type": "application/json"},
447430
'timeout': (30, 45)
@@ -460,8 +443,6 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
460443
auth_bearer='TestBearer!'
461444
)
462445

463-
464-
465446
def test_timeout_config(self):
466447
"""
467448
Test the setter and getter of `timeout_config`.

test/test_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ def test_version(self):
88
Test the `__version__` global variable.
99
"""
1010

11-
self.assertEqual(weaviate.__version__, "2.1.0", "Check if the version is set correctly!")
11+
self.assertEqual(weaviate.__version__, "2.2.0", "Check if the version is set correctly!")

0 commit comments

Comments
 (0)