-
Notifications
You must be signed in to change notification settings - Fork 75
/
tests.py
45 lines (34 loc) · 1.5 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from cosm.query.rest_client import QueryRestClient
from unittest import TestCase
from unittest.mock import Mock, patch
from requests import Session, Response
class QueryTests(TestCase):
@patch("requests.session", spec=Session)
def test_get_pass(self, session_mock):
rest_address = "some url"
client = QueryRestClient(rest_address)
session_mock.assert_called_once_with()
assert client.rest_address == rest_address
request_url_path = "my weird url path"
resp = Mock(spec=Response)
resp.status_code = 200
resp.content = "dfdffdss".encode(encoding="utf8")
session_mock.return_value.get.return_value = resp
client.get(request_url_path)
session_mock.return_value.get.assert_called_once_with(
url=rest_address + request_url_path
)
@patch("requests.session", spec=Session)
def test_get_error(self, session_mock):
rest_address = "some url"
client = QueryRestClient(rest_address)
session_mock.assert_called_once_with()
assert client.rest_address == rest_address
request_url_path = "my weird url path"
resp = Mock(spec=Response)
resp.status_code = 400
resp.content = "dfdffdss".encode(encoding="utf8")
session_mock.return_value.get.return_value = resp
with self.assertRaises(Exception) as context:
client.get(request_url_path)
self.assertTrue("Error when sending a query request" in str(context.exception))