-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_datasources_and_workbooks_command.py
More file actions
252 lines (213 loc) · 11.4 KB
/
Copy pathtest_datasources_and_workbooks_command.py
File metadata and controls
252 lines (213 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import argparse
from unittest.mock import MagicMock
from tabcmd.commands.datasources_and_workbooks.datasources_and_workbooks_command import DatasourcesAndWorkbooks
from tabcmd.commands.datasources_and_workbooks.export_command import ExportCommand
import tableauserverclient as tsc
import unittest
from unittest import mock
mock_logger = mock.MagicMock()
fake_item = mock.MagicMock()
fake_item.name = "fake-name"
fake_item.id = "fake-id"
getter = MagicMock()
getter.get = MagicMock("get", return_value=([fake_item], 1))
getter.get_by_id = MagicMock("get_by_id", return_value=([fake_item], 1))
mock_args = argparse.Namespace()
class ParameterTests(unittest.TestCase):
def test_get_view_url_from_names(self):
wb_name = "WB"
view_name = "VIEW"
out_value = DatasourcesAndWorkbooks.get_view_url_from_names(wb_name, view_name)
assert out_value == "{}/sheets/{}".format(wb_name, view_name)
def test_apply_filters_from_url_params(self):
query_params = "?Product=widget"
expected = [("Product", "widget")]
request_options = tsc.PDFRequestOptions()
DatasourcesAndWorkbooks.apply_values_from_url_params(mock_logger, request_options, query_params)
assert request_options.view_filters == expected
def test_apply_encoded_filters_from_url_params(self):
query_params = "?Product%20type=Z%C3%BCrich"
expected = [("Product type", "Zürich")]
request_options = tsc.PDFRequestOptions()
DatasourcesAndWorkbooks.apply_values_from_url_params(mock_logger, request_options, query_params)
assert request_options.view_filters == expected
def test_apply_options_from_url_params(self):
query_params = "?:iid=5&:refresh=yes&:size=600,700"
request_options = tsc.PDFRequestOptions()
DatasourcesAndWorkbooks.apply_values_from_url_params(mock_logger, request_options, query_params)
assert request_options.max_age == 0
def test_apply_png_options(self):
mock_args.width = "800"
mock_args.height = "76"
mock_args.resolution = None
mock_args.language = None
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_png_options(mock_logger, request_options, mock_args)
assert request_options.image_resolution == "high"
assert request_options.viz_width == 800
assert request_options.viz_height == 76
def test_apply_png_options_with_language(self):
mock_args.width = "800"
mock_args.height = "76"
mock_args.resolution = None
mock_args.language = "de"
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_png_options(mock_logger, request_options, mock_args)
assert request_options.image_resolution == "high"
assert request_options.viz_width == 800
assert request_options.viz_height == 76
assert request_options.language == "de"
def test_apply_png_options_with_resolution_high(self):
mock_args.width = "800"
mock_args.height = "76"
mock_args.resolution = "high"
mock_args.language = None
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_png_options(mock_logger, request_options, mock_args)
assert request_options.image_resolution == "high"
assert request_options.viz_width == 800
assert request_options.viz_height == 76
def test_apply_png_options_with_resolution_standard(self):
mock_args.width = "800"
mock_args.height = "76"
mock_args.resolution = "standard"
mock_args.language = None
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_png_options(mock_logger, request_options, mock_args)
assert request_options.image_resolution is None
assert request_options.viz_width == 800
assert request_options.viz_height == 76
def test_apply_png_options_bad_values(self):
mock_args.height = "seven"
mock_args.width = "800b"
request_options = tsc.ImageRequestOptions()
with self.assertRaises(ValueError):
DatasourcesAndWorkbooks.apply_png_options(mock_logger, request_options, mock_args)
def test_apply_pdf_options(self):
expected_page = tsc.PDFRequestOptions.PageType.Folio.__str__()
expected_layout = tsc.PDFRequestOptions.Orientation.Portrait.__str__()
expected_height = 800
expected_width = 600
mock_args.pagelayout = expected_layout
mock_args.pagesize = expected_page
mock_args.height = expected_height
mock_args.width = expected_width
mock_args.language = None
request_options = tsc.PDFRequestOptions()
DatasourcesAndWorkbooks.apply_pdf_options(mock_logger, request_options, mock_args)
assert request_options.page_type == expected_page
assert request_options.orientation == expected_layout
assert request_options.viz_width == expected_width
assert request_options.viz_height == expected_height
def test_apply_pdf_options_with_language(self):
language = "de"
expected_page = tsc.PDFRequestOptions.PageType.Folio.__str__()
expected_layout = tsc.PDFRequestOptions.Orientation.Portrait.__str__()
mock_args.pagelayout = expected_layout
mock_args.pagesize = expected_page
mock_args.language = language
request_options = tsc.PDFRequestOptions()
DatasourcesAndWorkbooks.apply_pdf_options(mock_logger, request_options, mock_args)
assert request_options.page_type == expected_page
assert request_options.orientation == expected_layout
assert request_options.language == language
def test_apply_options_in_url_with_size(self):
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, ":size=1920,1080")
self.assertEqual(request_options.viz_width, 1920)
self.assertEqual(request_options.viz_height, 1080)
def test_apply_options_in_url_with_size_via_url_params(self):
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_values_from_url_params(mock_logger, request_options, "?:size=1920,1080")
self.assertEqual(request_options.viz_width, 1920)
self.assertEqual(request_options.viz_height, 1080)
def test_apply_options_in_url_with_refresh(self):
request_options = tsc.ImageRequestOptions()
value = ":refresh=yes"
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, value)
self.assertEqual(request_options.max_age, 0)
def test_apply_options_in_url_with_invalid_size(self):
request_options = tsc.ImageRequestOptions()
value = ":size=invalid"
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, value)
self.assertEqual(request_options.viz_height, None)
self.assertEqual(request_options.viz_width, None)
def test_apply_options_in_url_invalid_first_dimension_leaves_both_unset(self):
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, ":size=notanumber,600")
self.assertIsNone(request_options.viz_width)
self.assertIsNone(request_options.viz_height)
def test_apply_options_in_url_invalid_second_dimension_leaves_both_unset(self):
request_options = tsc.ImageRequestOptions()
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, ":size=1920,notanumber")
self.assertIsNone(request_options.viz_width)
self.assertIsNone(request_options.viz_height)
def test_apply_options_in_url_with_unrecognized_parameter(self):
request_options = tsc.ImageRequestOptions()
default_max_age = request_options.max_age
value = ":unknown=param"
DatasourcesAndWorkbooks.apply_options_in_url(mock_logger, request_options, value)
self.assertEqual(request_options.viz_height, None)
self.assertEqual(request_options.viz_width, None)
self.assertEqual(request_options.max_age, default_max_age)
def test_apply_csv_options(self):
mock_args.language = None
request_options = tsc.CSVRequestOptions()
DatasourcesAndWorkbooks.apply_csv_options(mock_logger, request_options, mock_args)
assert request_options.language == None
def test_apply_csv_options_with_language(self):
mock_args.language = "de"
request_options = tsc.CSVRequestOptions()
DatasourcesAndWorkbooks.apply_csv_options(mock_logger, request_options, mock_args)
assert request_options.language == "de"
def test_apply_filters_from_args_none(self):
args = argparse.Namespace(filter=None)
request_options = tsc.PDFRequestOptions()
ExportCommand.apply_filters_from_args(request_options, args, mock_logger)
assert request_options.view_filters == []
def test_apply_filters_from_args_single(self):
args = argparse.Namespace(filter=["Region=West"])
request_options = tsc.PDFRequestOptions()
ExportCommand.apply_filters_from_args(request_options, args, mock_logger)
assert request_options.view_filters == [("Region", "West")]
def test_apply_filters_from_args_repeated(self):
# repeated --filter flags each carry one pair, so '&' in a value is safe.
args = argparse.Namespace(filter=["Region=West", "Product=AT&T"])
request_options = tsc.PDFRequestOptions()
ExportCommand.apply_filters_from_args(request_options, args, mock_logger)
assert request_options.view_filters == [("Region", "West"), ("Product", "AT&T")]
def test_apply_filters_from_args_backcompat_ampersand_join(self):
# Old-style single flag joining pairs with '&' still parses.
args = argparse.Namespace(filter=["Region=West&Product=Widget"])
request_options = tsc.PDFRequestOptions()
ExportCommand.apply_filters_from_args(request_options, args, mock_logger)
assert request_options.view_filters == [("Region", "West"), ("Product", "Widget")]
@mock.patch("tableauserverclient.Server")
class MockedServerTests(unittest.TestCase):
def test_mock_getter(self, mock_server):
mock_server.fakes = getter
mock_server.fakes.get()
getter.get.assert_called()
def test_get_ds_by_content_url(self, mock_server):
mock_server.datasources = getter
content_url = "blah"
DatasourcesAndWorkbooks.get_ds_by_content_url(mock_logger, mock_server, content_url)
getter.get.assert_called()
# should also assert the filter on content url
def test_get_wb_by_content_url(self, mock_server):
mock_server.workbooks = getter
content_url = "blah"
DatasourcesAndWorkbooks.get_wb_by_content_url(mock_logger, mock_server, content_url)
getter.get.assert_called()
# should also assert the filter on content url
def test_get_view_by_content_url(self, mock_server):
mock_server.views = getter
content_url = "blah"
DatasourcesAndWorkbooks.get_view_by_content_url(mock_logger, mock_server, content_url)
getter.get.assert_called()
# should also assert the filter on content url
def test_get_custom_view_by_id(self, mock_server):
mock_server.custom_views = getter
custom_view_id = "cv-id"
DatasourcesAndWorkbooks.get_custom_view_by_id(mock_logger, mock_server, custom_view_id)
getter.get_by_id.assert_called()