1
1
import tableauserverclient as TSC
2
2
import unittest
3
3
import requests
4
+ import requests_mock
4
5
5
- from requests_mock import adapter , mock
6
+ from unittest import mock
6
7
from requests .exceptions import MissingSchema
7
8
8
9
10
+ # This method will be used by the mock to replace requests.get
11
+ def mocked_requests_get (* args , ** kwargs ):
12
+ class MockResponse :
13
+ def __init__ (self , status_code ):
14
+ self .content = (
15
+ "<xml>"
16
+ "<version version='0.31'>"
17
+ "<api_version>0.31</api_version>"
18
+ "<server_api_version>0.31</server_api_version>"
19
+ "<product_version>2022.3</product_version>"
20
+ "</version>"
21
+ "</xml>"
22
+ )
23
+ self .status_code = status_code
24
+
25
+ return MockResponse (200 )
26
+
27
+
9
28
class ServerTests (unittest .TestCase ):
10
29
def test_init_server_model_empty_throws (self ):
11
30
with self .assertRaises (TypeError ):
12
31
server = TSC .Server ()
13
32
14
- def test_init_server_model_bad_server_name_complains (self ):
15
- # by default, it will just set the version to 2.3
33
+ def test_init_server_model_no_protocol_defaults_htt (self ):
16
34
server = TSC .Server ("fake-url" )
17
35
18
36
def test_init_server_model_valid_server_name_works (self ):
19
- # by default, it will just set the version to 2.3
20
37
server = TSC .Server ("http://fake-url" )
21
38
22
39
def test_init_server_model_valid_https_server_name_works (self ):
23
40
# by default, it will just set the version to 2.3
24
41
server = TSC .Server ("https://fake-url" )
25
42
26
43
def test_init_server_model_bad_server_name_not_version_check (self ):
27
- # by default, it will just set the version to 2.3
28
44
server = TSC .Server ("fake-url" , use_server_version = False )
29
45
30
46
def test_init_server_model_bad_server_name_do_version_check (self ):
31
- with self .assertRaises (MissingSchema ):
47
+ with self .assertRaises (requests . exceptions . ConnectionError ):
32
48
server = TSC .Server ("fake-url" , use_server_version = True )
33
49
34
50
def test_init_server_model_bad_server_name_not_version_check_random_options (self ):
35
- # by default, it will just set the version to 2.3
51
+ # with self.assertRaises(MissingSchema):
36
52
server = TSC .Server ("fake-url" , use_server_version = False , http_options = {"foo" : 1 })
37
53
38
54
def test_init_server_model_bad_server_name_not_version_check_real_options (self ):
55
+ # with self.assertRaises(ValueError):
39
56
server = TSC .Server ("fake-url" , use_server_version = False , http_options = {"verify" : False })
40
57
41
58
def test_http_options_skip_ssl_works (self ):
@@ -62,6 +79,25 @@ def test_http_options_not_sequence_fails(self):
62
79
with self .assertRaises (ValueError ):
63
80
server .add_http_options ({1 , 2 , 3 })
64
81
82
+ def test_validate_connection_http (self ):
83
+ url = "http://cookies.com"
84
+ server = TSC .Server (url )
85
+ server .validate_server_connection ()
86
+ self .assertEqual (url , server .server_address )
87
+
88
+ def test_validate_connection_https (self ):
89
+ url = "https://cookies.com"
90
+ server = TSC .Server (url )
91
+ server .validate_server_connection ()
92
+ self .assertEqual (url , server .server_address )
93
+
94
+ def test_validate_connection_no_protocol (self ):
95
+ url = "cookies.com"
96
+ fixed_url = "http://cookies.com"
97
+ server = TSC .Server (url )
98
+ server .validate_server_connection ()
99
+ self .assertEqual (fixed_url , server .server_address )
100
+
65
101
66
102
class SessionTests (unittest .TestCase ):
67
103
test_header = {"x-test" : "true" }
@@ -74,6 +110,6 @@ def session_factory():
74
110
75
111
def test_session_factory_adds_headers (self ):
76
112
test_request_bin = "http://capture-this-with-mock.com"
77
- with mock () as m :
113
+ with requests_mock . mock () as m :
78
114
m .get (url = "http://capture-this-with-mock.com/api/2.4/serverInfo" , request_headers = SessionTests .test_header )
79
115
server = TSC .Server (test_request_bin , use_server_version = True , session_factory = SessionTests .session_factory )
0 commit comments