Skip to content

Centralise test file references #1614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions test/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")


def asset(filename):
# should rename this because json files are also in the responses directory
def xml_asset_path(filename):
return os.path.join(TEST_ASSET_DIR, filename)


def data_asset_path(filename):
return os.path.join(TEST_ASSET_DIR, "Data", filename)


def read_xml_asset(filename):
with open(asset(filename), "rb") as f:
with open(xml_asset_path(filename), "rb") as f:
return f.read().decode("utf-8")


Expand Down
7,146 changes: 3,573 additions & 3,573 deletions test/assets/RESTAPISample.twb → test/assets/Data/RESTAPISample.twb

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions test/assets/Data/populate_csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Measure Names,Region,Profit Ratio,Sales per Customer,Distinct count of Customer Name,Measure Values,Profit,Quantity,Sales
Count of Customers,South,14.4%,$711.83,438,438,"$45,047","5,004","$311,784"
Sales,South,14.4%,$711.83,438,"311,783.644","$45,047","5,004","$311,784"
Quantity,South,14.4%,$711.83,438,"5,004","$45,047","5,004","$311,784"
Sales per Customer,South,14.4%,$711.83,438,711.834803653,"$45,047","5,004","$311,784"
Profit,South,14.4%,$711.83,438,"45,047.2231","$45,047","5,004","$311,784"
Profit Ratio,South,14.4%,$711.83,438,0.144482316,"$45,047","5,004","$311,784"
Count of Customers,Central,9.3%,$746.66,566,566,"$39,176","6,990","$422,611"
Sales,Central,9.3%,$746.66,566,"422,610.558800001","$39,176","6,990","$422,611"
Quantity,Central,9.3%,$746.66,566,"6,990","$39,176","6,990","$422,611"
Sales per Customer,Central,9.3%,$746.66,566,746.661764664,"$39,176","6,990","$422,611"
Profit,Central,9.3%,$746.66,566,"39,176.1836","$39,176","6,990","$422,611"
Profit Ratio,Central,9.3%,$746.66,566,0.092700437,"$39,176","6,990","$422,611"
Count of Customers,East,12.7%,$825.74,624,624,"$65,476","8,255","$515,262"
Sales,East,12.7%,$825.74,624,"515,261.598000001","$65,476","8,255","$515,262"
Quantity,East,12.7%,$825.74,624,"8,255","$65,476","8,255","$515,262"
Sales per Customer,East,12.7%,$825.74,624,825.739740385,"$65,476","8,255","$515,262"
Profit,East,12.7%,$825.74,624,"65,475.852700000","$65,476","8,255","$515,262"
Profit Ratio,East,12.7%,$825.74,624,0.127073030,"$65,476","8,255","$515,262"
Count of Customers,West,14.4%,$906.73,630,630,"$82,264","9,544","$571,239"
Sales,West,14.4%,$906.73,630,"571,239.036500001","$82,264","9,544","$571,239"
Quantity,West,14.4%,$906.73,630,"9,544","$82,264","9,544","$571,239"
Sales per Customer,West,14.4%,$906.73,630,906.728629365,"$82,264","9,544","$571,239"
Profit,West,14.4%,$906.73,630,"82,263.903800000","$82,264","9,544","$571,239"
Profit Ratio,West,14.4%,$906.73,630,0.144009598,"$82,264","9,544","$571,239"
Binary file added test/assets/Data/populate_excel.xlsx
Binary file not shown.
Binary file added test/assets/Data/populate_pdf.pdf
Binary file not shown.
Binary file added test/assets/Data/populate_powerpoint.pptx
Binary file not shown.
9 changes: 4 additions & 5 deletions test/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import os.path
import unittest

import requests_mock

import tableauserverclient as TSC

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
from test._utils import xml_asset_path

SIGN_IN_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in.xml")
SIGN_IN_IMPERSONATE_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in_impersonate.xml")
SIGN_IN_ERROR_XML = os.path.join(TEST_ASSET_DIR, "auth_sign_in_error.xml")
SIGN_IN_XML = xml_asset_path("auth_sign_in.xml")
SIGN_IN_IMPERSONATE_XML = xml_asset_path("auth_sign_in_impersonate.xml")
SIGN_IN_ERROR_XML = xml_asset_path("auth_sign_in_error.xml")


class AuthTests(unittest.TestCase):
Expand Down
22 changes: 11 additions & 11 deletions test/test_custom_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
from tableauserverclient.datetime_helpers import format_datetime
from tableauserverclient.server.endpoint.exceptions import MissingRequiredFieldError

TEST_ASSET_DIR = Path(__file__).parent / "assets"

GET_XML = os.path.join(TEST_ASSET_DIR, "custom_view_get.xml")
GET_XML_ID = os.path.join(TEST_ASSET_DIR, "custom_view_get_id.xml")
POPULATE_PREVIEW_IMAGE = os.path.join(TEST_ASSET_DIR, "Sample View Image.png")
CUSTOM_VIEW_UPDATE_XML = os.path.join(TEST_ASSET_DIR, "custom_view_update.xml")
CUSTOM_VIEW_POPULATE_PDF = os.path.join(TEST_ASSET_DIR, "populate_pdf.pdf")
CUSTOM_VIEW_POPULATE_CSV = os.path.join(TEST_ASSET_DIR, "populate_csv.csv")
CUSTOM_VIEW_DOWNLOAD = TEST_ASSET_DIR / "custom_view_download.json"
FILE_UPLOAD_INIT = TEST_ASSET_DIR / "fileupload_initialize.xml"
FILE_UPLOAD_APPEND = TEST_ASSET_DIR / "fileupload_append.xml"
from test._utils import data_asset_path, xml_asset_path

GET_XML = xml_asset_path("custom_view_get.xml")
GET_XML_ID = xml_asset_path("custom_view_get_id.xml")
POPULATE_PREVIEW_IMAGE = data_asset_path("Sample View Image.png")
CUSTOM_VIEW_UPDATE_XML = xml_asset_path("custom_view_update.xml")
CUSTOM_VIEW_POPULATE_PDF = data_asset_path("populate_pdf.pdf")
CUSTOM_VIEW_POPULATE_CSV = data_asset_path("populate_csv.csv")
CUSTOM_VIEW_DOWNLOAD = Path(xml_asset_path("custom_view_download.json"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed some use Path from pathlib and some don't. Is that intentional?

FILE_UPLOAD_INIT = Path(xml_asset_path("fileupload_initialize.xml"))
FILE_UPLOAD_APPEND = Path(xml_asset_path("fileupload_append.xml"))


class CustomViewTests(unittest.TestCase):
Expand Down
17 changes: 8 additions & 9 deletions test/test_data_freshness_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import unittest

import tableauserverclient as TSC

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")

UPDATE_DFP_ALWAYS_LIVE_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy.xml")
UPDATE_DFP_SITE_DEFAULT_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy2.xml")
UPDATE_DFP_FRESH_EVERY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy3.xml")
UPDATE_DFP_FRESH_AT_DAILY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy4.xml")
UPDATE_DFP_FRESH_AT_WEEKLY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy5.xml")
UPDATE_DFP_FRESH_AT_MONTHLY_XML = os.path.join(TEST_ASSET_DIR, "workbook_update_data_freshness_policy6.xml")
from test._utils import xml_asset_path

UPDATE_DFP_ALWAYS_LIVE_XML = xml_asset_path("workbook_update_data_freshness_policy.xml")
UPDATE_DFP_SITE_DEFAULT_XML = xml_asset_path("workbook_update_data_freshness_policy2.xml")
UPDATE_DFP_FRESH_EVERY_XML = xml_asset_path("workbook_update_data_freshness_policy3.xml")
UPDATE_DFP_FRESH_AT_DAILY_XML = xml_asset_path("workbook_update_data_freshness_policy4.xml")
UPDATE_DFP_FRESH_AT_WEEKLY_XML = xml_asset_path("workbook_update_data_freshness_policy5.xml")
UPDATE_DFP_FRESH_AT_MONTHLY_XML = xml_asset_path("workbook_update_data_freshness_policy6.xml")


class WorkbookTests(unittest.TestCase):
Expand Down
8 changes: 3 additions & 5 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests_mock

import tableauserverclient as TSC
from ._utils import read_xml_asset, asset
from ._utils import read_xml_asset

GET_XML = "database_get.xml"
POPULATE_PERMISSIONS_XML = "database_populate_permissions.xml"
Expand Down Expand Up @@ -55,8 +55,7 @@ def test_update(self):
self.assertEqual("Test", single_database.certification_note)

def test_populate_permissions(self):
with open(asset(POPULATE_PERMISSIONS_XML), "rb") as f:
response_xml = f.read().decode("utf-8")
response_xml = read_xml_asset(POPULATE_PERMISSIONS_XML)
with requests_mock.mock() as m:
m.get(self.baseurl + "/0448d2ed-590d-4fa0-b272-a2a8a24555b5/permissions", text=response_xml)
single_database = TSC.DatabaseItem("test")
Expand Down Expand Up @@ -85,8 +84,7 @@ def test_populate_permissions(self):
)

def test_populate_data_quality_warning(self):
with open(asset(GET_DQW_BY_CONTENT), "rb") as f:
response_xml = f.read().decode("utf-8")
response_xml = read_xml_asset(GET_DQW_BY_CONTENT)
with requests_mock.mock() as m:
m.get(
self.server.databases._data_quality_warnings.baseurl + "/94441d26-9a52-4a42-b0fb-3f94792d1aac",
Expand Down
32 changes: 18 additions & 14 deletions test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tableauserverclient.server.endpoint.exceptions import InternalServerError
from tableauserverclient.server.endpoint.fileuploads_endpoint import Fileuploads
from tableauserverclient.server.request_factory import RequestFactory
from ._utils import read_xml_asset, read_xml_assets, asset
from ._utils import data_asset_path, read_xml_asset, read_xml_assets, xml_asset_path

ADD_TAGS_XML = "datasource_add_tags.xml"
GET_XML = "datasource_get.xml"
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_update(self) -> None:
self.assertEqual(updated_datasource.certification_note, single_datasource.certification_note)

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

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

new_datasource = self.server.datasources.publish(new_datasource, asset("SampleDS.tds"), mode=publish_mode)
new_datasource = self.server.datasources.publish(
new_datasource, data_asset_path("SampleDS.tds"), mode=publish_mode
)

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

with open(asset("SampleDS.tds"), "rb") as file_object:
with open(data_asset_path("SampleDS.tds"), "rb") as file_object:
new_datasource = self.server.datasources.publish(new_datasource, file_object, mode=publish_mode)

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

zip_archive.seek(0)

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

new_job = self.server.datasources.publish(
new_datasource, asset("SampleDS.tds"), mode=publish_mode, as_job=True
new_datasource, data_asset_path("SampleDS.tds"), mode=publish_mode, as_job=True
)

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

with open(asset("SampleDS.tds"), "rb") as file_object:
with open(data_asset_path("SampleDS.tds"), "rb") as file_object:
self.assertRaises(ValueError, self.server.datasources.publish, new_datasource, file_object, publish_mode)

def test_refresh_id(self) -> None:
Expand Down Expand Up @@ -466,7 +468,7 @@ def test_update_hyper_data_datasource_payload_file(self) -> None:
text=response_xml,
)
new_job = self.server.datasources.update_hyper_data(
datasource_id, request_id="test_id", actions=[], payload=asset("World Indicators.hyper")
datasource_id, request_id="test_id", actions=[], payload=data_asset_path("World Indicators.hyper")
)

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

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

def test_publish_invalid_file_type(self) -> None:
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
self.assertRaises(
ValueError,
self.server.datasources.publish,
new_datasource,
asset("SampleWB.twbx"),
data_asset_path("SampleWB.twbx"),
self.server.PublishMode.Append,
)

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

def test_publish_tde_file_object_raises_exception(self) -> None:
new_datasource = TSC.DatasourceItem("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", "test")
tds_asset = asset(os.path.join("Data", "Tableau Samples", "World Indicators.tde"))
tds_asset = data_asset_path("World Indicators.tde")
with open(tds_asset, "rb") as file_object:
self.assertRaises(
ValueError, self.server.datasources.publish, new_datasource, file_object, self.server.PublishMode.Append
Expand Down Expand Up @@ -645,7 +649,7 @@ def test_synchronous_publish_timeout_error(self) -> None:
"Please use asynchronous publishing to avoid timeouts.",
self.server.datasources.publish,
new_datasource,
asset("SampleDS.tds"),
data_asset_path("SampleDS.tds"),
publish_mode,
)

Expand Down
17 changes: 8 additions & 9 deletions test/test_filesys_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from zipfile import ZipFile

from tableauserverclient.filesys_helpers import get_file_object_size, get_file_type
from ._utils import asset, TEST_ASSET_DIR
from test._utils import data_asset_path


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

def test_get_file_size_coincides_with_built_in_method(self):
asset_path = asset("SampleWB.twbx")
asset_path = data_asset_path("SampleWB.twbx")
target_size = os.path.getsize(asset_path)
with open(asset_path, "rb") as f:
file_size = get_file_object_size(f)
Expand All @@ -45,12 +45,12 @@ def test_get_file_type_identifies_a_zip_file(self):
self.assertEqual(file_type, "zip")

def test_get_file_type_identifies_tdsx_as_zip_file(self):
with open(asset("World Indicators.tdsx"), "rb") as file_object:
with open(data_asset_path("World Indicators.tdsx"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "zip")

def test_get_file_type_identifies_twbx_as_zip_file(self):
with open(asset("SampleWB.twbx"), "rb") as file_object:
with open(data_asset_path("SampleWB.twbx"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "zip")

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

def test_get_file_type_identifies_tds_as_xml_file(self):
with open(asset("World Indicators.tds"), "rb") as file_object:
with open(data_asset_path("World Indicators.tds"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "xml")

def test_get_file_type_identifies_twb_as_xml_file(self):
with open(asset("RESTAPISample.twb"), "rb") as file_object:
with open(data_asset_path("RESTAPISample.twb"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "xml")

def test_get_file_type_identifies_hyper_file(self):
with open(asset("World Indicators.hyper"), "rb") as file_object:
with open(data_asset_path("World Indicators.hyper"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "hyper")

def test_get_file_type_identifies_tde_file(self):
asset_path = os.path.join(TEST_ASSET_DIR, "Data", "Tableau Samples", "World Indicators.tde")
with open(asset_path, "rb") as file_object:
with open(data_asset_path("World Indicators.tde"), "rb") as file_object:
file_type = get_file_type(file_object)
self.assertEqual(file_type, "tde")

Expand Down
15 changes: 7 additions & 8 deletions test/test_fileuploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

from tableauserverclient.config import BYTES_PER_MB, config
from tableauserverclient.server import Server
from ._utils import asset
from test._utils import xml_asset_path, data_asset_path

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
FILEUPLOAD_INITIALIZE = os.path.join(TEST_ASSET_DIR, "fileupload_initialize.xml")
FILEUPLOAD_APPEND = os.path.join(TEST_ASSET_DIR, "fileupload_append.xml")
FILEUPLOAD_INITIALIZE = xml_asset_path("fileupload_initialize.xml")
FILEUPLOAD_APPEND = xml_asset_path("fileupload_append.xml")


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

def test_read_chunks_file_path(self):
file_path = asset("SampleWB.twbx")
file_path = data_asset_path("SampleWB.twbx")
chunks = self.server.fileuploads._read_chunks(file_path)
for chunk in chunks:
self.assertIsNotNone(chunk)

def test_read_chunks_file_object(self):
with open(asset("SampleWB.twbx"), "rb") as f:
with open(data_asset_path("SampleWB.twbx"), "rb") as f:
chunks = self.server.fileuploads._read_chunks(f)
for chunk in chunks:
self.assertIsNotNone(chunk)

def test_upload_chunks_file_path(self):
file_path = asset("SampleWB.twbx")
file_path = data_asset_path("SampleWB.twbx")
upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"

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

with open(asset("SampleWB.twbx"), "rb") as file_content:
with open(data_asset_path("SampleWB.twbx"), "rb") as file_content:
with open(FILEUPLOAD_INITIALIZE, "rb") as f:
initialize_response_xml = f.read().decode("utf-8")
with open(FILEUPLOAD_APPEND, "rb") as f:
Expand Down
Loading
Loading