Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Array parsing #3

Merged
merged 2 commits into from May 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/textParsers.js
Expand Up @@ -122,6 +122,18 @@ var parseStringArray = function(val) {
return p.parse();
};

var parseDateArray = function(val) {
if (!val) { return null; }

var p = arrayParser.create(val, function(entry) {
if (entry !== null) {
entry = parseDate(entry);
}
return entry;
});

return p.parse();
};

var NUM = '([+-]?\\d+)';
var YEAR = NUM + '\\s+years?';
Expand Down Expand Up @@ -227,6 +239,7 @@ var init = function(register) {
register(1015, parseStringArray); //varchar
register(1008, parseStringArray);
register(1009, parseStringArray);
register(1182, parseDateArray); // date array
register(1186, parseInterval);
register(17, parseByteA);
register(114, JSON.parse.bind(JSON));
Expand Down
48 changes: 31 additions & 17 deletions test/index.js
Expand Up @@ -81,7 +81,7 @@ var tests = [{
dataTypeID: 1184,
actual: '2011-01-23 22:05:00.68-06',
expected: function(val) {
assert.UTCDate(val, 2011, 0, 24, 4, 5, 00, 680);
assert.UTCDate(val, 2011, 0, 24, 4, 5, 0, 680);
}
}, {
name: 'timestampz with huge miliseconds in UTC',
Expand Down Expand Up @@ -114,33 +114,33 @@ var tests = [{
dataTypeID: 1082,
actual: '2010-10-31',
expected: function(val) {
var now = new Date(2010, 9, 31)
var now = new Date(2010, 9, 31);
assert.UTCDate(val, 2010, now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), 0, 0, 0);
assert.equal(val.getHours(), now.getHours())
assert.equal(val.getHours(), now.getHours());
}
},{
name: 'interval time',
format: 'text',
dataTypeID: 1186,
actual: '01:02:03',
expected: function(val) {
assert.deepEqual(val, {'hours':1, 'minutes':2, 'seconds':3})
assert.deepEqual(val, {'hours':1, 'minutes':2, 'seconds':3});
}
},{
name: 'interval long',
format: 'text',
dataTypeID: 1186,
actual: '1 year -32 days',
expected: function(val) {
assert.deepEqual(val, {'years':1, 'days':-32})
assert.deepEqual(val, {'years':1, 'days':-32});
}
},{
name: 'interval combined negative',
format: 'text',
dataTypeID: 1186,
actual: '1 day -00:00:03',
expected: function(val) {
assert.deepEqual(val, {'days':1, 'seconds':-3})
assert.deepEqual(val, {'days':1, 'seconds':-3});
}
},{
name: 'bytea',
Expand Down Expand Up @@ -236,6 +236,20 @@ var tests = [{
expected :function(val){
assert.deepEqual(val, [-12345678.1234567, 12345678.12345678]);
}
},{
name : 'array/date',
format : 'text',
dataTypeID: 1182,
actual: '{2014-01-01,2015-12-31}',
expected :function(val){
var now = new Date(2014, 0, 1);
var then = new Date(2015, 11, 31);
assert.equal(val.length, 2);
val.forEach(function(element, index) {
var match = index ? then : now;
assert.UTCDate(element, match.getUTCFullYear(), match.getUTCMonth(), match.getUTCDate(), match.getUTCHours(), 0, 0, 0);
});
}
},{
name: 'binary-string/varchar',
format: 'binary',
Expand Down Expand Up @@ -329,20 +343,20 @@ var assert = require('./assert');

describe('type parsing', function() {
tests.forEach(function(test) {
test.format = test.format || 'text'
test.format = test.format || 'text';
it('correctly parses ' + test.name + ' (' + test.format + ')', function() {
var parser = pgTypes.getTypeParser(test.dataTypeID, test.format)
var result = parser(test.actual)
var parser = pgTypes.getTypeParser(test.dataTypeID, test.format);
var result = parser(test.actual);
if(typeof test.expected == 'function') {
return test.expected(result)
return test.expected(result);
}
assert.strictEqual(result, test.expected)
})
})
})
assert.strictEqual(result, test.expected);
});
});
});

describe('interface', function() {
it('exports text parsers by default', function() {
assert.strictEqual(pgTypes.getTypeParser(23), pgTypes.getTypeParser(23, 'text'))
})
})
assert.strictEqual(pgTypes.getTypeParser(23), pgTypes.getTypeParser(23, 'text'));
});
});