Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor to extract options from query #1045

Merged
merged 7 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2045,28 +2045,28 @@ export class BigQuery extends Service {
*
* @private
*/
queryAsStream_(
query: Query,
optionsOrCallback?: QueryStreamOptions,
cb?: SimpleQueryRowsCallback
) {
let options =
// Default to using query as options to ensure the options set
// in a Query type are passed through to the query() method.
typeof optionsOrCallback === 'object' ? optionsOrCallback : query;
const callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;

options = query.job
? extend(query, options)
: extend(options, {autoPaginate: false});

queryAsStream_(query: Query, callback?: SimpleQueryRowsCallback) {
if (query.job) {
query.job!.getQueryResults(options, callback as QueryRowsCallback);
query.job.getQueryResults(query, callback as QueryRowsCallback);
return;
}

this.query(query, options, callback);
const {location, maxResults, pageToken, wrapIntegers} = query;

const opts = {
location,
maxResults,
pageToken,
wrapIntegers,
autoPaginate: false,
};

delete query.location;
delete query.maxResults;
delete query.pageToken;
delete query.wrapIntegers;

this.query(query, opts, callback);
}
}

Expand Down
44 changes: 21 additions & 23 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,13 @@ describe('BigQuery', () => {

describe('queryAsStream_', () => {
let queryStub: SinonStub;
let defaultOpts = {
location: undefined,
maxResults: undefined,
pageToken: undefined,
wrapIntegers: undefined,
autoPaginate: false,
};

beforeEach(() => {
queryStub = sandbox.stub(bq, 'query').callsArgAsync(2);
Expand All @@ -2715,23 +2722,16 @@ describe('BigQuery', () => {
const query = 'SELECT';
bq.queryAsStream_(query, done);
assert(
queryStub.calledOnceWithExactly(
query,
{autoPaginate: false},
sinon.match.func
)
queryStub.calledOnceWithExactly(query, defaultOpts, sinon.match.func)
);
});

it('should call query correctly with a Query object', done => {
const query = {query: 'SELECT', wrapIntegers: true};
bq.queryAsStream_(query, done);
defaultOpts = extend(defaultOpts, {wrapIntegers: true});
assert(
queryStub.calledOnceWithExactly(
query,
extend(query, {autoPaginate: false}),
sinon.match.func
)
queryStub.calledOnceWithExactly(query, defaultOpts, sinon.match.func)
);
});

Expand All @@ -2748,22 +2748,20 @@ describe('BigQuery', () => {
});

it('should pass wrapIntegers if supplied', done => {
const statement = 'SELECT';
const query = {
query: statement,
const wrapIntegers = {
integerValue: 100,
};
const options = {
wrapIntegers: {
integerValue: 100,
},
const query = {
query: 'SELECT',
wrapIntegers,
};
bq.queryAsStream_(query, options, done);

bq.queryAsStream_(query, done);

defaultOpts = extend(defaultOpts, {wrapIntegers});

assert(
queryStub.calledOnceWithExactly(
query,
{autoPaginate: false, wrapIntegers: options.wrapIntegers},
sinon.match.func
)
queryStub.calledOnceWithExactly(query, defaultOpts, sinon.match.func)
);
});
});
Expand Down