@@ -414,7 +414,7 @@ def test_bigquery_magic_with_legacy_sql():
414
414
with run_query_patch as run_query_mock :
415
415
ip .run_cell_magic ("bigquery" , "--use_legacy_sql" , "SELECT 17 AS num" )
416
416
417
- job_config_used = run_query_mock .call_args_list [0 ][0 ][ - 1 ]
417
+ job_config_used = run_query_mock .call_args_list [0 ][1 ][ "job_config" ]
418
418
assert job_config_used .use_legacy_sql is True
419
419
420
420
@@ -645,6 +645,57 @@ def test_bigquery_magic_without_bqstorage(monkeypatch):
645
645
assert isinstance (return_value , pandas .DataFrame )
646
646
647
647
648
+ @pytest .mark .usefixtures ("ipython_interactive" )
649
+ def test_bigquery_magic_w_max_results_invalid ():
650
+ ip = IPython .get_ipython ()
651
+ ip .extension_manager .load_extension ("google.cloud.bigquery" )
652
+ magics .context ._project = None
653
+
654
+ credentials_mock = mock .create_autospec (
655
+ google .auth .credentials .Credentials , instance = True
656
+ )
657
+ default_patch = mock .patch (
658
+ "google.auth.default" , return_value = (credentials_mock , "general-project" )
659
+ )
660
+ client_query_patch = mock .patch (
661
+ "google.cloud.bigquery.client.Client.query" , autospec = True
662
+ )
663
+
664
+ sql = "SELECT 17 AS num"
665
+
666
+ with pytest .raises (ValueError ), default_patch , client_query_patch :
667
+ ip .run_cell_magic ("bigquery" , "--max_results=abc" , sql )
668
+
669
+
670
+ @pytest .mark .usefixtures ("ipython_interactive" )
671
+ def test_bigquery_magic_w_max_results_valid_calls_queryjob_result ():
672
+ ip = IPython .get_ipython ()
673
+ ip .extension_manager .load_extension ("google.cloud.bigquery" )
674
+ magics .context ._project = None
675
+
676
+ credentials_mock = mock .create_autospec (
677
+ google .auth .credentials .Credentials , instance = True
678
+ )
679
+ default_patch = mock .patch (
680
+ "google.auth.default" , return_value = (credentials_mock , "general-project" )
681
+ )
682
+ client_query_patch = mock .patch (
683
+ "google.cloud.bigquery.client.Client.query" , autospec = True
684
+ )
685
+
686
+ sql = "SELECT 17 AS num"
687
+
688
+ query_job_mock = mock .create_autospec (
689
+ google .cloud .bigquery .job .QueryJob , instance = True
690
+ )
691
+
692
+ with client_query_patch as client_query_mock , default_patch :
693
+ client_query_mock .return_value = query_job_mock
694
+ ip .run_cell_magic ("bigquery" , "--max_results=5" , sql )
695
+
696
+ query_job_mock .result .assert_called_with (max_results = 5 )
697
+
698
+
648
699
@pytest .mark .usefixtures ("ipython_interactive" )
649
700
def test_bigquery_magic_dryrun_option_sets_job_config ():
650
701
ip = IPython .get_ipython ()
@@ -662,7 +713,7 @@ def test_bigquery_magic_dryrun_option_sets_job_config():
662
713
with run_query_patch as run_query_mock :
663
714
ip .run_cell_magic ("bigquery" , "--dry_run" , sql )
664
715
665
- job_config_used = run_query_mock .call_args_list [0 ][0 ][ - 1 ]
716
+ job_config_used = run_query_mock .call_args_list [0 ][1 ][ "job_config" ]
666
717
assert job_config_used .dry_run is True
667
718
668
719
@@ -924,6 +975,7 @@ def test_bigquery_magic_with_string_params():
924
975
run_query_mock .return_value = query_job_mock
925
976
926
977
ip .run_cell_magic ("bigquery" , 'params_string_df --params {"num":17}' , sql )
978
+
927
979
run_query_mock .assert_called_once_with (mock .ANY , sql .format (num = 17 ), mock .ANY )
928
980
929
981
assert "params_string_df" in ip .user_ns # verify that the variable exists
@@ -959,6 +1011,7 @@ def test_bigquery_magic_with_dict_params():
959
1011
# Insert dictionary into user namespace so that it can be expanded
960
1012
ip .user_ns ["params" ] = params
961
1013
ip .run_cell_magic ("bigquery" , "params_dict_df --params $params" , sql )
1014
+
962
1015
run_query_mock .assert_called_once_with (mock .ANY , sql .format (num = 17 ), mock .ANY )
963
1016
964
1017
assert "params_dict_df" in ip .user_ns # verify that the variable exists
0 commit comments