@@ -1037,6 +1037,86 @@ def test_result_dry_run(self):
1037
1037
self .assertIsNone (result .job_id )
1038
1038
self .assertIsNone (result .query_id )
1039
1039
1040
+ # If the job doesn't exist, create the job first. Issue:
1041
+ # https://github.com/googleapis/python-bigquery/issues/1940
1042
+ def test_result_begin_job_if_not_exist (self ):
1043
+ begun_resource = self ._make_resource ()
1044
+ query_running_resource = {
1045
+ "jobComplete" : True ,
1046
+ "jobReference" : {
1047
+ "projectId" : self .PROJECT ,
1048
+ "jobId" : self .JOB_ID ,
1049
+ "location" : "US" ,
1050
+ },
1051
+ "schema" : {"fields" : [{"name" : "col1" , "type" : "STRING" }]},
1052
+ "status" : {"state" : "RUNNING" },
1053
+ }
1054
+ query_done_resource = {
1055
+ "jobComplete" : True ,
1056
+ "jobReference" : {
1057
+ "projectId" : self .PROJECT ,
1058
+ "jobId" : self .JOB_ID ,
1059
+ "location" : "US" ,
1060
+ },
1061
+ "schema" : {"fields" : [{"name" : "col1" , "type" : "STRING" }]},
1062
+ "status" : {"state" : "DONE" },
1063
+ }
1064
+ done_resource = copy .deepcopy (begun_resource )
1065
+ done_resource ["status" ] = {"state" : "DONE" }
1066
+ connection = make_connection (
1067
+ begun_resource ,
1068
+ query_running_resource ,
1069
+ query_done_resource ,
1070
+ done_resource ,
1071
+ )
1072
+ client = _make_client (project = self .PROJECT , connection = connection )
1073
+ job = self ._make_one (self .JOB_ID , self .QUERY , client )
1074
+ job ._properties ["jobReference" ]["location" ] = "US"
1075
+
1076
+ job .result ()
1077
+
1078
+ create_job_call = mock .call (
1079
+ method = "POST" ,
1080
+ path = f"/projects/{ self .PROJECT } /jobs" ,
1081
+ data = {
1082
+ "jobReference" : {
1083
+ "jobId" : self .JOB_ID ,
1084
+ "projectId" : self .PROJECT ,
1085
+ "location" : "US" ,
1086
+ },
1087
+ "configuration" : {
1088
+ "query" : {"useLegacySql" : False , "query" : self .QUERY },
1089
+ },
1090
+ },
1091
+ timeout = None ,
1092
+ )
1093
+ reload_call = mock .call (
1094
+ method = "GET" ,
1095
+ path = f"/projects/{ self .PROJECT } /jobs/{ self .JOB_ID } " ,
1096
+ query_params = {"projection" : "full" , "location" : "US" },
1097
+ timeout = DEFAULT_GET_JOB_TIMEOUT ,
1098
+ )
1099
+ get_query_results_call = mock .call (
1100
+ method = "GET" ,
1101
+ path = f"/projects/{ self .PROJECT } /queries/{ self .JOB_ID } " ,
1102
+ query_params = {
1103
+ "maxResults" : 0 ,
1104
+ "location" : "US" ,
1105
+ },
1106
+ timeout = None ,
1107
+ )
1108
+
1109
+ connection .api_request .assert_has_calls (
1110
+ [
1111
+ # Make sure we start a job that hasn't started yet. See:
1112
+ # https://github.com/googleapis/python-bigquery/issues/1940
1113
+ create_job_call ,
1114
+ reload_call ,
1115
+ get_query_results_call ,
1116
+ reload_call ,
1117
+ ]
1118
+ )
1119
+
1040
1120
def test_result_with_done_job_calls_get_query_results (self ):
1041
1121
query_resource_done = {
1042
1122
"jobComplete" : True ,
@@ -1379,6 +1459,7 @@ def test_result_w_timeout_doesnt_raise(self):
1379
1459
client = _make_client (project = self .PROJECT , connection = connection )
1380
1460
job = self ._make_one (self .JOB_ID , self .QUERY , client )
1381
1461
job ._properties ["jobReference" ]["location" ] = "US"
1462
+ job ._properties ["status" ] = {"state" : "RUNNING" }
1382
1463
1383
1464
with freezegun .freeze_time ("1970-01-01 00:00:00" , tick = False ):
1384
1465
job .result (
@@ -1429,6 +1510,7 @@ def test_result_w_timeout_raises_concurrent_futures_timeout(self):
1429
1510
client = _make_client (project = self .PROJECT , connection = connection )
1430
1511
job = self ._make_one (self .JOB_ID , self .QUERY , client )
1431
1512
job ._properties ["jobReference" ]["location" ] = "US"
1513
+ job ._properties ["status" ] = {"state" : "RUNNING" }
1432
1514
1433
1515
with freezegun .freeze_time (
1434
1516
"1970-01-01 00:00:00" , auto_tick_seconds = 1.0
@@ -2319,5 +2401,6 @@ def test_iter(self):
2319
2401
connection = make_connection (begun_resource , query_resource , done_resource )
2320
2402
client = _make_client (project = self .PROJECT , connection = connection )
2321
2403
job = self ._make_one (self .JOB_ID , self .QUERY , client )
2404
+ job ._properties ["status" ] = {"state" : "RUNNING" }
2322
2405
2323
2406
self .assertIsInstance (iter (job ), types .GeneratorType )
0 commit comments