From 10613091d3da1b178ed6dab8b006d1cb62756789 Mon Sep 17 00:00:00 2001 From: Jordan Woods <13803242+jorwoods@users.noreply.github.com> Date: Thu, 20 Nov 2025 22:13:28 -0600 Subject: [PATCH 1/2] fix: black ci errors --- test/test_datasource.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_datasource.py b/test/test_datasource.py index 7f4cca759..56eb11ab7 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -895,7 +895,8 @@ def test_publish_description(server: TSC.Server) -> None: ds_elem = body.find(".//datasource") assert ds_elem is not None assert ds_elem.attrib["description"] == "Sample description" - + + def test_get_datasource_no_owner(server: TSC.Server) -> None: with requests_mock.mock() as m: m.get(server.datasources.baseurl, text=GET_NO_OWNER.read_text()) From b551cf814f351d20c4fcf2ea1fbdd5a977b9a115 Mon Sep 17 00:00:00 2001 From: Jordan Woods <13803242+jorwoods@users.noreply.github.com> Date: Mon, 22 Dec 2025 08:07:06 -0600 Subject: [PATCH 2/2] chore: pytestify filesys helpers --- test/test_filesys_helpers.py | 200 +++++++++++++++++++---------------- 1 file changed, 107 insertions(+), 93 deletions(-) diff --git a/test/test_filesys_helpers.py b/test/test_filesys_helpers.py index 0f3234d5d..aa31ae98a 100644 --- a/test/test_filesys_helpers.py +++ b/test/test_filesys_helpers.py @@ -1,99 +1,113 @@ import os -import unittest +from pathlib import Path from io import BytesIO from xml.etree import ElementTree as ET from zipfile import ZipFile +import pytest + from tableauserverclient.filesys_helpers import get_file_object_size, get_file_type -from ._utils import asset, TEST_ASSET_DIR - - -class FilesysTests(unittest.TestCase): - def test_get_file_size_returns_correct_size(self): - target_size = 1000 # bytes - - with BytesIO() as f: - f.seek(target_size - 1) - f.write(b"\0") - file_size = get_file_object_size(f) - - self.assertEqual(file_size, target_size) - - def test_get_file_size_returns_zero_for_empty_file(self): - with BytesIO() as f: - file_size = get_file_object_size(f) - - self.assertEqual(file_size, 0) - - def test_get_file_size_coincides_with_built_in_method(self): - asset_path = asset("SampleWB.twbx") - target_size = os.path.getsize(asset_path) - with open(asset_path, "rb") as f: - file_size = get_file_object_size(f) - - self.assertEqual(file_size, target_size) - - def test_get_file_type_identifies_a_zip_file(self): - with BytesIO() as file_object: - with ZipFile(file_object, "w") as zf: - with BytesIO() as stream: - stream.write(b"This is a zip file") - zf.writestr("dummy_file", stream.getbuffer()) - file_object.seek(0) - file_type = get_file_type(file_object) - - 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: - 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: - file_type = get_file_type(file_object) - self.assertEqual(file_type, "zip") - - def test_get_file_type_identifies_xml_file(self): - root = ET.Element("root") - child = ET.SubElement(root, "child") - child.text = "This is a child element" - etree = ET.ElementTree(root) - - with BytesIO() as file_object: - etree.write(file_object, encoding="utf-8", xml_declaration=True) - - file_object.seek(0) - file_type = get_file_type(file_object) - - 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: - 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: - 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: - 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: - file_type = get_file_type(file_object) - self.assertEqual(file_type, "tde") - - def test_get_file_type_handles_unknown_file_type(self): - # Create a dummy png file - with BytesIO() as file_object: - png_signature = bytes.fromhex("89504E470D0A1A0A") - file_object.write(png_signature) - file_object.seek(0) - - self.assertRaises(ValueError, get_file_type, file_object) + +TEST_ASSET_DIR = Path(__file__).parent / "assets" + + +def test_get_file_size_returns_correct_size() -> None: + target_size = 1000 # bytes + + with BytesIO() as f: + f.seek(target_size - 1) + f.write(b"\0") + file_size = get_file_object_size(f) + + assert file_size == target_size + + +def test_get_file_size_returns_zero_for_empty_file() -> None: + with BytesIO() as f: + file_size = get_file_object_size(f) + + assert file_size == 0 + + +def test_get_file_size_coincides_with_built_in_method() -> None: + asset_path = TEST_ASSET_DIR / "SampleWB.twbx" + target_size = os.path.getsize(asset_path) + with open(asset_path, "rb") as f: + file_size = get_file_object_size(f) + + assert file_size == target_size + + +def test_get_file_type_identifies_a_zip_file() -> None: + with BytesIO() as file_object: + with ZipFile(file_object, "w") as zf: + with BytesIO() as stream: + stream.write(b"This is a zip file") + zf.writestr("dummy_file", stream.getbuffer()) + file_object.seek(0) + file_type = get_file_type(file_object) + + assert file_type == "zip" + + +def test_get_file_type_identifies_tdsx_as_zip_file() -> None: + with open(TEST_ASSET_DIR / "World Indicators.tdsx", "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "zip" + + +def test_get_file_type_identifies_twbx_as_zip_file() -> None: + with open(TEST_ASSET_DIR / "SampleWB.twbx", "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "zip" + + +def test_get_file_type_identifies_xml_file() -> None: + root = ET.Element("root") + child = ET.SubElement(root, "child") + child.text = "This is a child element" + etree = ET.ElementTree(root) + + with BytesIO() as file_object: + etree.write(file_object, encoding="utf-8", xml_declaration=True) + + file_object.seek(0) + file_type = get_file_type(file_object) + + assert file_type == "xml" + + +def test_get_file_type_identifies_tds_as_xml_file() -> None: + with open(TEST_ASSET_DIR / "World Indicators.tds", "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "xml" + + +def test_get_file_type_identifies_twb_as_xml_file() -> None: + with open(TEST_ASSET_DIR / "RESTAPISample.twb", "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "xml" + + +def test_get_file_type_identifies_hyper_file() -> None: + with open(TEST_ASSET_DIR / "World Indicators.hyper", "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "hyper" + + +def test_get_file_type_identifies_tde_file() -> None: + asset_path = TEST_ASSET_DIR / "Data" / "Tableau Samples" / "World Indicators.tde" + with open(asset_path, "rb") as file_object: + file_type = get_file_type(file_object) + assert file_type == "tde" + + +def test_get_file_type_handles_unknown_file_type() -> None: + # Create a dummy png file + with BytesIO() as file_object: + png_signature = bytes.fromhex("89504E470D0A1A0A") + file_object.write(png_signature) + file_object.seek(0) + + with pytest.raises(ValueError): + get_file_type(file_object)