|
1 | 1 | import os |
2 | | -import unittest |
| 2 | +from pathlib import Path |
3 | 3 | from io import BytesIO |
4 | 4 | from xml.etree import ElementTree as ET |
5 | 5 | from zipfile import ZipFile |
6 | 6 |
|
| 7 | +import pytest |
| 8 | + |
7 | 9 | from tableauserverclient.filesys_helpers import get_file_object_size, get_file_type |
8 | | -from ._utils import asset, TEST_ASSET_DIR |
9 | | - |
10 | | - |
11 | | -class FilesysTests(unittest.TestCase): |
12 | | - def test_get_file_size_returns_correct_size(self): |
13 | | - target_size = 1000 # bytes |
14 | | - |
15 | | - with BytesIO() as f: |
16 | | - f.seek(target_size - 1) |
17 | | - f.write(b"\0") |
18 | | - file_size = get_file_object_size(f) |
19 | | - |
20 | | - self.assertEqual(file_size, target_size) |
21 | | - |
22 | | - def test_get_file_size_returns_zero_for_empty_file(self): |
23 | | - with BytesIO() as f: |
24 | | - file_size = get_file_object_size(f) |
25 | | - |
26 | | - self.assertEqual(file_size, 0) |
27 | | - |
28 | | - def test_get_file_size_coincides_with_built_in_method(self): |
29 | | - asset_path = asset("SampleWB.twbx") |
30 | | - target_size = os.path.getsize(asset_path) |
31 | | - with open(asset_path, "rb") as f: |
32 | | - file_size = get_file_object_size(f) |
33 | | - |
34 | | - self.assertEqual(file_size, target_size) |
35 | | - |
36 | | - def test_get_file_type_identifies_a_zip_file(self): |
37 | | - with BytesIO() as file_object: |
38 | | - with ZipFile(file_object, "w") as zf: |
39 | | - with BytesIO() as stream: |
40 | | - stream.write(b"This is a zip file") |
41 | | - zf.writestr("dummy_file", stream.getbuffer()) |
42 | | - file_object.seek(0) |
43 | | - file_type = get_file_type(file_object) |
44 | | - |
45 | | - self.assertEqual(file_type, "zip") |
46 | | - |
47 | | - def test_get_file_type_identifies_tdsx_as_zip_file(self): |
48 | | - with open(asset("World Indicators.tdsx"), "rb") as file_object: |
49 | | - file_type = get_file_type(file_object) |
50 | | - self.assertEqual(file_type, "zip") |
51 | | - |
52 | | - def test_get_file_type_identifies_twbx_as_zip_file(self): |
53 | | - with open(asset("SampleWB.twbx"), "rb") as file_object: |
54 | | - file_type = get_file_type(file_object) |
55 | | - self.assertEqual(file_type, "zip") |
56 | | - |
57 | | - def test_get_file_type_identifies_xml_file(self): |
58 | | - root = ET.Element("root") |
59 | | - child = ET.SubElement(root, "child") |
60 | | - child.text = "This is a child element" |
61 | | - etree = ET.ElementTree(root) |
62 | | - |
63 | | - with BytesIO() as file_object: |
64 | | - etree.write(file_object, encoding="utf-8", xml_declaration=True) |
65 | | - |
66 | | - file_object.seek(0) |
67 | | - file_type = get_file_type(file_object) |
68 | | - |
69 | | - self.assertEqual(file_type, "xml") |
70 | | - |
71 | | - def test_get_file_type_identifies_tds_as_xml_file(self): |
72 | | - with open(asset("World Indicators.tds"), "rb") as file_object: |
73 | | - file_type = get_file_type(file_object) |
74 | | - self.assertEqual(file_type, "xml") |
75 | | - |
76 | | - def test_get_file_type_identifies_twb_as_xml_file(self): |
77 | | - with open(asset("RESTAPISample.twb"), "rb") as file_object: |
78 | | - file_type = get_file_type(file_object) |
79 | | - self.assertEqual(file_type, "xml") |
80 | | - |
81 | | - def test_get_file_type_identifies_hyper_file(self): |
82 | | - with open(asset("World Indicators.hyper"), "rb") as file_object: |
83 | | - file_type = get_file_type(file_object) |
84 | | - self.assertEqual(file_type, "hyper") |
85 | | - |
86 | | - 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: |
89 | | - file_type = get_file_type(file_object) |
90 | | - self.assertEqual(file_type, "tde") |
91 | | - |
92 | | - def test_get_file_type_handles_unknown_file_type(self): |
93 | | - # Create a dummy png file |
94 | | - with BytesIO() as file_object: |
95 | | - png_signature = bytes.fromhex("89504E470D0A1A0A") |
96 | | - file_object.write(png_signature) |
97 | | - file_object.seek(0) |
98 | | - |
99 | | - self.assertRaises(ValueError, get_file_type, file_object) |
| 10 | + |
| 11 | +TEST_ASSET_DIR = Path(__file__).parent / "assets" |
| 12 | + |
| 13 | + |
| 14 | +def test_get_file_size_returns_correct_size() -> None: |
| 15 | + target_size = 1000 # bytes |
| 16 | + |
| 17 | + with BytesIO() as f: |
| 18 | + f.seek(target_size - 1) |
| 19 | + f.write(b"\0") |
| 20 | + file_size = get_file_object_size(f) |
| 21 | + |
| 22 | + assert file_size == target_size |
| 23 | + |
| 24 | + |
| 25 | +def test_get_file_size_returns_zero_for_empty_file() -> None: |
| 26 | + with BytesIO() as f: |
| 27 | + file_size = get_file_object_size(f) |
| 28 | + |
| 29 | + assert file_size == 0 |
| 30 | + |
| 31 | + |
| 32 | +def test_get_file_size_coincides_with_built_in_method() -> None: |
| 33 | + asset_path = TEST_ASSET_DIR / "SampleWB.twbx" |
| 34 | + target_size = os.path.getsize(asset_path) |
| 35 | + with open(asset_path, "rb") as f: |
| 36 | + file_size = get_file_object_size(f) |
| 37 | + |
| 38 | + assert file_size == target_size |
| 39 | + |
| 40 | + |
| 41 | +def test_get_file_type_identifies_a_zip_file() -> None: |
| 42 | + with BytesIO() as file_object: |
| 43 | + with ZipFile(file_object, "w") as zf: |
| 44 | + with BytesIO() as stream: |
| 45 | + stream.write(b"This is a zip file") |
| 46 | + zf.writestr("dummy_file", stream.getbuffer()) |
| 47 | + file_object.seek(0) |
| 48 | + file_type = get_file_type(file_object) |
| 49 | + |
| 50 | + assert file_type == "zip" |
| 51 | + |
| 52 | + |
| 53 | +def test_get_file_type_identifies_tdsx_as_zip_file() -> None: |
| 54 | + with open(TEST_ASSET_DIR / "World Indicators.tdsx", "rb") as file_object: |
| 55 | + file_type = get_file_type(file_object) |
| 56 | + assert file_type == "zip" |
| 57 | + |
| 58 | + |
| 59 | +def test_get_file_type_identifies_twbx_as_zip_file() -> None: |
| 60 | + with open(TEST_ASSET_DIR / "SampleWB.twbx", "rb") as file_object: |
| 61 | + file_type = get_file_type(file_object) |
| 62 | + assert file_type == "zip" |
| 63 | + |
| 64 | + |
| 65 | +def test_get_file_type_identifies_xml_file() -> None: |
| 66 | + root = ET.Element("root") |
| 67 | + child = ET.SubElement(root, "child") |
| 68 | + child.text = "This is a child element" |
| 69 | + etree = ET.ElementTree(root) |
| 70 | + |
| 71 | + with BytesIO() as file_object: |
| 72 | + etree.write(file_object, encoding="utf-8", xml_declaration=True) |
| 73 | + |
| 74 | + file_object.seek(0) |
| 75 | + file_type = get_file_type(file_object) |
| 76 | + |
| 77 | + assert file_type == "xml" |
| 78 | + |
| 79 | + |
| 80 | +def test_get_file_type_identifies_tds_as_xml_file() -> None: |
| 81 | + with open(TEST_ASSET_DIR / "World Indicators.tds", "rb") as file_object: |
| 82 | + file_type = get_file_type(file_object) |
| 83 | + assert file_type == "xml" |
| 84 | + |
| 85 | + |
| 86 | +def test_get_file_type_identifies_twb_as_xml_file() -> None: |
| 87 | + with open(TEST_ASSET_DIR / "RESTAPISample.twb", "rb") as file_object: |
| 88 | + file_type = get_file_type(file_object) |
| 89 | + assert file_type == "xml" |
| 90 | + |
| 91 | + |
| 92 | +def test_get_file_type_identifies_hyper_file() -> None: |
| 93 | + with open(TEST_ASSET_DIR / "World Indicators.hyper", "rb") as file_object: |
| 94 | + file_type = get_file_type(file_object) |
| 95 | + assert file_type == "hyper" |
| 96 | + |
| 97 | + |
| 98 | +def test_get_file_type_identifies_tde_file() -> None: |
| 99 | + asset_path = TEST_ASSET_DIR / "Data" / "Tableau Samples" / "World Indicators.tde" |
| 100 | + with open(asset_path, "rb") as file_object: |
| 101 | + file_type = get_file_type(file_object) |
| 102 | + assert file_type == "tde" |
| 103 | + |
| 104 | + |
| 105 | +def test_get_file_type_handles_unknown_file_type() -> None: |
| 106 | + # Create a dummy png file |
| 107 | + with BytesIO() as file_object: |
| 108 | + png_signature = bytes.fromhex("89504E470D0A1A0A") |
| 109 | + file_object.write(png_signature) |
| 110 | + file_object.seek(0) |
| 111 | + |
| 112 | + with pytest.raises(ValueError): |
| 113 | + get_file_type(file_object) |
0 commit comments