Skip to content

Commit

Permalink
Merge branch 'main' into renovate/sinon-17.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sofisl authored Feb 6, 2024
2 parents aac337c + e9f133b commit 5168bea
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ export class BigQuery extends Service {
'GEOGRAPHY',
'ARRAY',
'STRUCT',
'JSON',
'RANGE',
];

if (is.array(providedType)) {
Expand Down Expand Up @@ -1182,6 +1184,8 @@ export class BigQuery extends Service {
},
{}
);
} else if (typeName === 'JSON' && is.object(value)) {
queryParameter.parameterValue!.value = JSON.stringify(value);
} else {
queryParameter.parameterValue!.value = BigQuery._getValue(
value,
Expand Down Expand Up @@ -1420,7 +1424,7 @@ export class BigQuery extends Service {

query.destinationTable = {
datasetId: options.destination.dataset.id,
projectId: options.destination.dataset.bigQuery.projectId,
projectId: options.destination.dataset.projectId,
tableId: options.destination.id,
};

Expand Down
9 changes: 6 additions & 3 deletions src/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export type TableCallback = ResourceCallback<Table, bigquery.ITable>;
class Dataset extends ServiceObject {
bigQuery: BigQuery;
location?: string;
projectId?: string;
projectId: string;
getModelsStream(options?: GetModelsOptions): ResourceStream<Model> {

Check warning on line 127 in src/dataset.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Model>({}, () => {});
Expand Down Expand Up @@ -389,6 +389,8 @@ class Dataset extends ServiceObject {

if (options?.projectId) {
this.projectId = options.projectId;
} else {
this.projectId = bigQuery.projectId;
}

this.bigQuery = bigQuery;
Expand Down Expand Up @@ -642,7 +644,7 @@ class Dataset extends ServiceObject {
routineReference: {
routineId: id,
datasetId: this.id,
projectId: this.bigQuery.projectId,
projectId: this.projectId,
},
});

Expand Down Expand Up @@ -740,7 +742,7 @@ class Dataset extends ServiceObject {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(body as any).tableReference = {
datasetId: this.id,
projectId: this.bigQuery.projectId,
projectId: this.projectId,
tableId: id,
};

Expand Down Expand Up @@ -1303,6 +1305,7 @@ class Dataset extends ServiceObject {
options = extend(
{
location: this.location,
projectId: this.projectId,
},
options
);
Expand Down
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ class Model extends ServiceObject {
extract: extend(true, options, {
sourceModel: {
datasetId: this.dataset.id,
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
modelId: this.id,
},
}),
Expand Down
18 changes: 9 additions & 9 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,12 @@ class Table extends ServiceObject {
copy: extend(true, metadata, {
destinationTable: {
datasetId: destination.dataset.id,
projectId: destination.bigQuery.projectId,
projectId: destination.dataset.projectId,
tableId: destination.id,
},
sourceTable: {
datasetId: this.dataset.id,
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
tableId: this.id,
},
}),
Expand Down Expand Up @@ -1051,14 +1051,14 @@ class Table extends ServiceObject {
copy: extend(true, metadata, {
destinationTable: {
datasetId: this.dataset.id,
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
tableId: this.id,
},

sourceTables: sourceTables.map(sourceTable => {
return {
datasetId: sourceTable.dataset.id,
projectId: sourceTable.bigQuery.projectId,
projectId: sourceTable.dataset.projectId,
tableId: sourceTable.id,
};
}),
Expand Down Expand Up @@ -1224,7 +1224,7 @@ class Table extends ServiceObject {
extract: extend(true, options, {
sourceTable: {
datasetId: this.dataset.id,
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
tableId: this.id,
},
}),
Expand Down Expand Up @@ -1404,7 +1404,7 @@ class Table extends ServiceObject {
configuration: {
load: {
destinationTable: {
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
datasetId: this.dataset.id,
tableId: this.id,
},
Expand Down Expand Up @@ -1510,7 +1510,7 @@ class Table extends ServiceObject {
true,
{
destinationTable: {
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
datasetId: this.dataset.id,
tableId: this.id,
},
Expand Down Expand Up @@ -1542,12 +1542,12 @@ class Table extends ServiceObject {
},
jobReference: {
jobId,
projectId: this.bigQuery.projectId,
projectId: this.dataset.projectId,
location: this.location,
},
} as {},
request: {
uri: `${this.bigQuery.apiEndpoint}/upload/bigquery/v2/projects/${this.bigQuery.projectId}/jobs`,
uri: `${this.bigQuery.apiEndpoint}/upload/bigquery/v2/projects/${this.dataset.projectId}/jobs`,
},
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
29 changes: 28 additions & 1 deletion test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,33 @@ describe('BigQuery', () => {
assert.deepStrictEqual(param, expectedParam);
});

it('should format JSON types', () => {
const typeName = 'JSON';
const value = {
foo: 'bar',
};
const strValue = JSON.stringify(value);
assert.deepStrictEqual(BigQuery.valueToQueryParameter_(value, typeName), {
parameterType: {
type: typeName,
},
parameterValue: {
value: strValue,
},
});
assert.deepStrictEqual(
BigQuery.valueToQueryParameter_(strValue, typeName),
{
parameterType: {
type: typeName,
},
parameterValue: {
value: strValue,
},
}
);
});

it('should format all other types', () => {
const typeName = 'ANY-TYPE';
sandbox.stub(BigQuery, 'getTypeDescriptorFromValue_').returns({
Expand Down Expand Up @@ -1977,7 +2004,7 @@ describe('BigQuery', () => {
reqOpts.json.configuration.query.destinationTable,
{
datasetId: dataset.id,
projectId: dataset.bigQuery.projectId,
projectId: dataset.projectId,
tableId: TABLE_ID,
}
);
Expand Down
61 changes: 56 additions & 5 deletions test/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('BigQuery/Dataset', () => {
} as {} as _root.BigQuery;
const DATASET_ID = 'kittens';
const LOCATION = 'asia-northeast1';
const ANOTHER_PROJECT_ID = 'another-test-project';

// tslint:disable-next-line variable-name
let Dataset: typeof _root.Dataset;
Expand Down Expand Up @@ -148,6 +149,12 @@ describe('BigQuery/Dataset', () => {
assert.strictEqual(ds.location, LOCATION);
});

it('should set the client projectId by default', () => {
const ds = new Dataset(BIGQUERY, DATASET_ID);

assert.strictEqual(ds.projectId, BIGQUERY.projectId);
});

it('should capture user provided projectId', () => {
const projectIdOverride = 'octavia';
const options = {projectId: projectIdOverride};
Expand All @@ -171,7 +178,9 @@ describe('BigQuery/Dataset', () => {
});

it('should call through to BigQuery#createDataset', done => {
const OPTIONS = {};
const OPTIONS = {
projectId: BIGQUERY.projectId,
};

bq.createDataset = (id: string, options: {}, callback: Function) => {
assert.strictEqual(id, DATASET_ID);
Expand Down Expand Up @@ -249,6 +258,7 @@ describe('BigQuery/Dataset', () => {
json: {
etag: FAKE_ETAG,
},
uri: `/projects/${BIGQUERY.projectId}/`,
};

const reqOpts = interceptor.request(fakeReqOpts);
Expand All @@ -266,6 +276,7 @@ describe('BigQuery/Dataset', () => {
json: {
etag: FAKE_ETAG,
},
uri: `/projects/${BIGQUERY.projectId}/`,
};

const expectedHeaders = Object.assign({}, fakeReqOpts.headers, {
Expand All @@ -284,6 +295,7 @@ describe('BigQuery/Dataset', () => {
json: {
etag: FAKE_ETAG,
},
uri: `/projects/${BIGQUERY.projectId}/`,
};

const reqOpts = interceptor.request(fakeReqOpts);
Expand Down Expand Up @@ -428,6 +440,7 @@ describe('BigQuery/Dataset', () => {
const API_RESPONSE = {
tableReference: {
tableId: TABLE_ID,
projectId: BIGQUERY.projectId,
},
};

Expand All @@ -443,10 +456,7 @@ describe('BigQuery/Dataset', () => {
const body = reqOpts.json;
assert.deepStrictEqual(body.schema, SCHEMA_OBJECT);
assert.strictEqual(body.tableReference.datasetId, DATASET_ID);
assert.strictEqual(
body.tableReference.projectId,
ds.bigQuery.projectId
);
assert.strictEqual(body.tableReference.projectId, ds.projectId);
assert.strictEqual(body.tableReference.tableId, TABLE_ID);

done();
Expand All @@ -455,6 +465,31 @@ describe('BigQuery/Dataset', () => {
ds.createTable(TABLE_ID, options, assert.ifError);
});

it('should create a table on a different project', done => {
const options = {
schema: SCHEMA_OBJECT,
};
const anotherDs = new Dataset(BIGQUERY, DATASET_ID, {
projectId: ANOTHER_PROJECT_ID,
}) as any;
anotherDs.request = (reqOpts: DecorateRequestOptions) => {
assert.strictEqual(reqOpts.method, 'POST');
assert.strictEqual(reqOpts.uri, '/tables');

const body = reqOpts.json;
assert.deepStrictEqual(body.schema, SCHEMA_OBJECT);
assert.strictEqual(body.tableReference.datasetId, DATASET_ID);
assert.strictEqual(body.tableReference.projectId, ANOTHER_PROJECT_ID);
assert.strictEqual(body.tableReference.tableId, TABLE_ID);

done();
};

// Under the hood dataset.createTable is called
const table = anotherDs.table(TABLE_ID);
table.create(options, assert.ifError);
});

it('should not require options', done => {
ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
callback(null, API_RESPONSE);
Expand Down Expand Up @@ -577,6 +612,22 @@ describe('BigQuery/Dataset', () => {
ds.createTable(TABLE_ID, {schema: SCHEMA_OBJECT}, assert.ifError);
});

it('should pass the projectId to the Table', done => {
const response = Object.assign({location: LOCATION}, API_RESPONSE);

ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => {
callback(null, response);
};

ds.table = (id: string, options: TableOptions) => {
assert.strictEqual(options.location, LOCATION);
setImmediate(done);
return {};
};

ds.createTable(TABLE_ID, {schema: SCHEMA_OBJECT}, assert.ifError);
});

it('should return an apiResponse', done => {
const opts = {id: TABLE_ID, schema: SCHEMA_OBJECT};

Expand Down
4 changes: 2 additions & 2 deletions test/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('BigQuery/Model', () => {

const DATASET = {
id: 'dataset-id',
projectId: 'project-id',
createTable: util.noop,
bigQuery: {
projectId: 'project-id',
job: (id: string) => {
return {id};
},
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('BigQuery/Model', () => {
model.bigQuery.createJob = (reqOpts: JobOptions) => {
assert.deepStrictEqual(reqOpts.configuration!.extract!.sourceModel, {
datasetId: model.dataset.id,
projectId: model.bigQuery.projectId,
projectId: model.dataset.projectId,
modelId: model.id,
});

Expand Down
Loading

0 comments on commit 5168bea

Please sign in to comment.