Skip to content

Commit 536e286

Browse files
bigquery: add tests
1 parent b788e4b commit 536e286

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

packages/bigquery/src/table.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ Table.prototype.import = function(source, metadata, callback) {
10211021
* // Some rows failed to insert, while others may have succeeded.
10221022
*
10231023
* // err.errors (object[]):
1024-
* // err.errors[].row (original individual row object passed to `insert`)
1024+
* // err.errors[].row (original row object passed to `insert`)
10251025
* // err.errors[].errors[].reason
10261026
* // err.errors[].errors[].message
10271027
*
@@ -1063,6 +1063,11 @@ Table.prototype.insert = function(rows, options, callback) {
10631063
uri: '/insertAll',
10641064
json: json
10651065
}, function(err, resp) {
1066+
if (err) {
1067+
callback(err, resp);
1068+
return;
1069+
}
1070+
10661071
var partialFailures = (resp.insertErrors || []).map(function(insertError) {
10671072
return {
10681073
errors: insertError.errors.map(function(error) {
@@ -1071,23 +1076,18 @@ Table.prototype.insert = function(rows, options, callback) {
10711076
reason: error.reason
10721077
};
10731078
}),
1074-
row: json.rows[insertError.index].json
1079+
row: rows[insertError.index]
10751080
};
10761081
});
10771082

1078-
if (failedToInsert.length > 0) {
1083+
if (partialFailures.length > 0) {
10791084
err = new common.util.PartialFailureError({
10801085
errors: partialFailures,
10811086
response: resp
10821087
});
10831088
}
10841089

1085-
if (err) {
1086-
callback(err, null, resp);
1087-
return;
1088-
}
1089-
1090-
callback(null, failedToInsert, resp);
1090+
callback(err, resp);
10911091
});
10921092
};
10931093

packages/bigquery/system-test/bigquery.js

+50-6
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,9 @@ describe('BigQuery', function() {
500500
}
501501
};
502502

503-
table.insert(data, function(err, insertErrors) {
503+
table.insert(data, function(err) {
504504
assert.ifError(err);
505505

506-
if (insertErrors.length > 0) {
507-
done(insertErrors[0].errors[0]);
508-
return;
509-
}
510-
511506
function query(callback) {
512507
var query = {
513508
query: 'SELECT * FROM ' + table.id + ' WHERE id = ' + data.id,
@@ -533,6 +528,55 @@ describe('BigQuery', function() {
533528
});
534529
});
535530

531+
it('should return partial errors', function(done) {
532+
var data = {
533+
name: 'dave',
534+
breed: 'british shorthair',
535+
id: 99,
536+
dob: new Date(),
537+
around: true,
538+
buffer: new Buffer('test'),
539+
arrayOfInts: [1, 3, 5],
540+
recordOfRecords: {
541+
records: [
542+
{
543+
record: true
544+
}
545+
]
546+
}
547+
};
548+
549+
var improperData = {
550+
name: 11
551+
};
552+
553+
table.insert([data, improperData], function(err) {
554+
assert.strictEqual(err.name, 'PartialFailureError');
555+
556+
assert.deepEqual(err.errors[0], {
557+
errors: [
558+
{
559+
message: 'Conversion from int64 to string is unsupported.',
560+
reason: 'invalid'
561+
}
562+
],
563+
row: improperData
564+
});
565+
566+
assert.deepEqual(err.errors[1], {
567+
errors: [
568+
{
569+
message: undefined,
570+
reason: 'stopped'
571+
}
572+
],
573+
row: data
574+
});
575+
576+
done();
577+
});
578+
});
579+
536580
it('should export data to a file in your bucket', function(done) {
537581
var file = bucket.file('kitten-test-data-backup.json');
538582

packages/bigquery/test/table.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,8 @@ describe('BigQuery/Table', function() {
12211221
callback(null, apiResponse);
12221222
};
12231223

1224-
table.insert(data, function(err, insertErrors, apiResponse_) {
1224+
table.insert(data, function(err, apiResponse_) {
12251225
assert.ifError(err);
1226-
assert.deepEqual(insertErrors, []);
12271226
assert.strictEqual(apiResponse_, apiResponse);
12281227
done();
12291228
});
@@ -1237,15 +1236,14 @@ describe('BigQuery/Table', function() {
12371236
callback(error, apiResponse);
12381237
};
12391238

1240-
table.insert(data, function(err, insertErrors, apiResponse_) {
1239+
table.insert(data, function(err, apiResponse_) {
12411240
assert.strictEqual(err, error);
1242-
assert.strictEqual(insertErrors, null);
12431241
assert.strictEqual(apiResponse_, apiResponse);
12441242
done();
12451243
});
12461244
});
12471245

1248-
it('should return insert failures to the callback', function(done) {
1246+
it('should return partial failures', function(done) {
12491247
var row0Error = { message: 'Error.', reason: 'notFound' };
12501248
var row1Error = { message: 'Error.', reason: 'notFound' };
12511249

@@ -1259,11 +1257,17 @@ describe('BigQuery/Table', function() {
12591257
};
12601258

12611259
table.insert(data, function(err, insertErrors) {
1262-
assert.ifError(err);
1260+
assert.strictEqual(err.name, 'PartialFailureError');
12631261

1264-
assert.deepEqual(insertErrors, [
1265-
{ row: dataApiFormat.rows[0].json, errors: [row0Error] },
1266-
{ row: dataApiFormat.rows[1].json, errors: [row1Error] }
1262+
assert.deepEqual(err.errors, [
1263+
{
1264+
row: dataApiFormat.rows[0].json,
1265+
errors: [row0Error]
1266+
},
1267+
{
1268+
row: dataApiFormat.rows[1].json,
1269+
errors: [row1Error]
1270+
}
12671271
]);
12681272

12691273
done();

0 commit comments

Comments
 (0)