Skip to content

Commit 250ed79

Browse files
chore(bigquery): testing, stylistic update
1 parent a49dfdd commit 250ed79

File tree

4 files changed

+151
-124
lines changed

4 files changed

+151
-124
lines changed

bigquery/cloud-client/test/viewDatasetAccessPolicy.test.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,59 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
'use strict';
16+
17+
const {beforeEach, afterEach, it, describe} = require('mocha');
1518
const assert = require('assert');
16-
const {getDataset, setupBeforeAll, teardownAfterAll} = require('./config');
19+
const sinon = require('sinon');
20+
21+
const {setupBeforeAll, cleanupResources} = require('./config');
1722
const {viewDatasetAccessPolicy} = require('../viewDatasetAccessPolicy');
1823

1924
describe('viewDatasetAccessPolicy', () => {
20-
before(async () => {
21-
await setupBeforeAll();
25+
let datasetId = null;
26+
27+
beforeEach(async () => {
28+
const response = await setupBeforeAll();
29+
datasetId = response.datasetId;
30+
31+
sinon.stub(console, 'log');
32+
sinon.stub(console, 'error');
2233
});
2334

24-
after(async () => {
25-
await teardownAfterAll();
35+
afterEach(async () => {
36+
await cleanupResources(datasetId);
37+
console.log.restore();
38+
console.error.restore();
2639
});
2740

2841
it('should view dataset access policies', async () => {
29-
const dataset = await getDataset();
30-
const accessPolicy = await viewDatasetAccessPolicy(dataset.id);
42+
// Act: View the dataset access policy
43+
await viewDatasetAccessPolicy(datasetId);
44+
45+
// Assert: Check that the initial message was logged
46+
assert.strictEqual(
47+
console.log.calledWith(
48+
sinon.match(`Access entries in dataset '${datasetId}':`)
49+
),
50+
true
51+
);
52+
53+
// We're not checking the exact number of entries since that might vary,
54+
// but we're making sure the appropriate logging format was followed
55+
assert.ok(
56+
console.log.calledWith(sinon.match(/Role:/)),
57+
'Should log role information'
58+
);
59+
60+
assert.ok(
61+
console.log.calledWith(sinon.match(/Special group:/)),
62+
'Should log special group information'
63+
);
3164

32-
assert.ok(accessPolicy, 'Access policy should be defined');
33-
assert.ok(Array.isArray(accessPolicy), 'Access policy should be an array');
65+
assert.ok(
66+
console.log.calledWith(sinon.match(/User by Email:/)),
67+
'Should log user by email information'
68+
);
3469
});
3570
});

bigquery/cloud-client/test/viewTableOrViewAccessPolicy.test.js

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,90 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
'use strict';
16+
17+
const {describe, it, beforeEach, afterEach} = require('mocha');
1518
const assert = require('assert');
16-
const {
17-
getProjectId,
18-
getDataset,
19-
getTable,
20-
getView,
21-
setupBeforeAll,
22-
teardownAfterAll,
23-
} = require('./config.js');
24-
const viewTableOrViewAccessPolicy = require('../viewTableOrViewAccessPolicy.js');
19+
const sinon = require('sinon');
20+
21+
const {setupBeforeAll, cleanupResources} = require('./config');
22+
const {viewTableOrViewAccessPolicy} = require('../viewTableOrViewAccessPolicy');
2523

2624
describe('viewTableOrViewAccessPolicy', () => {
27-
before(async () => {
28-
await setupBeforeAll();
25+
let datasetId = null;
26+
let tableId = null;
27+
let viewId = null;
28+
const projectId = process.env.GCLOUD_PROJECT;
29+
30+
beforeEach(async () => {
31+
const response = await setupBeforeAll();
32+
datasetId = response.datasetId;
33+
tableId = response.tableId;
34+
viewId = response.viewId;
35+
36+
sinon.stub(console, 'log');
37+
sinon.stub(console, 'error');
2938
});
3039

31-
after(async () => {
32-
await teardownAfterAll();
40+
afterEach(async () => {
41+
await cleanupResources(datasetId);
42+
console.log.restore();
43+
console.error.restore();
3344
});
3445

3546
it('should view table access policies', async () => {
36-
const projectId = await getProjectId();
37-
const dataset = await getDataset();
38-
const table = await getTable();
39-
40-
const policy = await viewTableOrViewAccessPolicy(
41-
projectId,
42-
dataset.id,
43-
table.id
44-
);
47+
// View the table access policy
48+
await viewTableOrViewAccessPolicy(projectId, datasetId, tableId);
4549

46-
// Verify that the policy exists.
47-
assert.ok(policy, 'Policy should be defined');
50+
// Check that the right messages were logged
51+
assert.strictEqual(
52+
console.log.calledWith(
53+
`Access Policy details for table or view '${tableId}'.`
54+
),
55+
true
56+
);
4857

49-
// Verify that bindings exists and is an array.
50-
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
58+
assert.ok(
59+
console.log.calledWith(sinon.match('Bindings:')),
60+
'Should log bindings information'
61+
);
5162

52-
// In a new policy, bindings should be empty.
53-
assert.strictEqual(
54-
policy.bindings.length,
55-
0,
56-
'Bindings list should be empty'
63+
assert.ok(
64+
console.log.calledWith(sinon.match('etag:')),
65+
'Should log etag information'
5766
);
5867

59-
// Verify that etag exists, but do not validate its exact value.
60-
assert.ok(policy.etag, 'Etag should be defined');
68+
assert.ok(
69+
console.log.calledWith(sinon.match('Version:')),
70+
'Should log version information'
71+
);
6172
});
6273

6374
it('should view view access policies', async () => {
64-
const projectId = await getProjectId();
65-
const dataset = await getDataset();
66-
const view = await getView();
67-
68-
const policy = await viewTableOrViewAccessPolicy(
69-
projectId,
70-
dataset.id,
71-
view.id
72-
);
75+
// View the view access policy
76+
await viewTableOrViewAccessPolicy(projectId, datasetId, viewId);
7377

74-
// Verify that the policy exists.
75-
assert.ok(policy, 'Policy should be defined');
78+
// Check that the right messages were logged
79+
assert.strictEqual(
80+
console.log.calledWith(
81+
`Access Policy details for table or view '${viewId}'.`
82+
),
83+
true
84+
);
7685

77-
// Verify that bindings exists and is an array.
78-
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
86+
assert.ok(
87+
console.log.calledWith(sinon.match('Bindings:')),
88+
'Should log bindings information'
89+
);
7990

80-
// In a new policy, bindings should be empty.
81-
assert.strictEqual(
82-
policy.bindings.length,
83-
0,
84-
'Bindings list should be empty'
91+
assert.ok(
92+
console.log.calledWith(sinon.match('etag:')),
93+
'Should log etag information'
8594
);
8695

87-
// Verify that etag exists, but do not validate its exact value.
88-
assert.ok(policy.etag, 'Etag should be defined');
96+
assert.ok(
97+
console.log.calledWith(sinon.match('Version:')),
98+
'Should log version information'
99+
);
89100
});
90101
});

bigquery/cloud-client/viewDatasetAccessPolicy.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,26 @@
1414

1515
'use strict';
1616

17-
/**
18-
* View access policies for a BigQuery dataset.
19-
* @param {string} datasetId Dataset ID to view access policies for.
20-
* @returns {Array} Array of access entries.
21-
*/
22-
function viewDatasetAccessPolicy(datasetId) {
17+
async function main(datasetId) {
2318
// [START bigquery_view_dataset_access_policy]
19+
20+
/**
21+
* TODO(developer): Update and un-comment below lines
22+
*/
23+
// const datasetId = "my_project_id.my_dataset";
24+
2425
const {BigQuery} = require('@google-cloud/bigquery');
2526

2627
// Instantiate a client.
2728
const bigquery = new BigQuery();
2829

29-
// TODO (developer): Update and un-comment below lines.
30-
31-
// Dataset from which to get the access policy.
32-
// datasetId = "my_dataset_id";
30+
async function viewDatasetAccessPolicy() {
31+
const dataset = bigquery.dataset(datasetId);
3332

34-
// Get a reference to the dataset.
35-
const dataset = bigquery.dataset(datasetId);
36-
37-
return dataset.getMetadata().then(([metadata]) => {
33+
const [metadata] = await dataset.getMetadata();
3834
const accessEntries = metadata.access || [];
3935

40-
// Show the array of AccessEntry objects.
36+
// Show the list of AccessEntry objects.
4137
// More details about the AccessEntry object in the BigQuery documentation:
4238
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
4339
console.log(
@@ -48,12 +44,9 @@ function viewDatasetAccessPolicy(datasetId) {
4844
console.log(`Special group: ${accessEntry.specialGroup || 'null'}`);
4945
console.log(`User by Email: ${accessEntry.userByEmail || 'null'}`);
5046
}
51-
52-
return accessEntries;
53-
});
47+
}
5448
// [END bigquery_view_dataset_access_policy]
49+
await viewDatasetAccessPolicy();
5550
}
5651

57-
module.exports = {
58-
viewDatasetAccessPolicy,
59-
};
52+
exports.viewDatasetAccessPolicy = main;

bigquery/cloud-client/viewTableOrViewAccessPolicy.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,43 @@
1414

1515
'use strict';
1616

17-
/**
18-
* View access policy for a BigQuery table or view.
19-
*
20-
* @param {string} projectId Google Cloud Platform project.
21-
* @param {string} datasetId Dataset where the table or view is.
22-
* @param {string} resourceName Table or view name to get the access policy.
23-
* @returns {Promise<object>} The IAM policy object.
24-
*/
25-
async function viewTableOrViewAccessPolicy(projectId, datasetId, resourceName) {
17+
async function main(projectId, datasetId, resourceName) {
2618
// [START bigquery_view_table_or_view_access_policy]
27-
const {BigQuery} = require('@google-cloud/bigquery');
28-
29-
// TODO(developer): Update and un-comment below lines.
3019

31-
// Google Cloud Platform project.
32-
// projectId = "my_project_id";
20+
/**
21+
* TODO(developer): Update and un-comment below lines
22+
*/
23+
// const projectId = "YOUR_PROJECT_ID"
24+
// const datasetId = "YOUR_DATASET_ID"
25+
// const resourceName = "YOUR_RESOURCE_NAME";
3326

34-
// Dataset where the table or view is.
35-
// datasetId = "my_dataset_id";
36-
37-
// Table or view name to get the access policy.
38-
// resourceName = "my_table_name_id";
27+
const {BigQuery} = require('@google-cloud/bigquery');
3928

4029
// Instantiate a client.
4130
const client = new BigQuery();
4231

43-
// Get a reference to the dataset by datasetId.
44-
const dataset = client.dataset(datasetId);
45-
// Get a reference to the table by tableName.
46-
const table = dataset.table(resourceName);
47-
48-
// Get the IAM access policy for the table or view.
49-
const [policy] = await table.getIamPolicy();
50-
51-
// Initialize bindings array.
52-
if (!policy.bindings) {
53-
policy.bindings = [];
32+
async function viewTableOrViewAccessPolicy() {
33+
const dataset = client.dataset(datasetId);
34+
const table = dataset.table(resourceName);
35+
36+
// Get the IAM access policy for the table or view.
37+
const [policy] = await table.getIamPolicy();
38+
39+
// Initialize bindings if they don't exist
40+
if (!policy.bindings) {
41+
policy.bindings = [];
42+
}
43+
44+
// Show policy details.
45+
// Find more details for the Policy object here:
46+
// https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
47+
console.log(`Access Policy details for table or view '${resourceName}'.`);
48+
console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`);
49+
console.log(`etag: ${policy.etag}`);
50+
console.log(`Version: ${policy.version}`);
5451
}
55-
56-
// Show policy details
57-
// Find more details for the Policy object here:
58-
// https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
59-
console.log(`Access Policy details for table or view '${resourceName}'.`);
60-
console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`);
61-
console.log(`etag: ${policy.etag}`);
62-
console.log(`Version: ${policy.version}`);
63-
6452
// [END bigquery_view_table_or_view_access_policy]
65-
return policy;
53+
await viewTableOrViewAccessPolicy();
6654
}
6755

68-
module.exports = viewTableOrViewAccessPolicy;
56+
exports.viewTableOrViewAccessPolicy = main;

0 commit comments

Comments
 (0)