|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const {BigQueryClient} = require('@google-cloud/bigquery'); |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {describe, it, before, after} = require('mocha'); |
| 20 | +const {randomUUID} = require('crypto'); |
| 21 | +const {setInterval} = require('node:timers/promises'); |
| 22 | + |
| 23 | +const cp = require('child_process'); |
| 24 | + |
| 25 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 26 | + |
| 27 | +const bigquery = new BigQueryClient(); |
| 28 | +let jobId; |
| 29 | +const GCLOUD_TESTS_PREFIX = 'nodejs_samples_tests_jobs'; |
| 30 | + |
| 31 | +// the GCLOUD_PROJECT environment variable is set as part of test harness setup |
| 32 | +const projectId = process.env.GCLOUD_PROJECT; |
| 33 | + |
| 34 | +describe('Jobs', () => { |
| 35 | + const datasetId = `${GCLOUD_TESTS_PREFIX}_${randomUUID()}`.replace( |
| 36 | + /-/gi, |
| 37 | + '_', |
| 38 | + ); |
| 39 | + const query = `SELECT country_name |
| 40 | + FROM \`bigquery-public-data.utility_us.country_code_iso\` |
| 41 | + LIMIT 10`; |
| 42 | + const insertJobRequest = { |
| 43 | + projectId: projectId, |
| 44 | + job: { |
| 45 | + configuration: { |
| 46 | + query: { |
| 47 | + query: query, |
| 48 | + useLegacySql: {value: false}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + before(async () => { |
| 55 | + if (projectId === undefined) { |
| 56 | + throw Error( |
| 57 | + 'GCLOUD_PROJECT must be defined as an environment variable before tests can be run', |
| 58 | + ); |
| 59 | + } |
| 60 | + const datasetObject = { |
| 61 | + datasetReference: { |
| 62 | + datasetId: datasetId, |
| 63 | + }, |
| 64 | + location: 'US', |
| 65 | + }; |
| 66 | + const datasetRequest = { |
| 67 | + projectId: projectId, |
| 68 | + dataset: datasetObject, |
| 69 | + }; |
| 70 | + const [datasetResponse] = await bigquery.insertDataset(datasetRequest); |
| 71 | + assert.ok(datasetResponse); |
| 72 | + }); |
| 73 | + after(async () => { |
| 74 | + const deleteRequest = { |
| 75 | + projectId: projectId, |
| 76 | + datasetId: datasetId, |
| 77 | + deleteContents: true, |
| 78 | + }; |
| 79 | + await bigquery.deleteDataset(deleteRequest); |
| 80 | + const getDatasetRequest = { |
| 81 | + projectId: projectId, |
| 82 | + datasetId: datasetId, |
| 83 | + }; |
| 84 | + try { |
| 85 | + await bigquery.getDataset(getDatasetRequest); |
| 86 | + } catch (err) { |
| 87 | + assert.strictEqual( |
| 88 | + err.details, |
| 89 | + `Not found: Dataset ${projectId}:${datasetId}`, |
| 90 | + ); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + describe('get + list jobs', () => { |
| 95 | + before('create a job to get/list', async () => { |
| 96 | + // Run query to create a job |
| 97 | + const [job] = await bigquery.insertJob(insertJobRequest); |
| 98 | + assert.ok(job); |
| 99 | + const jobReference = job.jobReference; |
| 100 | + const getQueryResultsRequest = { |
| 101 | + projectId: projectId, |
| 102 | + jobId: jobReference.jobId, |
| 103 | + location: jobReference.location.value, |
| 104 | + }; |
| 105 | + jobId = getQueryResultsRequest.jobId; |
| 106 | + // poll the job status every 3 seconds until complete |
| 107 | + // eslint-disable-next-line |
| 108 | + for await (const t of setInterval(3000)) { // no-unused-vars - this is the syntax for promise based setInterval |
| 109 | + const [resp] = await bigquery.jobClient.getQueryResults( |
| 110 | + getQueryResultsRequest, |
| 111 | + ); |
| 112 | + if (resp.errors.length !== 0) { |
| 113 | + throw new Error('Something failed in job creation'); |
| 114 | + } |
| 115 | + if (resp.jobComplete.value) { |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('should list jobs', async () => { |
| 122 | + const output = execSync(`node jobs/listJobs.js ${projectId}`); |
| 123 | + assert.match(output, /Jobs:/); |
| 124 | + assert.include(output, jobId); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should retrieve a job', async () => { |
| 128 | + const output = execSync(`node jobs/getJob.js ${projectId} ${jobId}`); |
| 129 | + assert.include(output, `Job ${projectId}:US.${jobId}`); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('create + cancel jobs', () => { |
| 134 | + let jobId; |
| 135 | + before('create a job to cancel', async () => { |
| 136 | + // Run query to create a job |
| 137 | + const [job] = await bigquery.insertJob(insertJobRequest); |
| 138 | + assert.ok(job); |
| 139 | + jobId = job.jobReference.jobId; |
| 140 | + }); |
| 141 | + it('should attempt to cancel a job', async () => { |
| 142 | + assert.exists(jobId); |
| 143 | + const output = execSync(`node jobs/cancelJob.js ${projectId} ${jobId}`); |
| 144 | + assert.include(output, 'state:'); |
| 145 | + assert.include(output, 'errorResult: null'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should create a job', async () => { |
| 149 | + const output = execSync(`node jobs/createJob.js ${projectId}`); |
| 150 | + assert.include(output, 'Rows:'); |
| 151 | + assert.include(output, 'Tromelin Island'); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments