11import contextlib
22import io
33import os
4- import unittest
4+ from pathlib import Path
55
6+ import pytest
67import requests_mock
78
9+ import tableauserverclient as TSC
810from tableauserverclient .config import BYTES_PER_MB , config
9- from tableauserverclient .server import Server
10- from ._utils import asset
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+ TEST_ASSET_DIR = Path (__file__ ).parent / "assets"
13+ FILEUPLOAD_INITIALIZE = TEST_ASSET_DIR / "fileupload_initialize.xml"
14+ FILEUPLOAD_APPEND = TEST_ASSET_DIR / "fileupload_append.xml"
15+ SAMPLE_WB = TEST_ASSET_DIR / "SampleWB.twbx"
1516
1617
1718@contextlib .contextmanager
@@ -25,65 +26,67 @@ def set_env(**environ):
2526 os .environ .update (old_environ )
2627
2728
28- class FileuploadsTests (unittest .TestCase ):
29- def setUp (self ):
30- self .server = Server ("http://test" , False )
29+ @pytest .fixture (scope = "function" )
30+ def server ():
31+ """Fixture to create a TSC.Server instance for testing."""
32+ server = TSC .Server ("http://test" , False )
3133
32- # Fake sign in
33- self . server ._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67"
34- self . server ._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM"
34+ # Fake signin
35+ server ._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67"
36+ server ._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM"
3537
36- self . baseurl = f" { self . server . baseurl } /sites/ { self . server . site_id } /fileUploads"
38+ return server
3739
38- def test_read_chunks_file_path (self ):
39- file_path = asset ("SampleWB.twbx" )
40- chunks = self .server .fileuploads ._read_chunks (file_path )
40+
41+ def test_read_chunks_file_path (server : TSC .Server ) -> None :
42+ file_path = str (SAMPLE_WB )
43+ chunks = server .fileuploads ._read_chunks (file_path )
44+ for chunk in chunks :
45+ assert chunk is not None
46+
47+
48+ def test_read_chunks_file_object (server : TSC .Server ) -> None :
49+ with SAMPLE_WB .open ("rb" ) as f :
50+ chunks = server .fileuploads ._read_chunks (f )
4151 for chunk in chunks :
42- self .assertIsNotNone (chunk )
43-
44- def test_read_chunks_file_object (self ):
45- with open (asset ("SampleWB.twbx" ), "rb" ) as f :
46- chunks = self .server .fileuploads ._read_chunks (f )
47- for chunk in chunks :
48- self .assertIsNotNone (chunk )
49-
50- def test_upload_chunks_file_path (self ):
51- file_path = asset ("SampleWB.twbx" )
52- upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
53-
54- with open (FILEUPLOAD_INITIALIZE , "rb" ) as f :
55- initialize_response_xml = f .read ().decode ("utf-8" )
56- with open (FILEUPLOAD_APPEND , "rb" ) as f :
57- append_response_xml = f .read ().decode ("utf-8" )
52+ assert chunk is not None
53+
54+
55+ def test_upload_chunks_file_path (server : TSC .Server ) -> None :
56+ file_path = str (SAMPLE_WB )
57+ upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
58+
59+ initialize_response_xml = FILEUPLOAD_INITIALIZE .read_text ()
60+ append_response_xml = FILEUPLOAD_APPEND .read_text ()
61+ with requests_mock .mock () as m :
62+ m .post (server .fileuploads .baseurl , text = initialize_response_xml )
63+ m .put (f"{ server .fileuploads .baseurl } /{ upload_id } " , text = append_response_xml )
64+ actual = server .fileuploads .upload (file_path )
65+
66+ assert upload_id == actual
67+
68+
69+ def test_upload_chunks_file_object (server : TSC .Server ) -> None :
70+ upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
71+
72+ with SAMPLE_WB .open ("rb" ) as file_content :
73+ initialize_response_xml = FILEUPLOAD_INITIALIZE .read_text ()
74+ append_response_xml = FILEUPLOAD_APPEND .read_text ()
5875 with requests_mock .mock () as m :
59- m .post (self .baseurl , text = initialize_response_xml )
60- m .put (f"{ self .baseurl } /{ upload_id } " , text = append_response_xml )
61- actual = self .server .fileuploads .upload (file_path )
62-
63- self .assertEqual (upload_id , actual )
64-
65- def test_upload_chunks_file_object (self ):
66- upload_id = "7720:170fe6b1c1c7422dadff20f944d58a52-1:0"
67-
68- with open (asset ("SampleWB.twbx" ), "rb" ) as file_content :
69- with open (FILEUPLOAD_INITIALIZE , "rb" ) as f :
70- initialize_response_xml = f .read ().decode ("utf-8" )
71- with open (FILEUPLOAD_APPEND , "rb" ) as f :
72- append_response_xml = f .read ().decode ("utf-8" )
73- with requests_mock .mock () as m :
74- m .post (self .baseurl , text = initialize_response_xml )
75- m .put (f"{ self .baseurl } /{ upload_id } " , text = append_response_xml )
76- actual = self .server .fileuploads .upload (file_content )
77-
78- self .assertEqual (upload_id , actual )
79-
80- def test_upload_chunks_config (self ):
81- data = io .BytesIO ()
82- data .write (b"1" * (config .CHUNK_SIZE_MB * BYTES_PER_MB + 1 ))
76+ m .post (server .fileuploads .baseurl , text = initialize_response_xml )
77+ m .put (f"{ server .fileuploads .baseurl } /{ upload_id } " , text = append_response_xml )
78+ actual = server .fileuploads .upload (file_content )
79+
80+ assert upload_id == actual
81+
82+
83+ def test_upload_chunks_config (server : TSC .Server ) -> None :
84+ data = io .BytesIO ()
85+ data .write (b"1" * (config .CHUNK_SIZE_MB * BYTES_PER_MB + 1 ))
86+ data .seek (0 )
87+ with set_env (TSC_CHUNK_SIZE_MB = "1" ):
88+ chunker = server .fileuploads ._read_chunks (data )
89+ chunk = next (chunker )
90+ assert len (chunk ) == config .CHUNK_SIZE_MB * BYTES_PER_MB
8391 data .seek (0 )
84- with set_env (TSC_CHUNK_SIZE_MB = "1" ):
85- chunker = self .server .fileuploads ._read_chunks (data )
86- chunk = next (chunker )
87- assert len (chunk ) == config .CHUNK_SIZE_MB * BYTES_PER_MB
88- data .seek (0 )
89- assert len (chunk ) < len (data .read ())
92+ assert len (chunk ) < len (data .read ())
0 commit comments