|
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
|
|
|
def setUp(self): |
|
10
|
|
|
self.client = Client("http://example.com/") |
|
11
|
|
|
self.client._service = Mock() # TODO: should spec this |
|
12
|
|
|
|
|
13
|
|
|
def test_init(self): |
|
14
|
|
|
client = Client("http://example.com:9004") |
|
15
|
|
|
self.assertEqual(client._endpoint, "http://example.com:9004") |
|
16
|
|
|
self.assertEqual(client._remote_server, False) |
|
17
|
|
|
|
|
18
|
|
|
client = Client("http://example.com/", 10.0) |
|
19
|
|
|
self.assertEqual(client._endpoint, "http://example.com/") |
|
20
|
|
|
|
|
21
|
|
|
client = Client(endpoint="https://example.com/", query_timeout=-10.0) |
|
22
|
|
|
self.assertEqual(client._endpoint, "https://example.com/") |
|
23
|
|
|
self.assertEqual(client.query_timeout, 0.0) |
|
24
|
|
|
|
|
25
|
|
|
client = Client( |
|
26
|
|
|
"http://example.com:442/", |
|
27
|
|
|
remote_server=True, |
|
28
|
|
|
localhost_endpoint="http://localhost:9004/" |
|
29
|
|
|
) |
|
30
|
|
|
self.assertEqual(client._endpoint, "http://example.com:442/") |
|
31
|
|
|
self.assertEqual(client._remote_server, True) |
|
32
|
|
|
self.assertEqual(client._localhost_endpoint, "http://localhost:9004/") |
|
33
|
|
|
|
|
34
|
|
|
# valid name tests |
|
35
|
|
|
with self.assertRaises(ValueError): |
|
36
|
|
|
Client("") |
|
37
|
|
|
with self.assertRaises(TypeError): |
|
38
|
|
|
Client(1.0) |
|
39
|
|
|
with self.assertRaises(ValueError): |
|
40
|
|
|
Client("*#") |
|
41
|
|
|
with self.assertRaises(TypeError): |
|
42
|
|
|
Client() |
|
43
|
|
|
with self.assertRaises(ValueError): |
|
44
|
|
|
Client("http:/www.example.com/") |
|
45
|
|
|
with self.assertRaises(ValueError): |
|
46
|
|
|
Client("httpx://www.example.com:9004") |
|
47
|
|
|
|
|
48
|
|
|
def test_get_status(self): |
|
49
|
|
|
self.client._service.get_status.return_value = "asdf" |
|
50
|
|
|
self.assertEqual(self.client.get_status(), "asdf") |
|
51
|
|
|
|
|
52
|
|
|
def test_query_timeout(self): |
|
53
|
|
|
self.client.query_timeout = 5.0 |
|
54
|
|
|
self.assertEqual(self.client.query_timeout, 5.0) |
|
55
|
|
|
self.assertEqual(self.client._service.query_timeout, 5.0) |
|
56
|
|
|
|
|
57
|
|
|
def test_query(self): |
|
58
|
|
|
self.client._service.query.return_value = "ok" |
|
59
|
|
|
|
|
60
|
|
|
self.assertEqual(self.client.query("foo", 1, 2, 3), "ok") |
|
61
|
|
|
|
|
62
|
|
|
self.client._service.query.assert_called_once_with("foo", 1, 2, 3) |
|
63
|
|
|
|
|
64
|
|
|
self.client._service.query.reset_mock() |
|
65
|
|
|
|
|
66
|
|
|
self.assertEqual(self.client.query("foo", a=1, b=2, c=3), "ok") |
|
67
|
|
|
|
|
68
|
|
|
self.client._service.query.assert_called_once_with("foo", a=1, b=2, c=3) |
|
69
|
|
|
|
|
70
|
|
|
def test_get_endpoints(self): |
|
71
|
|
|
self.client._service.get_endpoints.return_value = "foo" |
|
72
|
|
|
|
|
73
|
|
|
self.assertEqual(self.client.get_endpoints("foo"), "foo") |
|
74
|
|
|
|
|
75
|
|
|
self.client._service.get_endpoints.assert_called_once_with("foo") |
|
76
|
|
|
|
|
77
|
|
|
def test_get_endpoint_upload_destination(self): |
|
78
|
|
|
self.client._service.get_endpoint_upload_destination.return_value = { |
|
79
|
|
|
"path": "foo" |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
self.assertEqual(self.client._get_endpoint_upload_destination(), "foo") |
|
83
|
|
|
|
|
84
|
|
|
def test_set_credentials(self): |
|
85
|
|
|
username, password = "username", "password" |
|
86
|
|
|
self.client.set_credentials(username, password) |
|
87
|
|
|
|
|
88
|
|
|
self.client._service.set_credentials.assert_called_once_with(username, password) |
|
89
|
|
|
|
|
90
|
|
|
def test_check_invalid_endpoint_name(self): |
|
91
|
|
|
endpoint_name = "Invalid:model:@name" |
|
92
|
|
|
with self.assertRaises(ValueError) as err: |
|
93
|
|
|
_check_endpoint_name(endpoint_name) |
|
94
|
|
|
|
|
95
|
|
|
self.assertEqual( |
|
96
|
|
|
err.exception.args[0], |
|
97
|
|
|
f"endpoint name {endpoint_name } can only contain: " |
|
98
|
|
|
"a-z, A-Z, 0-9, underscore, hyphens and spaces.", |
|
99
|
|
|
) |
|
100
|
|
|
|
|
101
|
|
|
def test_deploy_with_remote_server(self): |
|
102
|
|
|
client = Client("http://example.com:9004/", remote_server=True) |
|
103
|
|
|
mock_evaluate_remote_script = Mock() |
|
104
|
|
|
client._evaluate_remote_script = mock_evaluate_remote_script |
|
105
|
|
|
client.deploy('name', lambda: True, 'description') |
|
106
|
|
|
mock_evaluate_remote_script.assert_called() |
|
107
|
|
|
|
|
108
|
|
|
def test_gen_remote_script(self): |
|
109
|
|
|
client = Client("http://example.com:9004/", remote_server=True) |
|
110
|
|
|
script = client._gen_remote_script() |
|
111
|
|
|
self.assertTrue("from tabpy.tabpy_tools.client import Client" in script) |
|
112
|
|
|
self.assertTrue("client = Client('http://example.com:9004/')" in script) |
|
113
|
|
|
self.assertFalse("client.set_credentials" in script) |
|
114
|
|
|
|
|
115
|
|
|
client.set_credentials("username", "password") |
|
116
|
|
|
script = client._gen_remote_script() |
|
117
|
|
|
self.assertTrue("client.set_credentials('username', 'password')" in script) |
|
118
|
|
|
|