Skip to content

Commit

Permalink
bigquery: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 3, 2016
1 parent 6e93f3a commit dc9c83b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
18 changes: 9 additions & 9 deletions packages/bigquery/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ Table.prototype.import = function(source, metadata, callback) {
* // Some rows failed to insert, while others may have succeeded.
*
* // err.errors (object[]):
* // err.errors[].row (original individual row object passed to `insert`)
* // err.errors[].row (original row object passed to `insert`)
* // err.errors[].errors[].reason
* // err.errors[].errors[].message
*
Expand Down Expand Up @@ -1063,6 +1063,11 @@ Table.prototype.insert = function(rows, options, callback) {
uri: '/insertAll',
json: json
}, function(err, resp) {
if (err) {
callback(err, resp);
return;
}

var partialFailures = (resp.insertErrors || []).map(function(insertError) {
return {
errors: insertError.errors.map(function(error) {
Expand All @@ -1071,23 +1076,18 @@ Table.prototype.insert = function(rows, options, callback) {
reason: error.reason
};
}),
row: json.rows[insertError.index].json
row: rows[insertError.index]
};
});

if (failedToInsert.length > 0) {
if (partialFailures.length > 0) {
err = new common.util.PartialFailureError({
errors: partialFailures,
response: resp
});
}

if (err) {
callback(err, null, resp);
return;
}

callback(null, failedToInsert, resp);
callback(err, resp);
});
};

Expand Down
56 changes: 50 additions & 6 deletions packages/bigquery/system-test/bigquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,9 @@ describe('BigQuery', function() {
}
};

table.insert(data, function(err, insertErrors) {
table.insert(data, function(err) {
assert.ifError(err);

if (insertErrors.length > 0) {
done(insertErrors[0].errors[0]);
return;
}

function query(callback) {
var query = {
query: 'SELECT * FROM ' + table.id + ' WHERE id = ' + data.id,
Expand All @@ -533,6 +528,55 @@ describe('BigQuery', function() {
});
});

it('should return partial errors', function(done) {
var data = {
name: 'dave',
breed: 'british shorthair',
id: 99,
dob: new Date(),
around: true,
buffer: new Buffer('test'),
arrayOfInts: [1, 3, 5],
recordOfRecords: {
records: [
{
record: true
}
]
}
};

var improperData = {
name: 11
};

table.insert([data, improperData], function(err) {
assert.strictEqual(err.name, 'PartialFailureError');

assert.deepEqual(err.errors[0], {
errors: [
{
message: 'Conversion from int64 to string is unsupported.',
reason: 'invalid'
}
],
row: improperData
});

assert.deepEqual(err.errors[1], {
errors: [
{
message: undefined,
reason: 'stopped'
}
],
row: data
});

done();
});
});

it('should export data to a file in your bucket', function(done) {
var file = bucket.file('kitten-test-data-backup.json');

Expand Down
22 changes: 13 additions & 9 deletions packages/bigquery/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,9 +1221,8 @@ describe('BigQuery/Table', function() {
callback(null, apiResponse);
};

table.insert(data, function(err, insertErrors, apiResponse_) {
table.insert(data, function(err, apiResponse_) {
assert.ifError(err);
assert.deepEqual(insertErrors, []);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
Expand All @@ -1237,15 +1236,14 @@ describe('BigQuery/Table', function() {
callback(error, apiResponse);
};

table.insert(data, function(err, insertErrors, apiResponse_) {
table.insert(data, function(err, apiResponse_) {
assert.strictEqual(err, error);
assert.strictEqual(insertErrors, null);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});

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

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

table.insert(data, function(err, insertErrors) {
assert.ifError(err);
assert.strictEqual(err.name, 'PartialFailureError');

assert.deepEqual(insertErrors, [
{ row: dataApiFormat.rows[0].json, errors: [row0Error] },
{ row: dataApiFormat.rows[1].json, errors: [row1Error] }
assert.deepEqual(err.errors, [
{
row: dataApiFormat.rows[0].json,
errors: [row0Error]
},
{
row: dataApiFormat.rows[1].json,
errors: [row1Error]
}
]);

done();
Expand Down

0 comments on commit dc9c83b

Please sign in to comment.