1212from gcp_storage_emulator .server import create_server
1313from gcp_storage_emulator .settings import STORAGE_BASE , STORAGE_DIR
1414
15-
1615TEST_TEXT = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "test_text.txt" )
1716
1817
@@ -162,9 +161,7 @@ def tearDown(self):
162161 return super ().tearDown ()
163162
164163 def test_bucket_created (self ):
165- self ._server = create_server (
166- "localhost" , 9023 , in_memory = True , default_bucket = "example.appspot.com"
167- )
164+ self ._server = create_server ("localhost" , 9023 , in_memory = True , default_bucket = "example.appspot.com" )
168165 self ._server .start ()
169166 self ._session = requests .Session ()
170167 self ._client = _get_storage_client (self ._session )
@@ -198,9 +195,7 @@ def test_upload_from_text_file(self):
198195 self .assertEqual (read_content , expected_content )
199196
200197 def test_upload_from_bin_file (self ):
201- test_binary = os .path .join (
202- os .path .dirname (os .path .abspath (__file__ )), "test_binary.png"
203- )
198+ test_binary = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "test_binary.png" )
204199 bucket = self ._client .create_bucket ("testbucket" )
205200 blob = bucket .blob ("binary.png" )
206201 with open (test_binary , "rb" ) as file :
@@ -227,9 +222,7 @@ def test_upload_from_bin_file_cr_lf(self):
227222 self .assertEqual (read_content , content )
228223
229224 def test_upload_from_file_name (self ):
230- test_binary = os .path .join (
231- os .path .dirname (os .path .abspath (__file__ )), "test_binary.png"
232- )
225+ test_binary = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "test_binary.png" )
233226 file_name = "test_binary.png"
234227
235228 bucket = self ._client .create_bucket ("testbucket" )
@@ -242,9 +235,7 @@ def test_upload_from_file_name(self):
242235 self .assertEqual (temp_file .read (), orig_file .read ())
243236
244237 def test_upload_from_file (self ):
245- test_binary = os .path .join (
246- os .path .dirname (os .path .abspath (__file__ )), "test_binary.png"
247- )
238+ test_binary = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "test_binary.png" )
248239 file_name = "test_binary.png"
249240
250241 bucket = self ._client .create_bucket ("testbucket" )
@@ -463,9 +454,7 @@ def test_invalid_crc32c_hash(self):
463454 blob .upload_from_string (content )
464455
465456 def test_download_binary_to_file (self ):
466- test_binary = os .path .join (
467- os .path .dirname (os .path .abspath (__file__ )), "test_binary.png"
468- )
457+ test_binary = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "test_binary.png" )
469458 bucket = self ._client .create_bucket ("testbucket" )
470459
471460 blob = bucket .blob ("binary.png" )
@@ -832,9 +821,7 @@ def test_signed_url_download_with_content_disposition(self):
832821 response = requests .get (url )
833822 self .assertEqual (response .status_code , 200 )
834823 self .assertEqual (response .content , content )
835- self .assertEqual (
836- response .headers ["content-disposition" ], f"{ response_disposition } "
837- )
824+ self .assertEqual (response .headers ["content-disposition" ], f"{ response_disposition } " )
838825 self .assertEqual (response .headers ["content-type" ], "text/mycustom" )
839826
840827 def test_url_generation_for_browser (self ):
@@ -944,6 +931,24 @@ def test_upload_from_file_content_type_json(self):
944931 self .assertEqual (blob .name , file_name )
945932 self .assertEqual (blob .download_as_bytes (), content )
946933
934+ def test_upload_from_file_with_metadata (self ):
935+ file_name = "test.json"
936+ content = b'[{"a": 1}]'
937+ bucket = self ._client .create_bucket ("testbucket" )
938+ blob = bucket .blob (file_name )
939+ blob .metadata = {"foo" : "bar" }
940+
941+ with NamedTemporaryFile () as temp_file :
942+ temp_file .write (content )
943+ temp_file .flush ()
944+ temp_file .seek (0 )
945+ blob .upload_from_file (temp_file , content_type = "application/json" )
946+
947+ blob = bucket .get_blob (file_name )
948+ self .assertEqual (blob .name , file_name )
949+ self .assertEqual (blob .download_as_bytes (), content )
950+ self .assertEqual (blob .metadata , {"foo" : "bar" })
951+
947952
948953class HttpEndpointsTest (ServerBaseCase ):
949954 """Tests for the HTTP endpoints defined by server.HANDLERS."""
0 commit comments