@@ -431,6 +431,54 @@ def test_list_tables(self):
431431 )
432432 self .assertGreater (len (list (iterator )), 0 )
433433
434+ def test_listing_scripting_jobs (self ):
435+ # run an SQL script
436+ sql_script = """
437+ -- Declare a variable to hold names as an array.
438+ DECLARE top_names ARRAY<STRING>;
439+
440+ -- Build an array of the top 100 names from the year 2017.
441+ SET top_names = (
442+ SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
443+ FROM `bigquery-public-data.usa_names.usa_1910_current`
444+ WHERE year = 2017
445+ );
446+
447+ -- Which names appear as words in Shakespeare's plays?
448+ SELECT
449+ name AS shakespeare_name
450+ FROM UNNEST(top_names) AS name
451+ WHERE name IN (
452+ SELECT word
453+ FROM `bigquery-public-data.samples.shakespeare`
454+ );
455+ """
456+ test_start = datetime .datetime .utcnow ()
457+ query_job = Config .CLIENT .query (sql_script , project = Config .CLIENT .project )
458+ query_job .result ()
459+
460+ # fetch jobs created by the SQL script, sort them into parent and
461+ # child jobs
462+ script_jobs = list (Config .CLIENT .list_jobs (min_creation_time = test_start ))
463+
464+ parent_jobs = []
465+ child_jobs = []
466+
467+ for job in script_jobs :
468+ if job .num_child_jobs > 0 :
469+ parent_jobs .append (job )
470+ else :
471+ child_jobs .append (job )
472+
473+ assert len (parent_jobs ) == 1 # also implying num_child_jobs > 0
474+ assert len (child_jobs ) == parent_jobs [0 ].num_child_jobs
475+
476+ # fetch jobs using the parent job filter, verify that results are as expected
477+ fetched_jobs = list (Config .CLIENT .list_jobs (parent_job = parent_jobs [0 ]))
478+ assert sorted (job .job_id for job in fetched_jobs ) == sorted (
479+ job .job_id for job in child_jobs
480+ )
481+
434482 def test_update_table (self ):
435483 dataset = self .temp_dataset (_make_dataset_id ("update_table" ))
436484
0 commit comments