|
1
|
|
|
import unittest |
|
2
|
|
|
from unittest.mock import Mock |
|
3
|
|
|
|
|
4
|
|
|
from tabpy.tabpy_tools.client import Client |
|
5
|
|
|
from tabpy.tabpy_tools.client import _check_endpoint_name |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestClient(unittest.TestCase): |
|
9
|
|
|
|
|
10
|
|
|
def setUp(self): |
|
11
|
|
|
self.client = Client("http://example.com/") |
|
12
|
|
|
self.client._service = Mock() # TODO: should spec this |
|
13
|
|
|
|
|
14
|
|
|
def test_init(self): |
|
15
|
|
|
client = Client("http://example.com:9004") |
|
16
|
|
|
|
|
17
|
|
|
self.assertEqual(client._endpoint, "http://example.com:9004") |
|
18
|
|
|
|
|
19
|
|
|
client = Client("http://example.com/", 10.0) |
|
20
|
|
|
|
|
21
|
|
|
self.assertEqual(client._endpoint, "http://example.com/") |
|
22
|
|
|
|
|
23
|
|
|
client = Client( |
|
24
|
|
|
endpoint="https://example.com/", |
|
25
|
|
|
query_timeout=-10.0) |
|
26
|
|
|
|
|
27
|
|
|
self.assertEqual(client._endpoint, "https://example.com/") |
|
28
|
|
|
self.assertEqual(client.query_timeout, 0.0) |
|
29
|
|
|
|
|
30
|
|
|
# valid name tests |
|
31
|
|
|
with self.assertRaises(ValueError): |
|
32
|
|
|
Client('') |
|
33
|
|
|
with self.assertRaises(TypeError): |
|
34
|
|
|
Client(1.0) |
|
35
|
|
|
with self.assertRaises(ValueError): |
|
36
|
|
|
Client("*#") |
|
37
|
|
|
with self.assertRaises(TypeError): |
|
38
|
|
|
Client() |
|
39
|
|
|
with self.assertRaises(ValueError): |
|
40
|
|
|
Client("http:/www.example.com/") |
|
41
|
|
|
with self.assertRaises(ValueError): |
|
42
|
|
|
Client("httpx://www.example.com:9004") |
|
43
|
|
|
|
|
44
|
|
|
def test_get_status(self): |
|
45
|
|
|
self.client._service.get_status.return_value = "asdf" |
|
46
|
|
|
self.assertEqual(self.client.get_status(), "asdf") |
|
47
|
|
|
|
|
48
|
|
|
def test_query_timeout(self): |
|
49
|
|
|
self.client.query_timeout = 5.0 |
|
50
|
|
|
self.assertEqual(self.client.query_timeout, 5.0) |
|
51
|
|
|
self.assertEqual(self.client._service.query_timeout, 5.0) |
|
52
|
|
|
|
|
53
|
|
|
def test_query(self): |
|
54
|
|
|
self.client._service.query.return_value = "ok" |
|
55
|
|
|
|
|
56
|
|
|
self.assertEqual(self.client.query("foo", 1, 2, 3), "ok") |
|
57
|
|
|
|
|
58
|
|
|
self.client._service.query.assert_called_once_with("foo", 1, 2, 3) |
|
59
|
|
|
|
|
60
|
|
|
self.client._service.query.reset_mock() |
|
61
|
|
|
|
|
62
|
|
|
self.assertEqual(self.client.query("foo", a=1, b=2, c=3), "ok") |
|
63
|
|
|
|
|
64
|
|
|
self.client._service.query.assert_called_once_with( |
|
65
|
|
|
"foo", a=1, b=2, c=3) |
|
66
|
|
|
|
|
67
|
|
|
def test_get_endpoints(self): |
|
68
|
|
|
self.client._service.get_endpoints.return_value = "foo" |
|
69
|
|
|
|
|
70
|
|
|
self.assertEqual(self.client.get_endpoints("foo"), "foo") |
|
71
|
|
|
|
|
72
|
|
|
self.client._service.get_endpoints.assert_called_once_with("foo") |
|
73
|
|
|
|
|
74
|
|
|
def test_get_endpoint_upload_destination(self): |
|
75
|
|
|
self.client._service.get_endpoint_upload_destination.return_value = \ |
|
76
|
|
|
{"path": "foo"} |
|
77
|
|
|
|
|
78
|
|
|
self.assertEqual(self.client._get_endpoint_upload_destination(), "foo") |
|
79
|
|
|
|
|
80
|
|
|
def test_set_credentials(self): |
|
81
|
|
|
username, password = "username", "password" |
|
82
|
|
|
self.client.set_credentials(username, password) |
|
83
|
|
|
|
|
84
|
|
|
self.client._service.set_credentials.assert_called_once_with( |
|
85
|
|
|
username, password) |
|
86
|
|
|
|
|
87
|
|
|
def test_check_invalid_endpoint_name(self): |
|
88
|
|
|
endpoint_name = 'Invalid:model:@name' |
|
89
|
|
|
with self.assertRaises(ValueError) as err: |
|
90
|
|
|
_check_endpoint_name(endpoint_name) |
|
91
|
|
|
|
|
92
|
|
|
self.assertEqual(err.exception.args[0], |
|
93
|
|
|
f'endpoint name {endpoint_name } can only contain: ' |
|
94
|
|
|
'a-z, A-Z, 0-9, underscore, hyphens and spaces.') |
|
95
|
|
|
|