|
| 1 | +from contextlib import ExitStack |
1 | 2 | import io |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
| 5 | +from tempfile import TemporaryDirectory |
4 | 6 | import unittest |
5 | 7 |
|
6 | 8 | import requests_mock |
7 | 9 |
|
8 | 10 | import tableauserverclient as TSC |
| 11 | +from tableauserverclient.config import BYTES_PER_MB |
9 | 12 | from tableauserverclient.datetime_helpers import format_datetime |
| 13 | +from tableauserverclient.server.endpoint.exceptions import MissingRequiredFieldError |
10 | 14 |
|
11 | 15 | TEST_ASSET_DIR = Path(__file__).parent / "assets" |
12 | 16 |
|
|
15 | 19 | POPULATE_PREVIEW_IMAGE = os.path.join(TEST_ASSET_DIR, "Sample View Image.png") |
16 | 20 | CUSTOM_VIEW_UPDATE_XML = os.path.join(TEST_ASSET_DIR, "custom_view_update.xml") |
17 | 21 | CUSTOM_VIEW_DOWNLOAD = TEST_ASSET_DIR / "custom_view_download.json" |
| 22 | +FILE_UPLOAD_INIT = TEST_ASSET_DIR / "fileupload_initialize.xml" |
| 23 | +FILE_UPLOAD_APPEND = TEST_ASSET_DIR / "fileupload_append.xml" |
18 | 24 |
|
19 | 25 |
|
20 | 26 | class CustomViewTests(unittest.TestCase): |
@@ -146,3 +152,97 @@ def test_download(self) -> None: |
146 | 152 | self.server.custom_views.download(cv, data) |
147 | 153 |
|
148 | 154 | assert data.getvalue() == content |
| 155 | + |
| 156 | + def test_publish_filepath(self) -> None: |
| 157 | + cv = TSC.CustomViewItem(name="test") |
| 158 | + cv._owner = TSC.UserItem() |
| 159 | + cv._owner._id = "dd2239f6-ddf1-4107-981a-4cf94e415794" |
| 160 | + cv._workbook = TSC.WorkbookItem() |
| 161 | + cv._workbook._id = "1f951daf-4061-451a-9df1-69a8062664f2" |
| 162 | + with requests_mock.mock() as m: |
| 163 | + m.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 164 | + view = self.server.custom_views.publish(cv, CUSTOM_VIEW_DOWNLOAD) |
| 165 | + |
| 166 | + assert view is not None |
| 167 | + assert isinstance(view, TSC.CustomViewItem) |
| 168 | + assert view.id is not None |
| 169 | + assert view.name is not None |
| 170 | + |
| 171 | + def test_publish_file_str(self) -> None: |
| 172 | + cv = TSC.CustomViewItem(name="test") |
| 173 | + cv._owner = TSC.UserItem() |
| 174 | + cv._owner._id = "dd2239f6-ddf1-4107-981a-4cf94e415794" |
| 175 | + cv._workbook = TSC.WorkbookItem() |
| 176 | + cv._workbook._id = "1f951daf-4061-451a-9df1-69a8062664f2" |
| 177 | + with requests_mock.mock() as m: |
| 178 | + m.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 179 | + view = self.server.custom_views.publish(cv, str(CUSTOM_VIEW_DOWNLOAD)) |
| 180 | + |
| 181 | + assert view is not None |
| 182 | + assert isinstance(view, TSC.CustomViewItem) |
| 183 | + assert view.id is not None |
| 184 | + assert view.name is not None |
| 185 | + |
| 186 | + def test_publish_file_io(self) -> None: |
| 187 | + cv = TSC.CustomViewItem(name="test") |
| 188 | + cv._owner = TSC.UserItem() |
| 189 | + cv._owner._id = "dd2239f6-ddf1-4107-981a-4cf94e415794" |
| 190 | + cv._workbook = TSC.WorkbookItem() |
| 191 | + cv._workbook._id = "1f951daf-4061-451a-9df1-69a8062664f2" |
| 192 | + data = io.BytesIO(CUSTOM_VIEW_DOWNLOAD.read_bytes()) |
| 193 | + with requests_mock.mock() as m: |
| 194 | + m.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 195 | + view = self.server.custom_views.publish(cv, data) |
| 196 | + |
| 197 | + assert view is not None |
| 198 | + assert isinstance(view, TSC.CustomViewItem) |
| 199 | + assert view.id is not None |
| 200 | + assert view.name is not None |
| 201 | + |
| 202 | + def test_publish_missing_owner_id(self) -> None: |
| 203 | + cv = TSC.CustomViewItem(name="test") |
| 204 | + cv._owner = TSC.UserItem() |
| 205 | + cv._workbook = TSC.WorkbookItem() |
| 206 | + cv._workbook._id = "1f951daf-4061-451a-9df1-69a8062664f2" |
| 207 | + with requests_mock.mock() as m: |
| 208 | + m.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 209 | + with self.assertRaises(ValueError): |
| 210 | + self.server.custom_views.publish(cv, CUSTOM_VIEW_DOWNLOAD) |
| 211 | + |
| 212 | + def test_publish_missing_wb_id(self) -> None: |
| 213 | + cv = TSC.CustomViewItem(name="test") |
| 214 | + cv._owner = TSC.UserItem() |
| 215 | + cv._owner._id = "dd2239f6-ddf1-4107-981a-4cf94e415794" |
| 216 | + cv._workbook = TSC.WorkbookItem() |
| 217 | + with requests_mock.mock() as m: |
| 218 | + m.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 219 | + with self.assertRaises(ValueError): |
| 220 | + self.server.custom_views.publish(cv, CUSTOM_VIEW_DOWNLOAD) |
| 221 | + |
| 222 | + def test_large_publish(self): |
| 223 | + cv = TSC.CustomViewItem(name="test") |
| 224 | + cv._owner = TSC.UserItem() |
| 225 | + cv._owner._id = "dd2239f6-ddf1-4107-981a-4cf94e415794" |
| 226 | + cv._workbook = TSC.WorkbookItem() |
| 227 | + cv._workbook._id = "1f951daf-4061-451a-9df1-69a8062664f2" |
| 228 | + with ExitStack() as stack: |
| 229 | + temp_dir = stack.enter_context(TemporaryDirectory()) |
| 230 | + file_path = Path(temp_dir) / "test_file" |
| 231 | + file_path.write_bytes(os.urandom(65 * BYTES_PER_MB)) |
| 232 | + mock = stack.enter_context(requests_mock.mock()) |
| 233 | + # Mock initializing upload |
| 234 | + mock.post(self.server.fileuploads.baseurl, status_code=201, text=FILE_UPLOAD_INIT.read_text()) |
| 235 | + # Mock the upload |
| 236 | + mock.put( |
| 237 | + f"{self.server.fileuploads.baseurl}/7720:170fe6b1c1c7422dadff20f944d58a52-1:0", |
| 238 | + text=FILE_UPLOAD_APPEND.read_text(), |
| 239 | + ) |
| 240 | + # Mock the publish |
| 241 | + mock.post(self.server.custom_views.expurl, status_code=201, text=Path(GET_XML).read_text()) |
| 242 | + |
| 243 | + view = self.server.custom_views.publish(cv, file_path) |
| 244 | + |
| 245 | + assert view is not None |
| 246 | + assert isinstance(view, TSC.CustomViewItem) |
| 247 | + assert view.id is not None |
| 248 | + assert view.name is not None |
0 commit comments