|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import concurrent.futures |
15 | 16 | import copy |
16 | 17 | import json |
17 | 18 |
|
|
36 | 37 | except (ImportError, AttributeError): # pragma: NO COVER |
37 | 38 | geopandas = None |
38 | 39 | try: |
39 | | - from tqdm import tqdm |
| 40 | + import tqdm |
40 | 41 | except (ImportError, AttributeError): # pragma: NO COVER |
41 | 42 | tqdm = None |
42 | 43 |
|
@@ -300,7 +301,8 @@ def test_to_arrow_max_results_no_progress_bar(): |
300 | 301 |
|
301 | 302 |
|
302 | 303 | @pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") |
303 | | -def test_to_arrow_w_tqdm_w_query_plan(): |
| 304 | +@mock.patch("tqdm.tqdm") |
| 305 | +def test_to_arrow_w_tqdm_w_query_plan(tqdm_mock): |
304 | 306 | from google.cloud.bigquery import table |
305 | 307 | from google.cloud.bigquery.job import QueryJob as target_class |
306 | 308 | from google.cloud.bigquery.schema import SchemaField |
@@ -338,23 +340,26 @@ def test_to_arrow_w_tqdm_w_query_plan(): |
338 | 340 | result_patch = mock.patch( |
339 | 341 | "google.cloud.bigquery.job.QueryJob.result", |
340 | 342 | side_effect=[ |
| 343 | + concurrent.futures.TimeoutError, |
| 344 | + concurrent.futures.TimeoutError, |
341 | 345 | row_iterator, |
342 | 346 | ], |
343 | 347 | ) |
344 | 348 |
|
345 | | - with result_patch as result_patch_tqdm, reload_patch: |
| 349 | + with result_patch as tqdm_mock, reload_patch: |
346 | 350 | tbl = job.to_arrow(progress_bar_type="tqdm", create_bqstorage_client=False) |
347 | 351 |
|
348 | | - assert result_patch_tqdm.call_count == 1 |
| 352 | + assert tqdm_mock.call_count == 3 |
349 | 353 | assert isinstance(tbl, pyarrow.Table) |
350 | 354 | assert tbl.num_rows == 2 |
351 | | - result_patch_tqdm.assert_called_with( |
| 355 | + tqdm_mock.assert_called_with( |
352 | 356 | timeout=_PROGRESS_BAR_UPDATE_INTERVAL, max_results=None |
353 | 357 | ) |
354 | 358 |
|
355 | 359 |
|
356 | 360 | @pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") |
357 | | -def test_to_arrow_w_tqdm_w_pending_status(): |
| 361 | +@mock.patch("tqdm.tqdm") |
| 362 | +def test_to_arrow_w_tqdm_w_pending_status(tqdm_mock): |
358 | 363 | from google.cloud.bigquery import table |
359 | 364 | from google.cloud.bigquery.job import QueryJob as target_class |
360 | 365 | from google.cloud.bigquery.schema import SchemaField |
@@ -391,16 +396,16 @@ def test_to_arrow_w_tqdm_w_pending_status(): |
391 | 396 | ) |
392 | 397 | result_patch = mock.patch( |
393 | 398 | "google.cloud.bigquery.job.QueryJob.result", |
394 | | - side_effect=[row_iterator], |
| 399 | + side_effect=[concurrent.futures.TimeoutError, row_iterator], |
395 | 400 | ) |
396 | 401 |
|
397 | | - with result_patch as result_patch_tqdm, reload_patch: |
| 402 | + with result_patch as tqdm_mock, reload_patch: |
398 | 403 | tbl = job.to_arrow(progress_bar_type="tqdm", create_bqstorage_client=False) |
399 | 404 |
|
400 | | - assert result_patch_tqdm.call_count == 1 |
| 405 | + assert tqdm_mock.call_count == 2 |
401 | 406 | assert isinstance(tbl, pyarrow.Table) |
402 | 407 | assert tbl.num_rows == 2 |
403 | | - result_patch_tqdm.assert_called_with( |
| 408 | + tqdm_mock.assert_called_with( |
404 | 409 | timeout=_PROGRESS_BAR_UPDATE_INTERVAL, max_results=None |
405 | 410 | ) |
406 | 411 |
|
@@ -748,7 +753,8 @@ def test_to_dataframe_with_progress_bar(tqdm_mock): |
748 | 753 |
|
749 | 754 |
|
750 | 755 | @pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") |
751 | | -def test_to_dataframe_w_tqdm_pending(): |
| 756 | +@mock.patch("tqdm.tqdm") |
| 757 | +def test_to_dataframe_w_tqdm_pending(tqdm_mock): |
752 | 758 | from google.cloud.bigquery import table |
753 | 759 | from google.cloud.bigquery.job import QueryJob as target_class |
754 | 760 | from google.cloud.bigquery.schema import SchemaField |
@@ -787,23 +793,24 @@ def test_to_dataframe_w_tqdm_pending(): |
787 | 793 | ) |
788 | 794 | result_patch = mock.patch( |
789 | 795 | "google.cloud.bigquery.job.QueryJob.result", |
790 | | - side_effect=[row_iterator], |
| 796 | + side_effect=[concurrent.futures.TimeoutError, row_iterator], |
791 | 797 | ) |
792 | 798 |
|
793 | | - with result_patch as result_patch_tqdm, reload_patch: |
| 799 | + with result_patch as tqdm_mock, reload_patch: |
794 | 800 | df = job.to_dataframe(progress_bar_type="tqdm", create_bqstorage_client=False) |
795 | 801 |
|
796 | | - assert result_patch_tqdm.call_count == 1 |
| 802 | + assert tqdm_mock.call_count == 2 |
797 | 803 | assert isinstance(df, pandas.DataFrame) |
798 | 804 | assert len(df) == 4 # verify the number of rows |
799 | 805 | assert list(df) == ["name", "age"] # verify the column names |
800 | | - result_patch_tqdm.assert_called_with( |
| 806 | + tqdm_mock.assert_called_with( |
801 | 807 | timeout=_PROGRESS_BAR_UPDATE_INTERVAL, max_results=None |
802 | 808 | ) |
803 | 809 |
|
804 | 810 |
|
805 | 811 | @pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") |
806 | | -def test_to_dataframe_w_tqdm(): |
| 812 | +@mock.patch("tqdm.tqdm") |
| 813 | +def test_to_dataframe_w_tqdm(tqdm_mock): |
807 | 814 | from google.cloud.bigquery import table |
808 | 815 | from google.cloud.bigquery.job import QueryJob as target_class |
809 | 816 | from google.cloud.bigquery.schema import SchemaField |
@@ -843,24 +850,27 @@ def test_to_dataframe_w_tqdm(): |
843 | 850 | result_patch = mock.patch( |
844 | 851 | "google.cloud.bigquery.job.QueryJob.result", |
845 | 852 | side_effect=[ |
| 853 | + concurrent.futures.TimeoutError, |
| 854 | + concurrent.futures.TimeoutError, |
846 | 855 | row_iterator, |
847 | 856 | ], |
848 | 857 | ) |
849 | 858 |
|
850 | | - with result_patch as result_patch_tqdm, reload_patch: |
| 859 | + with result_patch as tqdm_mock, reload_patch: |
851 | 860 | df = job.to_dataframe(progress_bar_type="tqdm", create_bqstorage_client=False) |
852 | 861 |
|
853 | | - assert result_patch_tqdm.call_count == 1 |
| 862 | + assert tqdm_mock.call_count == 3 |
854 | 863 | assert isinstance(df, pandas.DataFrame) |
855 | 864 | assert len(df) == 4 # verify the number of rows |
856 | 865 | assert list(df), ["name", "age"] # verify the column names |
857 | | - result_patch_tqdm.assert_called_with( |
| 866 | + tqdm_mock.assert_called_with( |
858 | 867 | timeout=_PROGRESS_BAR_UPDATE_INTERVAL, max_results=None |
859 | 868 | ) |
860 | 869 |
|
861 | 870 |
|
862 | 871 | @pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") |
863 | | -def test_to_dataframe_w_tqdm_max_results(): |
| 872 | +@mock.patch("tqdm.tqdm") |
| 873 | +def test_to_dataframe_w_tqdm_max_results(tqdm_mock): |
864 | 874 | from google.cloud.bigquery import table |
865 | 875 | from google.cloud.bigquery.job import QueryJob as target_class |
866 | 876 | from google.cloud.bigquery.schema import SchemaField |
@@ -894,16 +904,16 @@ def test_to_dataframe_w_tqdm_max_results(): |
894 | 904 | ) |
895 | 905 | result_patch = mock.patch( |
896 | 906 | "google.cloud.bigquery.job.QueryJob.result", |
897 | | - side_effect=[row_iterator], |
| 907 | + side_effect=[concurrent.futures.TimeoutError, row_iterator], |
898 | 908 | ) |
899 | 909 |
|
900 | | - with result_patch as result_patch_tqdm, reload_patch: |
| 910 | + with result_patch as tqdm_mock, reload_patch: |
901 | 911 | job.to_dataframe( |
902 | 912 | progress_bar_type="tqdm", create_bqstorage_client=False, max_results=3 |
903 | 913 | ) |
904 | 914 |
|
905 | | - assert result_patch_tqdm.call_count == 1 |
906 | | - result_patch_tqdm.assert_called_with( |
| 915 | + assert tqdm_mock.call_count == 2 |
| 916 | + tqdm_mock.assert_called_with( |
907 | 917 | timeout=_PROGRESS_BAR_UPDATE_INTERVAL, max_results=3 |
908 | 918 | ) |
909 | 919 |
|
|
0 commit comments