1515import itertools
1616import json
1717import unittest
18+ import warnings
1819
1920import mock
2021import pytest
2930except (ImportError , AttributeError ): # pragma: NO COVER
3031 pandas = None
3132
33+ try :
34+ from tqdm import tqdm
35+ except (ImportError , AttributeError ): # pragma: NO COVER
36+ tqdm = None
37+
3238from google .cloud .bigquery .dataset import DatasetReference
3339
3440
@@ -901,7 +907,6 @@ def test_time_partitioning_setter_none(self):
901907 self .assertIsNone (table .time_partitioning )
902908
903909 def test_partitioning_type_setter (self ):
904- import warnings
905910 from google .cloud .bigquery .table import TimePartitioningType
906911
907912 dataset = DatasetReference (self .PROJECT , self .DS_ID )
@@ -920,7 +925,6 @@ def test_partitioning_type_setter(self):
920925 self .assertIs (warning .category , PendingDeprecationWarning )
921926
922927 def test_partitioning_type_setter_w_time_partitioning_set (self ):
923- import warnings
924928 from google .cloud .bigquery .table import TimePartitioning
925929
926930 dataset = DatasetReference (self .PROJECT , self .DS_ID )
@@ -938,7 +942,6 @@ def test_partitioning_type_setter_w_time_partitioning_set(self):
938942 self .assertIs (warning .category , PendingDeprecationWarning )
939943
940944 def test_partitioning_expiration_setter_w_time_partitioning_set (self ):
941- import warnings
942945 from google .cloud .bigquery .table import TimePartitioning
943946
944947 dataset = DatasetReference (self .PROJECT , self .DS_ID )
@@ -956,8 +959,6 @@ def test_partitioning_expiration_setter_w_time_partitioning_set(self):
956959 self .assertIs (warning .category , PendingDeprecationWarning )
957960
958961 def test_partition_expiration_setter (self ):
959- import warnings
960-
961962 dataset = DatasetReference (self .PROJECT , self .DS_ID )
962963 table_ref = dataset .table (self .TABLE_NAME )
963964 table = self ._make_one (table_ref )
@@ -1112,8 +1113,6 @@ def _make_one(self, *args, **kw):
11121113 return self ._get_target_class ()(* args , ** kw )
11131114
11141115 def test_ctor (self ):
1115- import warnings
1116-
11171116 project = "test-project"
11181117 dataset_id = "test_dataset"
11191118 table_id = "coffee_table"
@@ -1191,8 +1190,6 @@ def test_ctor_view(self):
11911190 self .assertTrue (table .view_use_legacy_sql )
11921191
11931192 def test_ctor_missing_properties (self ):
1194- import warnings
1195-
11961193 resource = {
11971194 "tableReference" : {
11981195 "projectId" : "testproject" ,
@@ -1413,6 +1410,129 @@ def test_to_dataframe(self):
14131410 self .assertEqual (df .name .dtype .name , "object" )
14141411 self .assertEqual (df .age .dtype .name , "int64" )
14151412
1413+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
1414+ @unittest .skipIf (tqdm is None , "Requires `tqdm`" )
1415+ @mock .patch ("tqdm.tqdm_gui" )
1416+ @mock .patch ("tqdm.tqdm_notebook" )
1417+ @mock .patch ("tqdm.tqdm" )
1418+ def test_to_dataframe_progress_bar (
1419+ self , tqdm_mock , tqdm_notebook_mock , tqdm_gui_mock
1420+ ):
1421+ from google .cloud .bigquery .table import RowIterator
1422+ from google .cloud .bigquery .table import SchemaField
1423+
1424+ schema = [
1425+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
1426+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
1427+ ]
1428+ rows = [
1429+ {"f" : [{"v" : "Phred Phlyntstone" }, {"v" : "32" }]},
1430+ {"f" : [{"v" : "Bharney Rhubble" }, {"v" : "33" }]},
1431+ {"f" : [{"v" : "Wylma Phlyntstone" }, {"v" : "29" }]},
1432+ {"f" : [{"v" : "Bhettye Rhubble" }, {"v" : "27" }]},
1433+ ]
1434+ path = "/foo"
1435+ api_request = mock .Mock (return_value = {"rows" : rows })
1436+
1437+ progress_bars = (
1438+ ("tqdm" , tqdm_mock ),
1439+ ("tqdm_notebook" , tqdm_notebook_mock ),
1440+ ("tqdm_gui" , tqdm_gui_mock ),
1441+ )
1442+
1443+ for progress_bar_type , progress_bar_mock in progress_bars :
1444+ row_iterator = RowIterator (_mock_client (), api_request , path , schema )
1445+ df = row_iterator .to_dataframe (progress_bar_type = progress_bar_type )
1446+
1447+ progress_bar_mock .assert_called ()
1448+ progress_bar_mock ().update .assert_called ()
1449+ self .assertEqual (len (df ), 4 )
1450+
1451+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
1452+ @mock .patch ("google.cloud.bigquery.table.tqdm" , new = None )
1453+ def test_to_dataframe_no_tqdm_no_progress_bar (self ):
1454+ from google .cloud .bigquery .table import RowIterator
1455+ from google .cloud .bigquery .table import SchemaField
1456+
1457+ schema = [
1458+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
1459+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
1460+ ]
1461+ rows = [
1462+ {"f" : [{"v" : "Phred Phlyntstone" }, {"v" : "32" }]},
1463+ {"f" : [{"v" : "Bharney Rhubble" }, {"v" : "33" }]},
1464+ {"f" : [{"v" : "Wylma Phlyntstone" }, {"v" : "29" }]},
1465+ {"f" : [{"v" : "Bhettye Rhubble" }, {"v" : "27" }]},
1466+ ]
1467+ path = "/foo"
1468+ api_request = mock .Mock (return_value = {"rows" : rows })
1469+ row_iterator = RowIterator (_mock_client (), api_request , path , schema )
1470+
1471+ with warnings .catch_warnings (record = True ) as warned :
1472+ df = row_iterator .to_dataframe ()
1473+
1474+ self .assertEqual (len (warned ), 0 )
1475+ self .assertEqual (len (df ), 4 )
1476+
1477+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
1478+ @mock .patch ("google.cloud.bigquery.table.tqdm" , new = None )
1479+ def test_to_dataframe_no_tqdm (self ):
1480+ from google .cloud .bigquery .table import RowIterator
1481+ from google .cloud .bigquery .table import SchemaField
1482+
1483+ schema = [
1484+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
1485+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
1486+ ]
1487+ rows = [
1488+ {"f" : [{"v" : "Phred Phlyntstone" }, {"v" : "32" }]},
1489+ {"f" : [{"v" : "Bharney Rhubble" }, {"v" : "33" }]},
1490+ {"f" : [{"v" : "Wylma Phlyntstone" }, {"v" : "29" }]},
1491+ {"f" : [{"v" : "Bhettye Rhubble" }, {"v" : "27" }]},
1492+ ]
1493+ path = "/foo"
1494+ api_request = mock .Mock (return_value = {"rows" : rows })
1495+ row_iterator = RowIterator (_mock_client (), api_request , path , schema )
1496+
1497+ with warnings .catch_warnings (record = True ) as warned :
1498+ df = row_iterator .to_dataframe (progress_bar_type = "tqdm" )
1499+
1500+ self .assertEqual (len (warned ), 1 )
1501+ for warning in warned :
1502+ self .assertIs (warning .category , UserWarning )
1503+
1504+ # Even though the progress bar won't show, downloading the dataframe
1505+ # should still work.
1506+ self .assertEqual (len (df ), 4 )
1507+
1508+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
1509+ @unittest .skipIf (tqdm is None , "Requires `tqdm`" )
1510+ @mock .patch ("tqdm.tqdm_gui" , new = None ) # will raise TypeError on call
1511+ @mock .patch ("tqdm.tqdm_notebook" , new = None ) # will raise TypeError on call
1512+ @mock .patch ("tqdm.tqdm" , new = None ) # will raise TypeError on call
1513+ def test_to_dataframe_tqdm_error (self ):
1514+ from google .cloud .bigquery .table import RowIterator
1515+ from google .cloud .bigquery .table import SchemaField
1516+
1517+ schema = [
1518+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
1519+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
1520+ ]
1521+ rows = [
1522+ {"f" : [{"v" : "Phred Phlyntstone" }, {"v" : "32" }]},
1523+ {"f" : [{"v" : "Bharney Rhubble" }, {"v" : "33" }]},
1524+ {"f" : [{"v" : "Wylma Phlyntstone" }, {"v" : "29" }]},
1525+ {"f" : [{"v" : "Bhettye Rhubble" }, {"v" : "27" }]},
1526+ ]
1527+ path = "/foo"
1528+
1529+ for progress_bar_type in ("tqdm" , "tqdm_notebook" , "tqdm_gui" ):
1530+ api_request = mock .Mock (return_value = {"rows" : rows })
1531+ row_iterator = RowIterator (_mock_client (), api_request , path , schema )
1532+ df = row_iterator .to_dataframe (progress_bar_type = progress_bar_type )
1533+
1534+ self .assertEqual (len (df ), 4 ) # all should be well
1535+
14161536 @unittest .skipIf (pandas is None , "Requires `pandas`" )
14171537 def test_to_dataframe_w_empty_results (self ):
14181538 from google .cloud .bigquery .table import RowIterator
0 commit comments