Skip to content

Commit

Permalink
fix: refactor to extract options from query (#1045)
Browse files Browse the repository at this point in the history
* fix: refactor to extract options from query

* lint
  • Loading branch information
steffnay committed Dec 13, 2021
1 parent 4652623 commit 4afed77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
36 changes: 18 additions & 18 deletions src/bigquery.ts
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
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

0 comments on commit 4afed77

Please sign in to comment.