Skip to content

Commit 20d0002

Browse files
committed
Add system test for listing scripting jobs
1 parent b0f261a commit 20d0002

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

bigquery/tests/system.py

+48
Original file line numberDiff line numberDiff line change
@@ -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
474+
assert len(child_jobs) == 2
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

Comments
 (0)