@@ -1828,12 +1828,17 @@ def _do_multipart_success(
1828
1828
if_metageneration_not_match = None ,
1829
1829
kms_key_name = None ,
1830
1830
timeout = None ,
1831
+ metadata = None ,
1831
1832
):
1832
1833
from six .moves .urllib .parse import urlencode
1833
1834
1834
1835
bucket = _Bucket (name = "w00t" , user_project = user_project )
1835
1836
blob = self ._make_one (u"blob-name" , bucket = bucket , kms_key_name = kms_key_name )
1836
1837
self .assertIsNone (blob .chunk_size )
1838
+ if metadata :
1839
+ self .assertIsNone (blob .metadata )
1840
+ blob ._properties ["metadata" ] = metadata
1841
+ self .assertEqual (len (blob ._changes ), 0 )
1837
1842
1838
1843
# Create mocks to be checked for doing transport.
1839
1844
transport = self ._mock_transport (http_client .OK , {})
@@ -1906,10 +1911,18 @@ def _do_multipart_success(
1906
1911
1907
1912
upload_url += "?" + urlencode (qs_params )
1908
1913
1914
+ blob_data = b'{"name": "blob-name"}\r \n '
1915
+ if metadata :
1916
+ blob_data = (
1917
+ b'{"name": "blob-name", "metadata": '
1918
+ + json .dumps (metadata ).encode ("utf-8" )
1919
+ + b"}\r \n "
1920
+ )
1921
+ self .assertEqual (blob ._changes , set (["metadata" ]))
1909
1922
payload = (
1910
1923
b"--==0==\r \n "
1911
1924
+ b"content-type: application/json; charset=UTF-8\r \n \r \n "
1912
- + b'{"name": "blob-name"} \r \n '
1925
+ + blob_data
1913
1926
+ b"--==0==\r \n "
1914
1927
+ b"content-type: application/xml\r \n \r \n "
1915
1928
+ data_read
@@ -1974,6 +1987,10 @@ def test__do_multipart_upload_with_generation_not_match(self, mock_get_boundary)
1974
1987
mock_get_boundary , if_generation_not_match = 4 , if_metageneration_not_match = 4
1975
1988
)
1976
1989
1990
+ @mock .patch (u"google.resumable_media._upload.get_boundary" , return_value = b"==0==" )
1991
+ def test__do_multipart_upload_with_metadata (self , mock_get_boundary ):
1992
+ self ._do_multipart_success (mock_get_boundary , metadata = {"test" : "test" })
1993
+
1977
1994
def test__do_multipart_upload_bad_size (self ):
1978
1995
blob = self ._make_one (u"blob-name" , bucket = None )
1979
1996
@@ -2006,14 +2023,20 @@ def _initiate_resumable_helper(
2006
2023
blob_chunk_size = 786432 ,
2007
2024
kms_key_name = None ,
2008
2025
timeout = None ,
2026
+ metadata = None ,
2009
2027
):
2010
2028
from six .moves .urllib .parse import urlencode
2011
2029
from google .resumable_media .requests import ResumableUpload
2012
2030
from google .cloud .storage .blob import _DEFAULT_CHUNKSIZE
2013
2031
2014
2032
bucket = _Bucket (name = "whammy" , user_project = user_project )
2015
2033
blob = self ._make_one (u"blob-name" , bucket = bucket , kms_key_name = kms_key_name )
2016
- blob .metadata = {"rook" : "takes knight" }
2034
+ if metadata :
2035
+ self .assertIsNone (blob .metadata )
2036
+ blob ._properties ["metadata" ] = metadata
2037
+ self .assertEqual (len (blob ._changes ), 0 )
2038
+ else :
2039
+ blob .metadata = {"rook" : "takes knight" }
2017
2040
blob .chunk_size = blob_chunk_size
2018
2041
if blob_chunk_size is not None :
2019
2042
self .assertIsNotNone (blob .chunk_size )
@@ -2022,8 +2045,11 @@ def _initiate_resumable_helper(
2022
2045
2023
2046
# Need to make sure **same** dict is used because ``json.dumps()``
2024
2047
# will depend on the hash order.
2025
- object_metadata = blob ._get_writable_metadata ()
2026
- blob ._get_writable_metadata = mock .Mock (return_value = object_metadata , spec = [])
2048
+ if not metadata :
2049
+ object_metadata = blob ._get_writable_metadata ()
2050
+ blob ._get_writable_metadata = mock .Mock (
2051
+ return_value = object_metadata , spec = []
2052
+ )
2027
2053
2028
2054
# Create mocks to be checked for doing transport.
2029
2055
resumable_url = "http://test.invalid?upload_id=hey-you"
@@ -2107,6 +2133,8 @@ def _initiate_resumable_helper(
2107
2133
self .assertNotEqual (blob .chunk_size , chunk_size )
2108
2134
self .assertEqual (upload ._chunk_size , chunk_size )
2109
2135
self .assertIs (upload ._stream , stream )
2136
+ if metadata :
2137
+ self .assertEqual (blob ._changes , set (["metadata" ]))
2110
2138
if size is None :
2111
2139
self .assertIsNone (upload ._total_bytes )
2112
2140
else :
@@ -2125,8 +2153,11 @@ def _initiate_resumable_helper(
2125
2153
# Make sure we never read from the stream.
2126
2154
self .assertEqual (stream .tell (), 0 )
2127
2155
2128
- # Check the mocks.
2129
- blob ._get_writable_metadata .assert_called_once_with ()
2156
+ if metadata :
2157
+ object_metadata = {"name" : u"blob-name" , "metadata" : metadata }
2158
+ else :
2159
+ # Check the mocks.
2160
+ blob ._get_writable_metadata .assert_called_once_with ()
2130
2161
payload = json .dumps (object_metadata ).encode ("utf-8" )
2131
2162
expected_headers = {
2132
2163
"content-type" : "application/json; charset=UTF-8" ,
@@ -2144,6 +2175,9 @@ def _initiate_resumable_helper(
2144
2175
timeout = expected_timeout ,
2145
2176
)
2146
2177
2178
+ def test__initiate_resumable_upload_with_metadata (self ):
2179
+ self ._initiate_resumable_helper (metadata = {"test" : "test" })
2180
+
2147
2181
def test__initiate_resumable_upload_with_custom_timeout (self ):
2148
2182
self ._initiate_resumable_helper (timeout = 9.58 )
2149
2183
0 commit comments