-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathtables.test.js
737 lines (659 loc) · 24.4 KB
/
tables.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const {assert} = require('chai');
const {describe, it, before, after, beforeEach} = require('mocha');
const path = require('path');
const {randomUUID} = require('crypto');
const cp = require('child_process');
const {Storage} = require('@google-cloud/storage');
const {BigQuery} = require('@google-cloud/bigquery');
const {DataCatalogClient, PolicyTagManagerClient} =
require('@google-cloud/datacatalog').v1;
const dataCatalog = new DataCatalogClient();
const policyTagManager = new PolicyTagManagerClient();
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const GCLOUD_TESTS_PREFIX = 'nodejs_samples_tests';
const storage = new Storage();
const generateUuid = () =>
`${GCLOUD_TESTS_PREFIX}_${randomUUID()}`.replace(/-/gi, '_');
const datasetId = generateUuid();
const srcDatasetId = datasetId;
const destDatasetId = generateUuid();
const tableId = generateUuid();
const nestedTableId = generateUuid();
const partitionedTableId = generateUuid();
const srcTableId = tableId;
const aclTableId = generateUuid();
const destTableId = generateUuid();
const viewId = generateUuid();
const bucketName = generateUuid();
const exportCSVFileName = 'data.json';
const exportJSONFileName = 'data.json';
const importFileName = 'data.avro';
const partialDataFileName = 'partialdata.csv';
const localFilePath = path.join(__dirname, `../resources/${importFileName}`);
const testExpirationTime = Date.now() + 2 * 60 * 60 * 1000; // Add two hours
let projectId;
let policyTag0;
let policyTag1;
const partialDataFilePath = path.join(
__dirname,
`../resources/${partialDataFileName}`,
);
const bigquery = new BigQuery();
describe('Tables', () => {
before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await Promise.all([
bucket.upload(localFilePath),
bigquery.createDataset(srcDatasetId),
bigquery.createDataset(destDatasetId),
]);
// Delete stale Data Catalog resources
projectId = await dataCatalog.getProjectId();
await deleteStaleTaxonomies();
// Create Data Catalog resources
const parent = dataCatalog.locationPath(projectId, 'us');
const taxRequest = {
parent,
taxonomy: {
displayName: generateUuid(),
activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'],
},
};
const [taxonomy] = await policyTagManager.createTaxonomy(taxRequest);
const tagRequest = {
parent: taxonomy.name,
policyTag: {
displayName: generateUuid(),
},
};
[policyTag0] = await policyTagManager.createPolicyTag(tagRequest);
tagRequest.policyTag.displayName = generateUuid();
[policyTag1] = await policyTagManager.createPolicyTag(tagRequest);
});
// to avoid getting rate limited
beforeEach(async function () {
this.currentTest.retries(2);
});
after(async () => {
await bigquery
.dataset(srcDatasetId)
.delete({force: true})
.catch(console.warn);
await bigquery
.dataset(destDatasetId)
.delete({force: true})
.catch(console.warn);
await bigquery.dataset(datasetId).delete({force: true}).catch(console.warn);
await storage
.bucket(bucketName)
.deleteFiles({force: true})
.catch(console.warn);
await storage
.bucket(bucketName)
.deleteFiles({force: true})
.catch(console.warn);
await bigquery
.dataset(srcDatasetId)
.delete({force: true})
.catch(console.warn);
await storage.bucket(bucketName).delete().catch(console.warn);
});
it('should create a table', async () => {
const output = execSync(`node createTable.js ${datasetId} ${tableId}`);
assert.include(output, `Table ${tableId} created.`);
const [exists] = await bigquery.dataset(datasetId).table(tableId).exists();
assert.ok(exists);
});
it('should create a partitioned table', async () => {
const output = execSync(
`node createTablePartitioned.js ${datasetId} ${partitionedTableId}`,
);
assert.include(
output,
`Table ${partitionedTableId} created with partitioning:`,
);
assert.include(output, "type: 'DAY'");
assert.include(output, "field: 'date'");
const [exists] = await bigquery
.dataset(datasetId)
.table(partitionedTableId)
.exists();
assert.ok(exists);
});
it('should create an integer range partitioned table', async () => {
const rangePartTableId = generateUuid();
const output = execSync(
`node createTableRangePartitioned.js ${datasetId} ${rangePartTableId}`,
);
assert.include(
output,
`Table ${rangePartTableId} created with integer range partitioning:`,
);
assert.include(
output,
"range: { start: '0', end: '100000', interval: '10' }",
);
const [exists] = await bigquery
.dataset(datasetId)
.table(rangePartTableId)
.exists();
assert.ok(exists);
});
it('should create a clustered table', async () => {
const clusteredTableId = generateUuid();
const output = execSync(
`node createTableClustered.js ${datasetId} ${clusteredTableId}`,
);
assert.include(
output,
`Table ${clusteredTableId} created with clustering:`,
);
assert.include(output, "{ fields: [ 'city', 'zipcode' ] }");
const [exists] = await bigquery
.dataset(datasetId)
.table(clusteredTableId)
.exists();
assert.ok(exists);
});
it('should update table clustering', async () => {
const clusteredTableId = generateUuid();
const output = execSync(
`node removeTableClustering.js ${datasetId} ${clusteredTableId}`,
);
assert.include(
output,
`Table ${clusteredTableId} created with clustering.`,
);
assert.include(output, `Table ${clusteredTableId} updated clustering:`);
const [exists] = await bigquery
.dataset(datasetId)
.table(clusteredTableId)
.exists();
assert.ok(exists);
});
it('should create a table with nested schema', async () => {
const output = execSync(
`node nestedRepeatedSchema.js ${datasetId} ${nestedTableId}`,
);
assert.include(output, `Table ${nestedTableId} created.`);
const [exists] = await bigquery
.dataset(datasetId)
.table(nestedTableId)
.exists();
assert.ok(exists);
});
it('should create a table with column-level security', async () => {
const output = execSync(
`node createTableColumnACL.js ${datasetId} ${aclTableId} ${policyTag0.name}`,
);
assert.include(output, `Created table ${aclTableId} with schema:`);
assert.include(output, policyTag0.name);
const [exists] = await bigquery
.dataset(datasetId)
.table(aclTableId)
.exists();
assert.ok(exists);
});
it('should update a table with column-level security', async () => {
const output = execSync(
`node updateTableColumnACL.js ${datasetId} ${aclTableId} ${policyTag1.name}`,
);
assert.include(output, `Updated table ${aclTableId} with schema:`);
assert.include(output, policyTag1.name);
const [exists] = await bigquery
.dataset(datasetId)
.table(aclTableId)
.exists();
assert.ok(exists);
});
it('should retrieve a table if it exists', async () => {
const output = execSync(`node getTable.js ${datasetId} ${tableId}`);
assert.include(output, 'Table:');
assert.include(output, datasetId);
assert.include(output, tableId);
});
it('should check whether a table exists', async () => {
const nonexistentTableId = 'foobar';
const output = execSync(
`node tableExists.js ${datasetId} ${nonexistentTableId}`,
);
assert.include(output, 'Not found');
assert.include(output, datasetId);
assert.include(output, nonexistentTableId);
});
it('should create/update a table with default collation', async () => {
const collationTableId = tableId + '_collation_test';
const [table] = await bigquery
.dataset(datasetId)
.createTable(collationTableId, {
schema: [
{name: 'name', type: 'STRING'},
{name: 'nums', type: 'INTEGER'},
],
defaultCollation: 'und:ci',
expirationTime: testExpirationTime,
});
let [md] = await table.getMetadata();
assert.equal(md.defaultCollation, 'und:ci');
for (const field of md.schema.fields) {
if (field.type === 'STRING') {
assert.equal(field.collation, 'und:ci');
}
}
// update table collation to case sensitive
md.defaultCollation = '';
await table.setMetadata(md);
[md] = await table.getMetadata();
assert.equal(md.defaultCollation, '');
// add field with different collation
md.schema.fields.push({
name: 'another_name',
type: 'STRING',
collation: 'und:ci',
});
await table.setMetadata(md);
[md] = await table.getMetadata();
for (const field of md.schema.fields) {
if (field.type === 'STRING') {
assert.equal(field.collation, 'und:ci');
}
}
});
it('should list tables', async () => {
const output = execSync(`node listTables.js ${datasetId}`);
assert.match(output, /Tables:/);
assert.match(output, new RegExp(tableId));
});
it("should update table's description", async () => {
const output = execSync(
`node updateTableDescription.js ${datasetId} ${tableId}`,
);
assert.include(output, `${tableId} description: New table description.`);
});
it("should update table's expiration", async () => {
const currentTime = Date.now();
const expirationTime = currentTime + 1000 * 60 * 60 * 24 * 5;
const output = execSync(
`node updateTableExpiration.js ${datasetId} ${tableId} ${expirationTime}`,
);
assert.include(output, `${tableId}`);
assert.include(output, `expiration: ${expirationTime}`);
});
it('should add label to a table', async () => {
const output = execSync(`node labelTable.js ${datasetId} ${tableId}`);
assert.include(output, `${tableId} labels:`);
assert.include(output, "{ color: 'green' }");
});
it('should delete a label from a table', async () => {
const output = execSync(`node deleteLabelTable.js ${datasetId} ${tableId}`);
assert.include(output, `${tableId} labels:`);
assert.include(output, 'undefined');
});
it('should load a local CSV file', async () => {
const output = execSync(
`node loadLocalFile.js ${datasetId} ${tableId} ${localFilePath}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.strictEqual(rows.length, 1);
});
it('should browse table rows', async () => {
const browseDestTable = generateUuid();
const output = execSync(
`node browseTable.js ${datasetId} ${browseDestTable}`,
);
assert.match(output, /name/);
assert.match(output, /total people/);
});
it('should extract a table to GCS CSV file', async () => {
const output = execSync(
`node extractTableToGCS.js ${datasetId} ${tableId} ${bucketName} ${exportCSVFileName}`,
);
assert.match(output, /created\./);
const [exists] = await storage
.bucket(bucketName)
.file(exportCSVFileName)
.exists();
assert.ok(exists);
});
it('should extract a table to GCS JSON file', async () => {
const output = execSync(
`node extractTableJSON.js ${datasetId} ${tableId} ${bucketName} ${exportJSONFileName}`,
);
assert.match(output, /created\./);
const [exists] = await storage
.bucket(bucketName)
.file(exportJSONFileName)
.exists();
assert.ok(exists);
});
it('should extract a table to GCS compressed file', async () => {
const output = execSync(
`node extractTableCompressed.js ${datasetId} ${tableId} ${bucketName} ${exportCSVFileName}`,
);
assert.match(output, /created\./);
const [exists] = await storage
.bucket(bucketName)
.file(exportCSVFileName)
.exists();
assert.ok(exists);
});
it('should load a GCS ORC file', async () => {
const tableId = generateUuid();
const output = execSync(`node loadTableGCSORC.js ${datasetId} ${tableId}`);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS Parquet file', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadTableGCSParquet.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS Avro file', async () => {
const tableId = generateUuid();
const output = execSync(`node loadTableGCSAvro.js ${datasetId} ${tableId}`);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS Firestore backup file', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadTableURIFirestore.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS CSV file with explicit schema', async () => {
const tableId = generateUuid();
const output = execSync(`node loadCSVFromGCS.js ${datasetId} ${tableId}`);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS JSON file with explicit schema', async () => {
const tableId = generateUuid();
const output = execSync(`node loadJSONFromGCS.js ${datasetId} ${tableId}`);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS CSV file to partitioned table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadTablePartitioned.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS CSV file to clustered table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadTableClustered.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should add a new column via a GCS file load job', async () => {
const destTableId = generateUuid();
execSync(
`node createTable.js ${datasetId} ${destTableId} 'Name:STRING, Age:INTEGER, Weight:FLOAT'`,
);
const output = execSync(
`node addColumnLoadAppend.js ${datasetId} ${destTableId} ${localFilePath}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should relax a column via a GCS file load job', async () => {
const destTableId = generateUuid();
execSync(`node createTable.js ${datasetId} ${destTableId}`);
const output = execSync(
`node relaxColumnLoadAppend.js ${datasetId} ${destTableId} ${partialDataFilePath}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS CSV file with autodetected schema', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadCSVFromGCSAutodetect.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS JSON file with autodetected schema', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadJSONFromGCSAutodetect.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS CSV file truncate table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadCSVFromGCSTruncate.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
assert.include(output, 'Write disposition used: WRITE_TRUNCATE.');
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS JSON file truncate table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadJSONFromGCSTruncate.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
assert.include(output, 'Write disposition used: WRITE_TRUNCATE.');
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS parquet file truncate table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadParquetFromGCSTruncate.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
assert.include(output, 'Write disposition used: WRITE_TRUNCATE.');
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS ORC file truncate table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadOrcFromGCSTruncate.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
assert.include(output, 'Write disposition used: WRITE_TRUNCATE.');
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should load a GCS Avro file truncate table', async () => {
const tableId = generateUuid();
const output = execSync(
`node loadTableGCSAvroTruncate.js ${datasetId} ${tableId}`,
);
assert.match(output, /completed\./);
assert.include(output, 'Write disposition used: WRITE_TRUNCATE.');
const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows();
assert.ok(rows.length > 0);
});
it('should copy a table', async () => {
const output = execSync(
`node copyTable.js ${srcDatasetId} ${srcTableId} ${destDatasetId} ${destTableId}`,
);
assert.match(output, /completed\./);
const [rows] = await bigquery
.dataset(destDatasetId)
.table(destTableId)
.getRows();
assert.ok(rows.length > 0);
});
it('should insert rows', async () => {
const output = execSync(
`node insertRowsAsStream.js ${datasetId} ${tableId}`,
);
assert.match(output, /Inserted 2 rows/);
});
it('should insert rows with supported data types', async () => {
const typesTableId = generateUuid();
const output = execSync(
`node insertingDataTypes.js ${datasetId} ${typesTableId}`,
);
assert.match(output, /Inserted 2 rows/);
});
it('copy multiple source tables to a given destination', async () => {
execSync(`node createTable.js ${datasetId} destinationTable`);
const output = execSync(
`node copyTableMultipleSource.js ${datasetId} ${tableId} destinationTable`,
);
assert.include(output, 'sourceTable');
assert.include(output, 'destinationTable');
assert.include(output, 'createDisposition');
assert.include(output, 'writeDisposition');
});
it('should add a column to the schema', async () => {
const column = "name: 'size', type: 'STRING'";
const output = execSync(`node addEmptyColumn.js ${datasetId} ${tableId}`);
assert.include(output, column);
});
it("should update a column from 'REQUIRED' TO 'NULLABLE'", async () => {
const column = "name: 'Name', type: 'STRING', mode: 'NULLABLE'";
execSync(`node createTable.js ${datasetId} newTable`);
const output = execSync(`node relaxColumn.js ${datasetId} newTable`);
assert.include(output, column);
});
it('should get labels on a table', async () => {
execSync(`node labelTable.js ${datasetId} ${tableId}`);
const output = execSync(`node getTableLabels.js ${datasetId} ${tableId}`);
assert.include(output, `${tableId} Labels:`);
assert.include(output, 'color: green');
});
describe('Views', () => {
beforeEach(async function () {
this.currentTest.retries(2);
});
it('should create a view', async () => {
const output = execSync(`node createView.js ${datasetId} ${viewId}`);
assert.include(output, `View ${viewId} created.`);
const [exists] = await bigquery.dataset(datasetId).table(viewId).exists();
assert.ok(exists);
});
it('should get a view', async () => {
const viewId = generateUuid();
execSync(`node createView.js ${datasetId} ${viewId}`);
const output = execSync(`node getView.js ${datasetId} ${viewId}`);
assert.match(output, /View at/);
assert.match(output, /View query:/);
});
it('should update a view', async () => {
const output = execSync(`node updateViewQuery.js ${datasetId} ${viewId}`);
assert.include(output, `View ${viewId} updated.`);
});
});
describe('Delete Table', () => {
const datasetId = `gcloud_tests_${randomUUID()}`.replace(/-/gi, '_');
const tableId = `gcloud_tests_${randomUUID()}`.replace(/-/gi, '_');
before(async () => {
const datasetOptions = {
location: 'US',
};
const tableOptions = {
location: 'US',
};
await bigquery.createDataset(datasetId, datasetOptions);
// Create a new table in the dataset
await bigquery.dataset(datasetId).createTable(tableId, tableOptions);
});
beforeEach(async function () {
this.currentTest.retries(2);
});
after(async () => {
await bigquery
.dataset(datasetId)
.delete({force: true})
.catch(console.warn);
});
it('should delete a table', async () => {
const output = execSync(`node deleteTable.js ${datasetId} ${tableId}`);
assert.include(output, `Table ${tableId} deleted.`);
const [exists] = await bigquery
.dataset(datasetId)
.table(tableId)
.exists();
assert.strictEqual(exists, false);
});
it('should undelete a table', async () => {
const tableId = generateUuid();
const recoveredTableId = generateUuid();
execSync(`node createTable.js ${datasetId} ${tableId}`);
const output = execSync(
`node undeleteTable.js ${datasetId} ${tableId} ${recoveredTableId}`,
);
assert.include(output, `Table ${tableId} deleted.`);
assert.match(output, /Copied data from deleted table/);
const [exists] = await bigquery
.dataset(datasetId)
.table(recoveredTableId)
.exists();
assert.strictEqual(exists, true);
});
});
// Only delete a resource if it is older than 24 hours. That will prevent
// collisions with parallel CI test runs.
function isResourceStale(creationTime) {
const oneDayMs = 86400000;
const now = new Date();
const created = new Date(creationTime * 1000);
return now.getTime() - created.getTime() >= oneDayMs;
}
async function deleteStaleTaxonomies() {
const location = 'us';
const listTaxonomiesRequest = {
parent: dataCatalog.locationPath(projectId, location),
};
let [taxonomies] = await policyTagManager.listTaxonomies(
listTaxonomiesRequest,
);
taxonomies = taxonomies.filter(taxonomy => {
return taxonomy.displayName.includes(GCLOUD_TESTS_PREFIX);
});
taxonomies.forEach(async taxonomy => {
if (isResourceStale(taxonomy.taxonomyTimestamps.createTime.seconds)) {
try {
await policyTagManager.deleteTaxonomy({name: taxonomy.name});
} catch (e) {
console.error(e);
}
}
});
}
});