|
| 1 | +import pytest |
1 | 2 | import tableauserverclient as TSC |
2 | 3 | import unittest |
3 | 4 | import requests |
@@ -27,91 +28,103 @@ def __init__(self, status_code): |
27 | 28 | return MockResponse(200) |
28 | 29 |
|
29 | 30 |
|
30 | | -class ServerTests(unittest.TestCase): |
31 | | - def test_init_server_model_empty_throws(self): |
32 | | - with self.assertRaises(TypeError): |
33 | | - server = TSC.Server() |
34 | | - |
35 | | - def test_init_server_model_no_protocol_defaults_htt(self): |
36 | | - server = TSC.Server("fake-url") |
37 | | - |
38 | | - def test_init_server_model_valid_server_name_works(self): |
39 | | - server = TSC.Server("http://fake-url") |
40 | | - |
41 | | - def test_init_server_model_valid_https_server_name_works(self): |
42 | | - # by default, it will just set the version to 2.3 |
43 | | - server = TSC.Server("https://fake-url") |
44 | | - |
45 | | - def test_init_server_model_bad_server_name_not_version_check(self): |
46 | | - server = TSC.Server("fake-url", use_server_version=False) |
47 | | - |
48 | | - @mock.patch("requests.sessions.Session.get", side_effect=mocked_requests_get) |
49 | | - def test_init_server_model_bad_server_name_do_version_check(self, mock_get): |
50 | | - server = TSC.Server("fake-url", use_server_version=True) |
51 | | - |
52 | | - def test_init_server_model_bad_server_name_not_version_check_random_options(self): |
53 | | - # with self.assertRaises(MissingSchema): |
54 | | - server = TSC.Server("fake-url", use_server_version=False, http_options={"foo": 1}) |
55 | | - |
56 | | - def test_init_server_model_bad_server_name_not_version_check_real_options(self): |
57 | | - # with self.assertRaises(ValueError): |
58 | | - server = TSC.Server("fake-url", use_server_version=False, http_options={"verify": False}) |
59 | | - |
60 | | - def test_http_options_skip_ssl_works(self): |
61 | | - http_options = {"verify": False} |
62 | | - server = TSC.Server("http://fake-url") |
63 | | - server.add_http_options(http_options) |
64 | | - |
65 | | - def test_http_options_multiple_options_works(self): |
66 | | - http_options = {"verify": False, "birdname": "Parrot"} |
67 | | - server = TSC.Server("http://fake-url") |
68 | | - server.add_http_options(http_options) |
69 | | - |
70 | | - # ValueError: dictionary update sequence element #0 has length 1; 2 is required |
71 | | - def test_http_options_multiple_dicts_fails(self): |
72 | | - http_options_1 = {"verify": False} |
73 | | - http_options_2 = {"birdname": "Parrot"} |
74 | | - server = TSC.Server("http://fake-url") |
75 | | - with self.assertRaises(ValueError): |
76 | | - server.add_http_options([http_options_1, http_options_2]) |
77 | | - |
78 | | - # TypeError: cannot convert dictionary update sequence element #0 to a sequence |
79 | | - def test_http_options_not_sequence_fails(self): |
80 | | - server = TSC.Server("http://fake-url") |
81 | | - with self.assertRaises(ValueError): |
82 | | - server.add_http_options({1, 2, 3}) |
83 | | - |
84 | | - def test_validate_connection_http(self): |
85 | | - url = "http://cookies.com" |
86 | | - server = TSC.Server(url) |
87 | | - server.validate_connection_settings() |
88 | | - self.assertEqual(url, server.server_address) |
89 | | - |
90 | | - def test_validate_connection_https(self): |
91 | | - url = "https://cookies.com" |
92 | | - server = TSC.Server(url) |
93 | | - server.validate_connection_settings() |
94 | | - self.assertEqual(url, server.server_address) |
95 | | - |
96 | | - def test_validate_connection_no_protocol(self): |
97 | | - url = "cookies.com" |
98 | | - fixed_url = "http://cookies.com" |
99 | | - server = TSC.Server(url) |
100 | | - server.validate_connection_settings() |
101 | | - self.assertEqual(fixed_url, server.server_address) |
102 | | - |
103 | | - |
104 | | -class SessionTests(unittest.TestCase): |
105 | | - test_header = {"x-test": "true"} |
106 | | - |
107 | | - @staticmethod |
108 | | - def session_factory(): |
109 | | - session = requests.session() |
110 | | - session.headers.update(SessionTests.test_header) |
111 | | - return session |
112 | | - |
113 | | - def test_session_factory_adds_headers(self): |
114 | | - test_request_bin = "http://capture-this-with-mock.com" |
115 | | - with requests_mock.mock() as m: |
116 | | - m.get(url="http://capture-this-with-mock.com/api/2.4/serverInfo", request_headers=SessionTests.test_header) |
117 | | - server = TSC.Server(test_request_bin, use_server_version=True, session_factory=SessionTests.session_factory) |
| 31 | +def test_init_server_model_empty_throws(): |
| 32 | + with pytest.raises(TypeError): |
| 33 | + server = TSC.Server() |
| 34 | + |
| 35 | + |
| 36 | +def test_init_server_model_no_protocol_defaults_htt(): |
| 37 | + server = TSC.Server("fake-url") |
| 38 | + |
| 39 | + |
| 40 | +def test_init_server_model_valid_server_name_works(): |
| 41 | + server = TSC.Server("http://fake-url") |
| 42 | + |
| 43 | + |
| 44 | +def test_init_server_model_valid_https_server_name_works(): |
| 45 | + # by default, it will just set the version to 2.3 |
| 46 | + server = TSC.Server("https://fake-url") |
| 47 | + |
| 48 | + |
| 49 | +def test_init_server_model_bad_server_name_not_version_check(): |
| 50 | + server = TSC.Server("fake-url", use_server_version=False) |
| 51 | + |
| 52 | + |
| 53 | +@mock.patch("requests.sessions.Session.get", side_effect=mocked_requests_get) |
| 54 | +def test_init_server_model_bad_server_name_do_version_check(mock_get): |
| 55 | + server = TSC.Server("fake-url", use_server_version=True) |
| 56 | + |
| 57 | + |
| 58 | +def test_init_server_model_bad_server_name_not_version_check_random_options(): |
| 59 | + server = TSC.Server("fake-url", use_server_version=False, http_options={"foo": 1}) |
| 60 | + |
| 61 | + |
| 62 | +def test_init_server_model_bad_server_name_not_version_check_real_options(): |
| 63 | + server = TSC.Server("fake-url", use_server_version=False, http_options={"verify": False}) |
| 64 | + |
| 65 | + |
| 66 | +def test_http_options_skip_ssl_works(): |
| 67 | + http_options = {"verify": False} |
| 68 | + server = TSC.Server("http://fake-url") |
| 69 | + server.add_http_options(http_options) |
| 70 | + |
| 71 | + |
| 72 | +def test_http_options_multiple_options_works(): |
| 73 | + http_options = {"verify": False, "birdname": "Parrot"} |
| 74 | + server = TSC.Server("http://fake-url") |
| 75 | + server.add_http_options(http_options) |
| 76 | + |
| 77 | + |
| 78 | +# ValueError: dictionary update sequence element #0 has length 1; 2 is required |
| 79 | +def test_http_options_multiple_dicts_fails(): |
| 80 | + http_options_1 = {"verify": False} |
| 81 | + http_options_2 = {"birdname": "Parrot"} |
| 82 | + server = TSC.Server("http://fake-url") |
| 83 | + with pytest.raises(ValueError): |
| 84 | + server.add_http_options([http_options_1, http_options_2]) |
| 85 | + |
| 86 | + |
| 87 | +# TypeError: cannot convert dictionary update sequence element #0 to a sequence |
| 88 | +def test_http_options_not_sequence_fails(): |
| 89 | + server = TSC.Server("http://fake-url") |
| 90 | + with pytest.raises(ValueError): |
| 91 | + server.add_http_options({1, 2, 3}) |
| 92 | + |
| 93 | + |
| 94 | +def test_validate_connection_http(): |
| 95 | + url = "http://cookies.com" |
| 96 | + server = TSC.Server(url) |
| 97 | + server.validate_connection_settings() |
| 98 | + assert url == server.server_address |
| 99 | + |
| 100 | + |
| 101 | +def test_validate_connection_https(): |
| 102 | + url = "https://cookies.com" |
| 103 | + server = TSC.Server(url) |
| 104 | + server.validate_connection_settings() |
| 105 | + assert url == server.server_address |
| 106 | + |
| 107 | + |
| 108 | +def test_validate_connection_no_protocol(): |
| 109 | + url = "cookies.com" |
| 110 | + fixed_url = "http://cookies.com" |
| 111 | + server = TSC.Server(url) |
| 112 | + server.validate_connection_settings() |
| 113 | + assert fixed_url == server.server_address |
| 114 | + |
| 115 | + |
| 116 | +test_header = {"x-test": "true"} |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture |
| 120 | +def session_factory() -> requests.Session: |
| 121 | + session = requests.session() |
| 122 | + session.headers.update(test_header) |
| 123 | + return session |
| 124 | + |
| 125 | + |
| 126 | +def test_session_factory_adds_headers(session_factory): |
| 127 | + test_request_bin = "http://capture-this-with-mock.com" |
| 128 | + with requests_mock.mock() as m: |
| 129 | + m.get(url="http://capture-this-with-mock.com/api/2.4/serverInfo", request_headers=test_header) |
| 130 | + server = TSC.Server(test_request_bin, use_server_version=True, session_factory=lambda: session_factory) |
0 commit comments