-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathqueries.test.js
More file actions
237 lines (206 loc) · 7.48 KB
/
Copy pathqueries.test.js
File metadata and controls
237 lines (206 loc) · 7.48 KB
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
// Copyright 2016, Google, Inc.
// 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';
var proxyquire = require('proxyquire').noCallThru();
function getSample () {
var natalityMock = [
{ year: '2001' },
{ year: '2002' },
{ year: '2003' },
{ year: '2004' },
{ year: '2005' }
];
var metadataMock = { status: { state: 'DONE' } };
var jobId = 'abc';
var jobMock = {
id: jobId,
getQueryResults: sinon.stub().callsArgWith(0, null, natalityMock),
getMetadata: sinon.stub().callsArgWith(0, null, metadataMock)
};
var bigqueryMock = {
job: sinon.stub().returns(jobMock),
startQuery: sinon.stub().callsArgWith(1, null, jobMock),
query: sinon.stub().callsArgWith(1, null, natalityMock)
};
var BigQueryMock = sinon.stub().returns(bigqueryMock);
return {
program: proxyquire('../queries', {
'@google-cloud/bigquery': BigQueryMock
}),
mocks: {
BigQuery: BigQueryMock,
bigquery: bigqueryMock,
natality: natalityMock,
metadata: metadataMock,
job: jobMock
},
jobId: jobId
};
}
describe('bigquery:query', function () {
describe('syncQuery', function () {
var query = 'foo';
it('should return results', function () {
var example = getSample();
example.program.syncQuery(query,
function (err, data) {
assert.ifError(err);
assert(example.mocks.bigquery.query.called);
assert.deepEqual(data, example.mocks.natality);
assert(console.log.calledWith(
'SyncQuery: found %d rows!',
data.length
));
}
);
});
it('should require a query', function () {
var example = getSample();
example.program.syncQuery(null, function (err, data) {
assert.deepEqual(err, Error('"query" is required!'));
assert.equal(data, undefined);
});
});
it('should handle error', function () {
var error = new Error('error');
var example = getSample();
example.mocks.bigquery.query = sinon.stub().callsArgWith(1, error);
example.program.syncQuery(query, function (err, data) {
assert.deepEqual(err, error);
assert.equal(data, undefined);
});
});
});
describe('asyncQuery', function () {
var query = 'foo';
it('should submit a job', function () {
var example = getSample();
example.program.asyncQuery(query,
function (err, job) {
assert.ifError(err);
assert(example.mocks.bigquery.startQuery.called);
assert.deepEqual(example.mocks.job, job);
assert(console.log.calledWith(
'AsyncQuery: submitted job %s!', example.jobId
));
}
);
});
it('should require a query', function () {
var example = getSample();
example.program.asyncQuery(null, function (err, job) {
assert.deepEqual(err, Error('"query" is required!'));
assert.equal(job, undefined);
});
});
it('should handle error', function () {
var error = new Error('error');
var example = getSample();
example.mocks.bigquery.startQuery = sinon.stub().callsArgWith(1, error);
example.program.asyncQuery(query, function (err, job) {
assert.deepEqual(err, error);
assert.equal(job, undefined);
});
});
});
describe('asyncPoll', function () {
it('should get the results of a job given its ID', function () {
var example = getSample();
example.mocks.bigquery.job = sinon.stub().returns(example.mocks.job);
example.program.asyncPoll(example.jobId,
function (err, rows) {
assert.ifError(err);
assert(example.mocks.job.getQueryResults.called);
assert(console.log.calledWith(
'AsyncQuery: polled job %s; got %d rows!',
example.jobId,
example.mocks.natality.length
));
}
);
});
it('should report the status of a job', function () {
var example = getSample();
example.program.asyncPoll(example.jobId, function (err, rows) {
assert.ifError(err);
assert(example.mocks.job.getMetadata.called);
assert(console.log.calledWith(
'Job status: %s',
example.mocks.metadata.status.state
));
});
});
it('should check whether a job is finished', function () {
var example = getSample();
var pendingState = { status: { state: 'PENDING' } };
example.mocks.job.getMetadata = sinon.stub().callsArgWith(0, null, pendingState);
example.program.asyncPoll(example.jobId, function (err, rows) {
assert.deepEqual(err, Error('Job %s is not done', example.jobId));
assert(console.log.calledWith('Job status: %s', pendingState.status.state));
assert(example.mocks.job.getMetadata.called);
assert.equal(example.mocks.job.getQueryResults.called, false);
assert.equal(rows, undefined);
});
var doneState = { status: { state: 'DONE' } };
example.mocks.job.getMetadata = sinon.stub().callsArgWith(0, null, doneState);
example.program.asyncPoll(example.jobId, function (err, rows) {
assert.ifError(err);
assert(console.log.calledWith('Job status: %s', doneState.status.state));
assert(example.mocks.job.getMetadata.called);
assert(example.mocks.job.getQueryResults.called);
assert.equal(rows, example.mocks.natality);
});
});
it('should require a job ID', function () {
var example = getSample();
example.program.asyncPoll(null, function (err, rows) {
assert.deepEqual(err, Error('"jobId" is required!'));
assert.equal(rows, undefined);
});
});
it('should handle error', function () {
var error = new Error('error');
var example = getSample();
example.mocks.job.getQueryResults = sinon.stub().callsArgWith(0, error);
example.program.asyncPoll(example.jobId, function (err, rows) {
assert.deepEqual(err, error);
assert.equal(rows, undefined);
});
});
});
describe('main', function () {
var query = 'foo';
var jobId = 'foo';
it('should call syncQuery', function () {
var program = getSample().program;
sinon.stub(program, 'syncQuery');
program.main(['sync', query]);
assert.equal(program.syncQuery.calledOnce, true);
assert.deepEqual(program.syncQuery.firstCall.args.slice(0, -1), [query]);
});
it('should call asyncQuery', function () {
var program = getSample().program;
sinon.stub(program, 'asyncQuery');
program.main(['async', query]);
assert.equal(program.asyncQuery.calledOnce, true);
assert.deepEqual(program.asyncQuery.firstCall.args.slice(0, -1), [query]);
});
it('should call asyncPoll', function () {
var program = getSample().program;
sinon.stub(program, 'asyncPoll');
program.main(['poll', jobId]);
assert.equal(program.asyncPoll.calledOnce, true);
assert.deepEqual(program.asyncPoll.firstCall.args.slice(0, -1), [jobId]);
});
});
});