Skip to content

Commit

Permalink
bigquery: complete system tests (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored Mar 3, 2017
1 parent 1edb800 commit d3b84f5
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 104 deletions.
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ BigQuery.prototype.datetime = function(value) {
d: value.day,
time: time ? ' ' + time : ''
});
} else {
value = value.replace(/^(.*)T(.*)Z$/, '$1 $2');
}

this.value = value;
Expand Down Expand Up @@ -228,11 +230,8 @@ BigQuery.prototype.timestamp = function(value) {
return new BigQuery.timestamp(value);
}

value = value || new Date();

if (is.date(value)) {
value = value.toJSON().replace(/^(.*)T(.*)Z$/, '$1 $2');
}
value = new Date(value);
value = value.toJSON().replace(/^(.*)T(.*)Z$/, '$1 $2');

this.value = value;
};
Expand Down Expand Up @@ -841,7 +840,7 @@ BigQuery.prototype.query = function(options, callback) {

var rows = [];
if (resp.schema && resp.rows) {
rows = Table.mergeSchemaWithRows_(resp.schema, resp.rows);
rows = Table.mergeSchemaWithRows_(BigQuery, resp.schema, resp.rows);
}

var nextQuery = null;
Expand Down
24 changes: 20 additions & 4 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Table.encodeValue_ = function(value) {
* @param {array} rows
* @return {array} Fields using their matching names from the table's schema.
*/
Table.mergeSchemaWithRows_ = function(schema, rows) {
Table.mergeSchemaWithRows_ = function(BigQuery, schema, rows) {
return arrify(rows).map(mergeSchema).map(flattenRows);

function mergeSchema(row) {
Expand Down Expand Up @@ -303,11 +303,23 @@ Table.mergeSchemaWithRows_ = function(schema, rows) {
break;
}
case 'RECORD': {
value = Table.mergeSchemaWithRows_(schemaField, value).pop();
value = Table.mergeSchemaWithRows_(BigQuery, schemaField, value).pop();
break;
}
case 'DATE': {
value = BigQuery.date(value);
break;
}
case 'DATETIME': {
value = BigQuery.datetime(value);
break;
}
case 'TIME': {
value = BigQuery.time(value);
break;
}
case 'TIMESTAMP': {
value = new Date(value * 1000);
value = BigQuery.timestamp(new Date(value * 1000));
break;
}
}
Expand Down Expand Up @@ -780,7 +792,11 @@ Table.prototype.getRows = function(options, callback) {
return;
}

rows = Table.mergeSchemaWithRows_(self.metadata.schema, rows || []);
rows = Table.mergeSchemaWithRows_(
self.bigQuery,
self.metadata.schema,
rows || []
);

callback(null, rows, nextQuery, resp);
}
Expand Down
176 changes: 116 additions & 60 deletions system-test/bigquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,68 +501,9 @@ describe('BigQuery', function() {
});
});

it('should convert values to their schema types', function(done) {
var data = {
name: 'dave',
breed: 'husky',
id: 99,
dob: new Date(),
around: true,
buffer: new Buffer('test'),
arrayOfInts: [1, 3, 5],
recordOfRecords: {
records: [
{
record: true
}
]
}
};

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

function query(callback) {
var query = {
query: 'SELECT * FROM ' + table.id + ' WHERE id = ' + data.id,
useLegacySql: false
};
var row;

table.createQueryStream(query)
.on('error', callback)
.once('data', function(row_) { row = row_; })
.on('end', function() {
if (!row) {
callback(new Error('Query returned 0 results.'));
return;
}

assert.deepEqual(row, data);
callback();
});
}

async.retry({ times: 3, interval: 2000 }, query, done);
});
});

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

var improperData = {
Expand Down Expand Up @@ -988,6 +929,121 @@ describe('BigQuery', function() {
});
});

describe('Provided Tests', function() {
var table = dataset.table(generateName('table'));
var schema = require('./data/schema.json');
var testData = require('./data/schema-test-data.json');

var EXPECTED_ROWS = {
Bilbo: {
Name: 'Bilbo',
Age: 111,
Weight: 67.2,
IsMagic: false,
Spells: [],
TeaTime: bigquery.time('10:00:00'),
NextVacation: bigquery.date('2017-09-22'),
FavoriteTime: bigquery.datetime('2031-04-01T05:09:27')
},

Gandalf: {
Name: 'Gandalf',
Age: 1000,
Weight: 198.6,
IsMagic: true,
Spells: [
{
Name: 'Skydragon',
LastUsed: bigquery.timestamp('2015-10-31T23:59:56.000Z'),
DiscoveredBy: 'Firebreather',
Properties: [
{
Name: 'Flying',
Power: 1
},
{
Name: 'Creature',
Power: 1
},
{
Name: 'Explodey',
Power: 11
}
],
Icon: new Buffer(testData[1].Spells[0].Icon, 'base64')
}
],
TeaTime: bigquery.time('15:00:00'),
NextVacation: bigquery.date('2666-06-06'),
FavoriteTime: bigquery.datetime('2001-12-19T23:59:59')
},

Sabrina: {
Name: 'Sabrina',
Age: 17,
Weight: 128.3,
IsMagic: true,
Spells: [
{
Name: 'Talking cats',
LastUsed: bigquery.timestamp('2017-02-14T12:07:23.000Z'),
DiscoveredBy: 'Salem',
Properties: [
{
Name: 'Makes you look crazy',
Power: 1
}
],
Icon: new Buffer(testData[2].Spells[0].Icon, 'base64')
}
],
TeaTime: bigquery.time('12:00:00'),
NextVacation: bigquery.date('2017-03-14'),
FavoriteTime: bigquery.datetime('2000-10-31T23:27:46')
}
};

before(function(done) {
async.series([
function(next) {
table.create({
schema: schema
}, next);
},

function(next) {
table.insert(testData, next);
},

function(next) {
setTimeout(next, 15000);
}
], done);
});

after(function(done) {
table.delete(done);
});

it('should convert rows back correctly', function(done) {
table.getRows(function(err, rows) {
assert.ifError(err);

if (rows.length === 0) {
done(new Error('Rows not returned from the API.'));
return;
}

rows.forEach(function(row) {
var expectedRow = EXPECTED_ROWS[row.Name];
assert.deepEqual(row, expectedRow);
});

done();
});
});
});

function generateName(resourceType) {
return (GCLOUD_TESTS_PREFIX + resourceType + '_' + uuid.v1())
.replace(/-/g, '_');
Expand Down
66 changes: 66 additions & 0 deletions system-test/data/schema-test-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
{
"Age": "111",
"FavoriteTime": "2031-04-01T05:09:27",
"IsMagic": false,
"Name": "Bilbo",
"NextVacation": "2017-09-22",
"Spells": [],
"TeaTime": "10:00:00",
"Weight": 67.2
},
{
"Age": "1000",
"FavoriteTime": "2001-12-19T23:59:59",
"IsMagic": true,
"Name": "Gandalf",
"NextVacation": "2666-06-06",
"Spells": [
{
"DiscoveredBy": "Firebreather",
"Icon": "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAgCAYAAAAFQMh/AAAAAXNSR0IArs4c6QAAA9lJREFUSA21lk9OVEEQxvsRDImoiMG9mLjjCG5mEg7gEfQGsIcF7p0EDsBBSJiNO7ZsFRZqosb/QkSj7fer7ur33sw8GDFUUq+7q6vqq6qu7pkQzqG4EeI521e7FePVgM9cGPYwhCi6UO8qFOK+YY+Br66ujsmmxb84Yzwp6zCsxjJfWVkxnMsEMGuWHZ9Wcz11cM48hkq0vLwc1tbW4mAwqDpcdIqnMmgF0JMv2CiGnZ2dcHR0FA4PD8Pe3t5U/tx6bCSlb+JT8XfxT3HsUek0Li0tRdjWl+z6iRF+FNA1hXPDQ/IMNyRg3s8bD/OaZS+VP+9cOLSa64cA34oXZWagDkRzAaJxXaE+ufc4rCN7LrazZ2+8+STtpAL8WYDvpTaHKlkB2iQARMvb2+H27m4YaL7zaDtUw1BZAASi6T8T2UZnPZV2pvnJfCH5p8bewcGB6TrIfz8wBZgHQ83kjpuj6RBYQpuo09Tvmpd7TPe+ktZN8cKwS92KWXGuaqWowlYEwthtMcWOZUNJc8at+zuF/Xkqo69baS7P+AvWjYwJ4jyHXXsEnd74ZO/Pq+uXUuv6WNlso6cvnDsZB1V/unJab3D1/KrJDw9NCM9wHf2FK2ejTKMejnBHfGtfH7LGGCdQDqaqJgfgzWjXK1nYV4jRbPGnxUT7cqUaZfJrVZeOm9QmB21L6xXgbu/ScsYusJFMoU0x2fsamRJOd6kOYDRLUxv94ENZe8+0gM+0dyz+KgU7X8rLHHCIOZyrna4y6ykIu0YCs02TBXmk3PZssmEgaTxTo83xjCIjoE21h0Yah3MrV4+9kR8MaabGze+9NEILGAFE5nMOiiA32KnAr/sb7tED3nzlzC4dB38WMC+EjaqHfqvUKHi2gJPdWQ6AbH8hgyQ7QY6jvjj3QZWvX6pUAtduTX5Dss96Q7NI9RQRJeeKvRFbt0v2gb1Gx/PooJsztn1c1DqpAU3Hde2dB2aEHBhjgOFjMeDvxLafjQ3YZQSgOcHJZX611H45sGLHWvYTz9hiURlpNoBZvxb/Ft9lAQ1DmBfUiR+j1hAPkMBTE9L9+zLva1QvGFHurRBaZ5xLVitoBviiRkD/sIMDztKA5FA0b9/0OclzO2/XAQymJ0TcghZwEo9/AX8gMeAJMOvIsWWt5bwCoiFhVSllrdH0t5Q1JHAFlKJNkvTVdn2GHb9KdmacMT+d/Os05imJUccRX2YuZ93Sxf0Ilc4DPDeAq5SAvFEAY94cQc6BA26dzb4HWAJI4DPmQE5KCVUyvb2FcDZem7JdT2ggKUP3xX6n9XNq1DpzSf4Cy4ZqSlmM8d8AAAAASUVORK5CYII=",
"LastUsed": "2015-10-31 23:59:56 UTC",
"Name": "Skydragon",
"Properties": [
{
"Name": "Flying",
"Power": 1
},
{
"Name": "Creature",
"Power": 1
},
{
"Name": "Explodey",
"Power": 11
}
]
}
],
"TeaTime": "15:00:00",
"Weight": 198.6
},
{
"Age": "17",
"FavoriteTime": "2000-10-31T23:27:46",
"IsMagic": true,
"Name": "Sabrina",
"NextVacation": "2017-03-14",
"Spells": [
{
"DiscoveredBy": "Salem",
"Icon": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAgAAAAKAAAACAAAAAgAAABxj2CfowAAAGSSURBVHgB7Jc9TsNAEIX3JDkCPUV6KlpKFHEGlD4nyA04ACUXQKTgCEipUnKGNEbP0otentayicZ24SlWs7tjO/N9u/5J2b2+NUtuZcnwYE8BuQPyGZAPwXwLLPk5kG+BJa9+fgfkh1B+CeancL4F8i2Q/wWm/S/w+XFoTseftn0dvhu0OXfhpM+AGvzcEiYVAFisPqE9zrETJhHAlXfg2lglMK9z0f3RBfB+ZyRUV3x+erzsEIjjOBqc1xtNAIrvguybV3A9lkVHxlEE6GrrPb/ZvAySwlUnfCmlPQ+R8JCExvGtcRQBLFwj4FGkznX1VYDKPG/f2/MjwCksXACgdNUxJjwK9xwl4JihOwTFR0kIF+CABEPRnvsvPFctMoYKqAFSAFaMwB4pp3Y+bodIYL9WmIAaIOHxo7W8wiHvAjTvhUeNwwSgeAeAABbqOewC5hBdwFD4+9+7puzXV9fS6/b1wwT4tsaYAhwOOQdUQch5vgZCeAhAv3ZM31yYAAUgvApQQQ6n5w6FB/RVe1jdJOAPAAD//1eMQwoAAAGQSURBVO1UMU4DQQy8X9AgWopIUINEkS4VlJQo4gvwAV7AD3gEH4iSgidESpWSXyyZExP5lr0c7K5PsXBhec/2+jzjuWtent9CLdtu1mG5+gjz+WNr7IsY7eH+tvO+xfuqk4vz7CH91edFaF5v9nb6dBKm13edvrL+0Lk5lMzJkQDeJSkkgHF6mR8CHwMHCQR/NAQQGD0BAlwK4FCefQiefq+A2Vn29tG7igLAfmwcnJu/nJy3BMQkMN9HEPr8AL3bfBv7Bp+7/SoExMDjZwKEJwmyhnnmQIQEBIlz2x0iKoAvJkAC6TsTIH6MqRrEWUMSZF2zAwqT4Eu/e6pzFAIkmNSZ4OFT+VYBIIF//UqbJwnF/4DU0GwOn8r/JQYCpPGufEfJuZiA37ycQw/5uFeqPq4pfR6FADmkBCXjfWdZj3NfXW58dAJyB9W65wRoMWulryvAyqa05nQFaDFrpa8rwMqmtOZ0BWgxa6WvK8DKprTmdAVoMWulryvAyqa05nQFaDFrpa8rwMqmtOb89wr4AtQ4aPoL6yVpAAAAAElFTkSuQmCC",
"LastUsed": "2017-02-14 12:07:23 UTC",
"Name": "Talking cats",
"Properties": [
{
"Name": "Makes you look crazy",
"Power": 1
}
]
}
],
"TeaTime": "12:00:00",
"Weight": 128.3
}
]
Loading

0 comments on commit d3b84f5

Please sign in to comment.