Skip to content

Commit cd39b4e

Browse files
committed
always use helper method to locate files from tests
1 parent d0efb32 commit cd39b4e

26 files changed

+264
-294
lines changed

test/_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
77

88

9-
def asset(filename):
10-
return os.path.join(TEST_ASSET_DIR, filename)
9+
def xml_asset_path(filename):
10+
return os.path.join(TEST_ASSET_DIR, "responses", filename)
11+
12+
13+
def data_asset_path(filename):
14+
return os.path.join(TEST_ASSET_DIR, "data", filename)
1115

1216

1317
def read_xml_asset(filename):
14-
with open(asset(filename), "rb") as f:
18+
with open(xml_asset_path(filename), "rb") as f:
1519
return f.read().decode("utf-8")
1620

1721

test/test_auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import os.path
21
import unittest
32

43
import requests_mock
54

65
import tableauserverclient as TSC
76

8-
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
7+
from test._utils import xml_asset_path
98

10-
SIGN_IN_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in.xml")
11-
SIGN_IN_IMPERSONATE_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in_impersonate.xml")
12-
SIGN_IN_ERROR_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in_error.xml")
9+
SIGN_IN_XML = xml_asset_path("auth_sign_in.xml")
10+
SIGN_IN_IMPERSONATE_XML = xml_asset_path("auth_sign_in_impersonate.xml")
11+
SIGN_IN_ERROR_XML = xml_asset_path("auth_sign_in_error.xml")
1312

1413

1514
class AuthTests(unittest.TestCase):

test/test_custom_view.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
from tableauserverclient.datetime_helpers import format_datetime
1313
from tableauserverclient.server.endpoint.exceptions import MissingRequiredFieldError
1414

15-
TEST_ASSET_DIR = Path(__file__).parent / "assets"
16-
17-
GET_XML = os.path.join(TEST_ASSET_DIR, "custom_view_get.xml")
18-
GET_XML_ID = os.path.join(TEST_ASSET_DIR, "custom_view_get_id.xml")
19-
POPULATE_PREVIEW_IMAGE = os.path.join(TEST_ASSET_DIR, "Sample View Image.png")
20-
CUSTOM_VIEW_UPDATE_XML = os.path.join(TEST_ASSET_DIR, "custom_view_update.xml")
21-
CUSTOM_VIEW_POPULATE_PDF = os.path.join(TEST_ASSET_DIR, "populate_pdf.pdf")
22-
CUSTOM_VIEW_POPULATE_CSV = os.path.join(TEST_ASSET_DIR, "populate_csv.csv")
23-
CUSTOM_VIEW_DOWNLOAD = TEST_ASSET_DIR / "custom_view_download.json"
24-
FILE_UPLOAD_INIT = TEST_ASSET_DIR / "fileupload_initialize.xml"
25-
FILE_UPLOAD_APPEND = TEST_ASSET_DIR / "fileupload_append.xml"
15+
from test._utils import data_asset_path, xml_asset_path
16+
17+
GET_XML = xml_asset_path("custom_view_get.xml")
18+
GET_XML_ID = xml_asset_path("custom_view_get_id.xml")
19+
POPULATE_PREVIEW_IMAGE = data_asset_path("Sample View Image.png")
20+
CUSTOM_VIEW_UPDATE_XML = xml_asset_path("custom_view_update.xml")
21+
CUSTOM_VIEW_POPULATE_PDF = data_asset_path("populate_pdf.pdf")
22+
CUSTOM_VIEW_POPULATE_CSV = data_asset_path("populate_csv.csv")
23+
CUSTOM_VIEW_DOWNLOAD = xml_asset_path("custom_view_download.json")
24+
FILE_UPLOAD_INIT = xml_asset_path("fileupload_initialize.xml")
25+
FILE_UPLOAD_APPEND = xml_asset_path("fileupload_append.xml")
2626

2727

2828
class CustomViewTests(unittest.TestCase):

test/test_data_freshness_policy.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import unittest
44

55
import tableauserverclient as TSC
6-
7-
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
8-
9-
UPDATE_DFP_ALWAYS_LIVE_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy.xml")
10-
UPDATE_DFP_SITE_DEFAULT_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy2.xml")
11-
UPDATE_DFP_FRESH_EVERY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy3.xml")
12-
UPDATE_DFP_FRESH_AT_DAILY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy4.xml")
13-
UPDATE_DFP_FRESH_AT_WEEKLY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy5.xml")
14-
UPDATE_DFP_FRESH_AT_MONTHLY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy6.xml")
6+
from test._utils import xml_asset_path
7+
8+
UPDATE_DFP_ALWAYS_LIVE_XML = xml_asset_path("workbook_update_data_freshness_policy.xml")
9+
UPDATE_DFP_SITE_DEFAULT_XML = xml_asset_path("workbook_update_data_freshness_policy2.xml")
10+
UPDATE_DFP_FRESH_EVERY_XML = xml_asset_path("workbook_update_data_freshness_policy3.xml")
11+
UPDATE_DFP_FRESH_AT_DAILY_XML = xml_asset_path("workbook_update_data_freshness_policy4.xml")
12+
UPDATE_DFP_FRESH_AT_WEEKLY_XML = xml_asset_path("workbook_update_data_freshness_policy5.xml")
13+
UPDATE_DFP_FRESH_AT_MONTHLY_XML = xml_asset_path("workbook_update_data_freshness_policy6.xml")
1514

1615

1716
class WorkbookTests(unittest.TestCase):

test/test_database.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import requests_mock
44

55
import tableauserverclient as TSC
6-
from ._utils import read_xml_asset, asset
6+
from ._utils import read_xml_asset
77

88
GET_XML = "database_get.xml"
99
POPULATE_PERMISSIONS_XML = "database_populate_permissions.xml"
@@ -85,8 +85,7 @@ def test_populate_permissions(self):
8585
)
8686

8787
def test_populate_data_quality_warning(self):
88-
with open(asset(GET_DQW_BY_CONTENT), "rb") as f:
89-
response_xml = f.read().decode("utf-8")
88+
response_xml = read_xml_asset(GET_DQW_BY_CONTENT)
9089
with requests_mock.mock() as m:
9190
m.get(
9291
self.server.databases._data_quality_warnings.baseurl + "/94441d26-9a52-4a42-b0fb-3f94792d1aac",

test/test_datasource.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from tableauserverclient.server.endpoint.exceptions import InternalServerError
1515
from tableauserverclient.server.endpoint.fileuploads_endpoint import Fileuploads
1616
from tableauserverclient.server.request_factory import RequestFactory
17-
from ._utils import read_xml_asset, read_xml_assets, asset
17+
from ._utils import data_asset_path, read_xml_asset, read_xml_assets, xml_asset_path
1818

1919
ADD_TAGS_XML = "datasource_add_tags.xml"
2020
GET_XML = "datasource_get.xml"
@@ -135,7 +135,7 @@ def test_update(self) -> None:
135135
self.assertEqual(updated_datasource.certification_note, single_datasource.certification_note)
136136

137137
def test_update_copy_fields(self) -> None:
138-
with open(asset(UPDATE_XML), "rb") as f:
138+
with open(xml_asset_path(UPDATE_XML), "rb") as f:
139139
response_xml = f.read().decode("utf-8")
140140
with requests_mock.mock() as m:
141141
m.put(self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", text=response_xml)
@@ -218,7 +218,7 @@ def test_update_connection(self) -> None:
218218
self.assertEqual("foo", new_connection.username)
219219

220220
def test_populate_permissions(self) -> None:
221-
with open(asset(POPULATE_PERMISSIONS_XML), "rb") as f:
221+
with open(xml_asset_path(POPULATE_PERMISSIONS_XML), "rb") as f:
222222
response_xml = f.read().decode("utf-8")
223223
with requests_mock.mock() as m:
224224
m.get(self.baseurl + "/0448d2ed-590d-4fa0-b272-a2a8a24555b5/permissions", text=response_xml)
@@ -256,7 +256,9 @@ def test_publish(self) -> None:
256256
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "SampleDS")
257257
publish_mode = self.server.PublishMode.CreateNew
258258

259-
new_datasource = self.server.datasources.publish(new_datasource, asset("SampleDS.tds"), mode=publish_mode)
259+
new_datasource = self.server.datasources.publish(
260+
new_datasource, data_asset_path("SampleDS.tds"), mode=publish_mode
261+
)
260262

261263
self.assertEqual("e76a1461-3b1d-4588-bf1b-17551a879ad9", new_datasource.id)
262264
self.assertEqual("SampleDS", new_datasource.name)
@@ -275,7 +277,7 @@ def test_publish_a_non_packaged_file_object(self) -> None:
275277
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "SampleDS")
276278
publish_mode = self.server.PublishMode.CreateNew
277279

278-
with open(asset("SampleDS.tds"), "rb") as file_object:
280+
with open(data_asset_path("SampleDS.tds"), "rb") as file_object:
279281
new_datasource = self.server.datasources.publish(new_datasource, file_object, mode=publish_mode)
280282

281283
self.assertEqual("e76a1461-3b1d-4588-bf1b-17551a879ad9", new_datasource.id)
@@ -298,7 +300,7 @@ def test_publish_a_packaged_file_object(self) -> None:
298300
# Create a dummy tdsx file in memory
299301
with BytesIO() as zip_archive:
300302
with ZipFile(zip_archive, "w") as zf:
301-
zf.write(asset("SampleDS.tds"))
303+
zf.write(data_asset_path("SampleDS.tds"))
302304

303305
zip_archive.seek(0)
304306

@@ -324,7 +326,7 @@ def test_publish_async(self) -> None:
324326
publish_mode = self.server.PublishMode.CreateNew
325327

326328
new_job = self.server.datasources.publish(
327-
new_datasource, asset("SampleDS.tds"), mode=publish_mode, as_job=True
329+
new_datasource, data_asset_path("SampleDS.tds"), mode=publish_mode, as_job=True
328330
)
329331

330332
self.assertEqual("9a373058-af5f-4f83-8662-98b3e0228a73", new_job.id)
@@ -337,7 +339,7 @@ def test_publish_unnamed_file_object(self) -> None:
337339
new_datasource = TSC.DatasourceItem("test")
338340
publish_mode = self.server.PublishMode.CreateNew
339341

340-
with open(asset("SampleDS.tds"), "rb") as file_object:
342+
with open(data_asset_path("SampleDS.tds"), "rb") as file_object:
341343
self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, file_object, publish_mode)
342344

343345
def test_refresh_id(self) -> None:
@@ -466,7 +468,7 @@ def test_update_hyper_data_datasource_payload_file(self) -> None:
466468
text=response_xml,
467469
)
468470
new_job = self.server.datasources.update_hyper_data(
469-
datasource_id, request_id="test_id", actions=[], payload=asset("World Indicators.hyper")
471+
datasource_id, request_id="test_id", actions=[], payload=data_asset_path("World Indicators.hyper")
470472
)
471473

472474
# We only check the `id`; remaining fields are already tested in `test_update_hyper_data_datasource_object`
@@ -551,28 +553,30 @@ def test_publish_missing_path(self) -> None:
551553

552554
def test_publish_missing_mode(self) -> None:
553555
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
554-
self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, asset("SampleDS.tds"), None)
556+
self.assertRaises(
557+
ValueError, self.server.datasources.publish, new_datasource, data_asset_path("SampleDS.tds"), None
558+
)
555559

556560
def test_publish_invalid_file_type(self) -> None:
557561
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
558562
self.assertRaises(
559563
ValueError,
560564
self.server.datasources.publish,
561565
new_datasource,
562-
asset("SampleWB.twbx"),
566+
data_asset_path("SampleWB.twbx"),
563567
self.server.PublishMode.Append,
564568
)
565569

566570
def test_publish_hyper_file_object_raises_exception(self) -> None:
567571
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
568-
with open(asset("World Indicators.hyper"), "rb") as file_object:
572+
with open(data_asset_path("World Indicators.hyper"), "rb") as file_object:
569573
self.assertRaises(
570574
ValueError, self.server.datasources.publish, new_datasource, file_object, self.server.PublishMode.Append
571575
)
572576

573577
def test_publish_tde_file_object_raises_exception(self) -> None:
574578
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
575-
tds_asset = asset(os.path.join("Data", "Tableau Samples", "World Indicators.tde"))
579+
tds_asset = data_asset_path("World Indicators.tde")
576580
with open(tds_asset, "rb") as file_object:
577581
self.assertRaises(
578582
ValueError, self.server.datasources.publish, new_datasource, file_object, self.server.PublishMode.Append
@@ -645,7 +649,7 @@ def test_synchronous_publish_timeout_error(self) -> None:
645649
"Please use asynchronous publishing to avoid timeouts.",
646650
self.server.datasources.publish,
647651
new_datasource,
648-
asset("SampleDS.tds"),
652+
data_asset_path("SampleDS.tds"),
649653
publish_mode,
650654
)
651655

test/test_filesys_helpers.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from zipfile import ZipFile
66

77
from tableauserverclient.filesys_helpers import get_file_object_size, get_file_type
8-
from ._utils import asset, TEST_ASSET_DIR
8+
from test._utils import data_asset_path
99

1010

1111
class FilesysTests(unittest.TestCase):
@@ -26,7 +26,7 @@ def test_get_file_size_returns_zero_for_empty_file(self):
2626
self.assertEqual(file_size, 0)
2727

2828
def test_get_file_size_coincides_with_built_in_method(self):
29-
asset_path = asset("SampleWB.twbx")
29+
asset_path = data_asset_path("SampleWB.twbx")
3030
target_size = os.path.getsize(asset_path)
3131
with open(asset_path, "rb") as f:
3232
file_size = get_file_object_size(f)
@@ -45,12 +45,12 @@ def test_get_file_type_identifies_a_zip_file(self):
4545
self.assertEqual(file_type, "zip")
4646

4747
def test_get_file_type_identifies_tdsx_as_zip_file(self):
48-
with open(asset("World Indicators.tdsx"), "rb") as file_object:
48+
with open(data_asset_path("World Indicators.tdsx"), "rb") as file_object:
4949
file_type = get_file_type(file_object)
5050
self.assertEqual(file_type, "zip")
5151

5252
def test_get_file_type_identifies_twbx_as_zip_file(self):
53-
with open(asset("SampleWB.twbx"), "rb") as file_object:
53+
with open(data_asset_path("SampleWB.twbx"), "rb") as file_object:
5454
file_type = get_file_type(file_object)
5555
self.assertEqual(file_type, "zip")
5656

@@ -69,23 +69,22 @@ def test_get_file_type_identifies_xml_file(self):
6969
self.assertEqual(file_type, "xml")
7070

7171
def test_get_file_type_identifies_tds_as_xml_file(self):
72-
with open(asset("World Indicators.tds"), "rb") as file_object:
72+
with open(data_asset_path("World Indicators.tds"), "rb") as file_object:
7373
file_type = get_file_type(file_object)
7474
self.assertEqual(file_type, "xml")
7575

7676
def test_get_file_type_identifies_twb_as_xml_file(self):
77-
with open(asset("RESTAPISample.twb"), "rb") as file_object:
77+
with open(data_asset_path("RESTAPISample.twb"), "rb") as file_object:
7878
file_type = get_file_type(file_object)
7979
self.assertEqual(file_type, "xml")
8080

8181
def test_get_file_type_identifies_hyper_file(self):
82-
with open(asset("World Indicators.hyper"), "rb") as file_object:
82+
with open(data_asset_path("World Indicators.hyper"), "rb") as file_object:
8383
file_type = get_file_type(file_object)
8484
self.assertEqual(file_type, "hyper")
8585

8686
def test_get_file_type_identifies_tde_file(self):
87-
asset_path = os.path.join(TEST_ASSET_DIR, "Data", "Tableau Samples", "World Indicators.tde")
88-
with open(asset_path, "rb") as file_object:
87+
with open(data_asset_path("World Indicators.tde"), "rb") as file_object:
8988
file_type = get_file_type(file_object)
9089
self.assertEqual(file_type, "tde")
9190

test/test_fileuploads.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
from tableauserverclient.config import BYTES_PER_MB, config
99
from tableauserverclient.server import Server
10-
from ._utils import asset
10+
from test._utils import xml_asset_path, data_asset_path
1111

12-
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
13-
FILEUPLOAD_INITIALIZE = os.path.join(TEST_ASSET_DIR, "fileupload_initialize.xml")
14-
FILEUPLOAD_APPEND = os.path.join(TEST_ASSET_DIR, "fileupload_append.xml")
12+
FILEUPLOAD_INITIALIZE = xml_asset_path("fileupload_initialize.xml")
13+
FILEUPLOAD_APPEND = xml_asset_path("fileupload_append.xml")
1514

1615

1716
@contextlib.contextmanager
@@ -36,19 +35,19 @@ def setUp(self):
3635
self.baseurl = f"{self.server.baseurl}/sites/{self.server.site_id}/fileUploads"
3736

3837
def test_read_chunks_file_path(self):
39-
file_path = asset("SampleWB.twbx")
38+
file_path = data_asset_path("SampleWB.twbx")
4039
chunks = self.server.fileuploads._read_chunks(file_path)
4140
for chunk in chunks:
4241
self.assertIsNotNone(chunk)
4342

4443
def test_read_chunks_file_object(self):
45-
with open(asset("SampleWB.twbx"), "rb") as f:
44+
with open(data_asset_path("SampleWB.twbx"), "rb") as f:
4645
chunks = self.server.fileuploads._read_chunks(f)
4746
for chunk in chunks:
4847
self.assertIsNotNone(chunk)
4948

5049
def test_upload_chunks_file_path(self):
51-
file_path = asset("SampleWB.twbx")
50+
file_path = data_asset_path("SampleWB.twbx")
5251
upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
5352

5453
with open(FILEUPLOAD_INITIALIZE, "rb") as f:
@@ -65,7 +64,7 @@ def test_upload_chunks_file_path(self):
6564
def test_upload_chunks_file_object(self):
6665
upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
6766

68-
with open(asset("SampleWB.twbx"), "rb") as file_content:
67+
with open(data_asset_path("SampleWB.twbx"), "rb") as file_content:
6968
with open(FILEUPLOAD_INITIALIZE, "rb") as f:
7069
initialize_response_xml = f.read().decode("utf-8")
7170
with open(FILEUPLOAD_APPEND, "rb") as f:

0 commit comments

Comments
 (0)