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