Skip to content

Commit

Permalink
Properly handle custom types from Table#insert(). (#2741)
Browse files Browse the repository at this point in the history
* Properly handle custom types from Table#insert().
  • Loading branch information
stephenplusplus authored Nov 10, 2017
1 parent f10f61f commit cb85d5f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/bigquery/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ BigQuery.prototype.mergeSchemaWithRows_ = function(schema, rows) {
* });
*/
BigQuery.date =
BigQuery.prototype.date = function(value) {
BigQuery.prototype.date = function BigQueryDate(value) {
if (!(this instanceof BigQuery.date)) {
return new BigQuery.date(value);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ BigQuery.prototype.date = function(value) {
* });
*/
BigQuery.datetime =
BigQuery.prototype.datetime = function(value) {
BigQuery.prototype.datetime = function BigQueryDatetime(value) {
if (!(this instanceof BigQuery.datetime)) {
return new BigQuery.datetime(value);
}
Expand Down Expand Up @@ -291,7 +291,7 @@ BigQuery.prototype.datetime = function(value) {
* });
*/
BigQuery.time =
BigQuery.prototype.time = function(value) {
BigQuery.prototype.time = function BigQueryTime(value) {
if (!(this instanceof BigQuery.time)) {
return new BigQuery.time(value);
}
Expand All @@ -318,7 +318,7 @@ BigQuery.prototype.time = function(value) {
* var timestamp = bigquery.timestamp(new Date());
*/
BigQuery.timestamp =
BigQuery.prototype.timestamp = function(value) {
BigQuery.prototype.timestamp = function BigQueryTimestamp(value) {
if (!(this instanceof BigQuery.timestamp)) {
return new BigQuery.timestamp(value);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/bigquery/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ Table.encodeValue_ = function(value) {
return value.toString('base64');
}

var customTypeConstructorNames = [
'BigQueryDate',
'BigQueryDatetime',
'BigQueryTime',
'BigQueryTimestamp',
];
var constructorName = value.constructor.name;
var isCustomType = customTypeConstructorNames.indexOf(constructorName) > -1;

if (isCustomType) {
return value.value;
}

if (is.date(value)) {
return value.toJSON();
}
Expand Down
25 changes: 25 additions & 0 deletions packages/bigquery/system-test/bigquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,31 @@ describe('BigQuery', function() {
});
});

describe('Custom Types', function() {
var table;

var DATE = bigquery.date('2017-01-01');
var DATETIME = bigquery.datetime('2017-01-01 13:00:00');
var TIME = bigquery.time('14:00:00');
var TIMESTAMP = bigquery.timestamp(new Date());

before(function() {
table = dataset.table(generateName('table'));
return table.create({
schema: 'date:DATE, datetime:DATETIME, time:TIME, timestamp:TIMESTAMP'
});
});

it('inserts with custom types', function() {
return table.insert({
date: DATE,
datetime: DATETIME,
time: TIME,
timestamp: TIMESTAMP
});
});
});

describe('Provided Tests', function() {
var table = dataset.table(generateName('table'));
var schema = require('./data/schema.json');
Expand Down
20 changes: 20 additions & 0 deletions packages/bigquery/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ describe('BigQuery', function() {
assert(instanceD instanceof bq.date);
});

it('should have the correct constructor name', function() {
var date = bq.date(INPUT_STRING);
assert.strictEqual(date.constructor.name, 'BigQueryDate');
});

it('should accept a string', function() {
var date = bq.date(INPUT_STRING);
assert.strictEqual(date.value, INPUT_STRING);
Expand Down Expand Up @@ -415,6 +420,11 @@ describe('BigQuery', function() {
assert(instanceDt instanceof bq.datetime);
});

it('should have the correct constructor name', function() {
var datetime = bq.datetime(INPUT_STRING);
assert.strictEqual(datetime.constructor.name, 'BigQueryDatetime');
});

it('should accept an object', function() {
var datetime = bq.datetime(INPUT_OBJ);
assert.strictEqual(datetime.value, EXPECTED_VALUE);
Expand Down Expand Up @@ -455,6 +465,11 @@ describe('BigQuery', function() {
assert(instanceT instanceof bq.time);
});

it('should have the correct constructor name', function() {
var time = bq.time(INPUT_STRING);
assert.strictEqual(time.constructor.name, 'BigQueryTime');
});

it('should accept a string', function() {
var time = bq.time(INPUT_STRING);
assert.strictEqual(time.value, INPUT_STRING);
Expand Down Expand Up @@ -496,6 +511,11 @@ describe('BigQuery', function() {
assert(instanceT instanceof bq.timestamp);
});

it('should have the correct constructor name', function() {
var timestamp = bq.timestamp(INPUT_STRING);
assert.strictEqual(timestamp.constructor.name, 'BigQueryTimestamp');
});

it('should accept a string', function() {
var timestamp = bq.timestamp(INPUT_STRING);
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
Expand Down
17 changes: 17 additions & 0 deletions packages/bigquery/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ describe('BigQuery/Table', function() {
assert.strictEqual(Table.encodeValue_(date), date.toJSON());
});

it('should properly encode custom types', function() {
function BigQueryDate(value) { this.value = value; }
function BigQueryDatetime(value) { this.value = value; }
function BigQueryTime(value) { this.value = value; }
function BigQueryTimestamp(value) { this.value = value; }

var date = new BigQueryDate('date');
var datetime = new BigQueryDatetime('datetime');
var time = new BigQueryTime('time');
var timestamp = new BigQueryTimestamp('timestamp');

assert.strictEqual(Table.encodeValue_(date), 'date');
assert.strictEqual(Table.encodeValue_(datetime), 'datetime');
assert.strictEqual(Table.encodeValue_(time), 'time');
assert.strictEqual(Table.encodeValue_(timestamp), 'timestamp');
});

it('should properly encode arrays', function() {
var buffer = new Buffer('test');
var date = new Date();
Expand Down

0 comments on commit cb85d5f

Please sign in to comment.