@@ -1622,66 +1622,88 @@ def test_download_as_bytes_w_raw(self):
1622
1622
def test_download_as_byte_w_custom_timeout (self ):
1623
1623
self ._download_as_bytes_helper (raw_download = False , timeout = 9.58 )
1624
1624
1625
- def _download_as_text_helper (self , raw_download , encoding = None , timeout = None ):
1625
+ def _download_as_text_helper (
1626
+ self ,
1627
+ raw_download ,
1628
+ client = None ,
1629
+ start = None ,
1630
+ end = None ,
1631
+ if_generation_match = None ,
1632
+ if_generation_not_match = None ,
1633
+ if_metageneration_match = None ,
1634
+ if_metageneration_not_match = None ,
1635
+ timeout = None ,
1636
+ encoding = None ,
1637
+ charset = None ,
1638
+ no_charset = False ,
1639
+ expected_value = u"DEADBEEF" ,
1640
+ payload = None ,
1641
+ ):
1642
+ if payload is None :
1643
+ if encoding is not None :
1644
+ payload = expected_value .encode (encoding )
1645
+ elif charset is not None :
1646
+ payload = expected_value .encode (charset )
1647
+ else :
1648
+ payload = expected_value .encode ()
1649
+
1626
1650
blob_name = "blob-name"
1627
- client = mock .Mock (spec = ["_http" ])
1628
- bucket = _Bucket (client )
1629
- media_link = "http://example.com/media/"
1630
- properties = {"mediaLink" : media_link }
1631
- if encoding :
1632
- properties ["contentEncoding" ] = encoding
1651
+ bucket = _Bucket ()
1652
+
1653
+ properties = {}
1654
+ if charset is not None :
1655
+ properties ["contentType" ] = "text/plain; charset={}" .format (charset )
1656
+ elif no_charset :
1657
+ properties = {"contentType" : "text/plain" }
1658
+
1633
1659
blob = self ._make_one (blob_name , bucket = bucket , properties = properties )
1634
- blob ._do_download = mock .Mock ()
1660
+ blob .download_as_bytes = mock .Mock (return_value = payload )
1635
1661
1636
- if timeout is None :
1637
- expected_timeout = self ._get_default_timeout ()
1638
- fetched = blob .download_as_text (raw_download = raw_download )
1639
- else :
1640
- expected_timeout = timeout
1641
- fetched = blob .download_as_text (raw_download = raw_download , timeout = timeout )
1662
+ kwargs = {"raw_download" : raw_download }
1642
1663
1643
- self .assertEqual (fetched , "" )
1664
+ if client is not None :
1665
+ kwargs ["client" ] = client
1644
1666
1645
- headers = {"accept-encoding" : "gzip" }
1646
- blob ._do_download .assert_called_once_with (
1647
- client ._http ,
1648
- mock .ANY ,
1649
- media_link ,
1650
- headers ,
1651
- None ,
1652
- None ,
1653
- raw_download ,
1654
- timeout = expected_timeout ,
1655
- checksum = "md5" ,
1656
- )
1657
- stream = blob ._do_download .mock_calls [0 ].args [1 ]
1658
- self .assertIsInstance (stream , io .BytesIO )
1667
+ if start is not None :
1668
+ kwargs ["start" ] = start
1659
1669
1660
- def test_download_as_text_w_generation_match (self ):
1661
- GENERATION_NUMBER = 6
1662
- MEDIA_LINK = "http://example.com/media/"
1670
+ if end is not None :
1671
+ kwargs ["end" ] = end
1663
1672
1664
- client = mock .Mock (spec = ["_http" ])
1665
- blob = self ._make_one (
1666
- "blob-name" , bucket = _Bucket (client ), properties = {"mediaLink" : MEDIA_LINK }
1667
- )
1668
- blob .download_to_file = mock .Mock ()
1673
+ if encoding is not None :
1674
+ kwargs ["encoding" ] = encoding
1669
1675
1670
- fetched = blob . download_as_text ( if_generation_match = GENERATION_NUMBER )
1671
- self . assertEqual ( fetched , "" )
1676
+ if if_generation_match is not None :
1677
+ kwargs [ "if_generation_match" ] = if_generation_match
1672
1678
1673
- blob .download_to_file .assert_called_once_with (
1674
- mock .ANY ,
1675
- client = None ,
1676
- start = None ,
1677
- end = None ,
1678
- raw_download = False ,
1679
- if_generation_match = GENERATION_NUMBER ,
1680
- if_generation_not_match = None ,
1681
- if_metageneration_match = None ,
1682
- if_metageneration_not_match = None ,
1683
- timeout = self ._get_default_timeout (),
1684
- checksum = "md5" ,
1679
+ if if_generation_not_match is not None :
1680
+ kwargs ["if_generation_not_match" ] = if_generation_not_match
1681
+
1682
+ if if_metageneration_match is not None :
1683
+ kwargs ["if_metageneration_match" ] = if_metageneration_match
1684
+
1685
+ if if_metageneration_not_match is not None :
1686
+ kwargs ["if_metageneration_not_match" ] = if_metageneration_not_match
1687
+
1688
+ if timeout is None :
1689
+ expected_timeout = self ._get_default_timeout ()
1690
+ else :
1691
+ kwargs ["timeout" ] = expected_timeout = timeout
1692
+
1693
+ fetched = blob .download_as_text (** kwargs )
1694
+
1695
+ self .assertEqual (fetched , expected_value )
1696
+
1697
+ blob .download_as_bytes .assert_called_once_with (
1698
+ client = client ,
1699
+ start = client ,
1700
+ end = client ,
1701
+ raw_download = raw_download ,
1702
+ timeout = expected_timeout ,
1703
+ if_generation_match = if_generation_match ,
1704
+ if_generation_not_match = if_generation_not_match ,
1705
+ if_metageneration_match = if_metageneration_match ,
1706
+ if_metageneration_not_match = if_metageneration_not_match ,
1685
1707
)
1686
1708
1687
1709
def test_download_as_text_wo_raw (self ):
@@ -1693,8 +1715,41 @@ def test_download_as_text_w_raw(self):
1693
1715
def test_download_as_text_w_custom_timeout (self ):
1694
1716
self ._download_as_text_helper (raw_download = False , timeout = 9.58 )
1695
1717
1696
- def test_download_as_text_w_encoding (self ):
1697
- self ._download_as_text_helper (raw_download = False , encoding = "utf-8" )
1718
+ def test_download_as_text_w_if_generation_match (self ):
1719
+ self ._download_as_text_helper (raw_download = False , if_generation_match = 6 )
1720
+
1721
+ def test_download_as_text_w_if_generation_not_match (self ):
1722
+ self ._download_as_text_helper (raw_download = False , if_generation_not_match = 6 )
1723
+
1724
+ def test_download_as_text_w_if_metageneration_match (self ):
1725
+ self ._download_as_text_helper (raw_download = False , if_metageneration_match = 6 )
1726
+
1727
+ def test_download_as_text_w_if_metageneration_not_match (self ):
1728
+ self ._download_as_text_helper (raw_download = False , if_metageneration_not_match = 6 )
1729
+
1730
+ def test_download_as_text_w_non_ascii_w_explicit_encoding (self ):
1731
+ expected_value = u"\x0A Fe"
1732
+ encoding = "utf-16"
1733
+ charset = "latin1"
1734
+ payload = expected_value .encode (encoding )
1735
+ self ._download_as_text_helper (
1736
+ raw_download = False ,
1737
+ expected_value = expected_value ,
1738
+ payload = payload ,
1739
+ encoding = encoding ,
1740
+ charset = charset ,
1741
+ )
1742
+
1743
+ def test_download_as_text_w_non_ascii_wo_explicit_encoding_w_charset (self ):
1744
+ expected_value = u"\x0A Fe"
1745
+ charset = "utf-16"
1746
+ payload = expected_value .encode (charset )
1747
+ self ._download_as_text_helper (
1748
+ raw_download = False ,
1749
+ expected_value = expected_value ,
1750
+ payload = payload ,
1751
+ charset = charset ,
1752
+ )
1698
1753
1699
1754
@mock .patch ("warnings.warn" )
1700
1755
def test_download_as_string (self , mock_warn ):
0 commit comments