diff --git a/lib/Drivers/DML/mysql.js b/lib/Drivers/DML/mysql.js index 467ecdfe..fde22913 100644 --- a/lib/Drivers/DML/mysql.js +++ b/lib/Drivers/DML/mysql.js @@ -289,7 +289,9 @@ Driver.prototype.propertyToValue = function (value, property) { value = (value) ? 1 : 0; break; case "object": - value = JSON.stringify(value); + if (value !== null) { + value = JSON.stringify(value); + } break; case "point": return function() { return 'POINT(' + value.x + ', ' + value.y + ')'; }; diff --git a/lib/Drivers/DML/postgres.js b/lib/Drivers/DML/postgres.js index 12807a11..4d1aa343 100644 --- a/lib/Drivers/DML/postgres.js +++ b/lib/Drivers/DML/postgres.js @@ -324,7 +324,7 @@ Driver.prototype.propertyToValue = function (value, property) { switch (property.type) { case "object": - if (!Buffer.isBuffer(value)) { + if (value !== null && !Buffer.isBuffer(value)) { value = new Buffer(JSON.stringify(value)); } break; diff --git a/lib/Drivers/DML/sqlite.js b/lib/Drivers/DML/sqlite.js index a5c5bc34..fb2017e0 100644 --- a/lib/Drivers/DML/sqlite.js +++ b/lib/Drivers/DML/sqlite.js @@ -295,7 +295,9 @@ Driver.prototype.propertyToValue = function (value, property) { value = (value) ? 1 : 0; break; case "object": - value = JSON.stringify(value); + if (value !== null) { + value = JSON.stringify(value); + } break; case "date": if (this.config.query && this.config.query.strdates) { diff --git a/package.json b/package.json index fd26cda3..c31b167f 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "async" : "*", "mocha" : "1.13.0", "should" : "1.2.2", - "mongodb" : "1.3.19" + "mongodb" : "1.3.19", + "glob" : "3.2.8" }, "optionalDependencies": {} } diff --git a/test/integration/drivers/postgres_spec.js b/test/integration/drivers/postgres_spec.js new file mode 100644 index 00000000..c620e778 --- /dev/null +++ b/test/integration/drivers/postgres_spec.js @@ -0,0 +1,130 @@ +var _ = require('lodash'); +var should = require('should'); +var Driver = require('../../../lib/Drivers/DML/postgres').Driver; +var helper = require('../../support/spec_helper'); +var common = require('../../common'); + +if (common.protocol() == "mongodb") return; + +describe("Postgres driver", function() { + describe("#propertyToValue", function () { + describe("type object", function () { + function evaluate (input) { + var driver = new Driver({}, {}, {}); + return driver.propertyToValue(input, { type: 'object' }); + } + + it("should not change null", function () { + should.strictEqual(evaluate(null), null); + }); + + it("should not change buffer", function () { + var b = new Buffer('abc'); + should.strictEqual(evaluate(b), b); + }); + + it("should encode everything else as a Buffer", function () { + var input = { abc: 123 }; + var out = evaluate(input); + + should(out instanceof Buffer); + should.equal(JSON.stringify(input), out.toString()); + }); + }); + + describe("date", function () { + function evaluate (input, opts) { + if (!opts) opts = {}; + var driver = new Driver(opts.config, {}, {}); + return driver.propertyToValue(input, { type: 'date' }); + } + + it("should do nothing when timezone isn't configured", function () { + var input = new Date(); + var inputStr = input.toString(); + var out = evaluate(input); + + should.strictEqual(input, out); + should.equal(inputStr, out.toString()); + }); + + // I'm not sure if the timezone tests are right + it("should offset time by specified timezone amount for + timezones", function () { + var input = new Date(Date.UTC(2014,2,5,13,14,0,0)); + var inputStr = input.toUTCString(); + var out = evaluate(input, { config: { timezone: '+06:11' } }); + + should.equal(out.toUTCString(), 'Wed, 05 Mar 2014 08:25:00 GMT'); + }); + + // The HH:mm for this one seem odd + it("should offset time by specified timezone amount for + timezones", function () { + var input = new Date(Date.UTC(2014,2,5,13,14,0,0)); + var inputStr = input.toUTCString(); + var out = evaluate(input, { config: { timezone: '-06:11' } }); + + should.equal(out.toUTCString(), 'Tue, 04 Mar 2014 20:03:00 GMT'); + }); + // -------------- + }); + + describe("type point", function () { + function evaluate (input) { + var driver = new Driver({}, {}, {}); + return driver.propertyToValue(input, { type: 'point' }); + } + + it("should encode correctly", function () { + var out = evaluate({ x: 5, y: 7 }); + + should(out instanceof Function); + should.equal(out(), "POINT(5, 7)"); + }); + }); + + describe("custom type", function () { + var customType = { + propertyToValue: function (input) { + return input + ' QWERTY'; + } + }; + + function evaluate (input, customTypes) { + var driver = new Driver({}, {}, {}); + if (customType) { + for (var k in customTypes) { + driver.customTypes[k] = customTypes[k]; + } + } + return driver.propertyToValue(input, { type: 'qwerty' }); + } + + it("should do custom type conversion if provided", function () { + var opts = { qwerty: customType }; + var out = evaluate('f', opts); + + should.equal(out, 'f QWERTY'); + }); + + it("should not do custom type conversion if not provided", function () { + var opts = { qwerty: {} }; + var out = evaluate('f', opts); + + should.equal(out, 'f'); + }); + }); + + it("should do nothing for other types", function () { + function evaluate (input, type) { + var driver = new Driver({}, {}, {}); + return driver.propertyToValue(input, { type: type }); + } + + should.strictEqual(evaluate('abc', { type: 'string' }), 'abc'); + should.strictEqual(evaluate(42, { type: 'number' }), 42); + should.strictEqual(evaluate(undefined, { type: 'bleh' }), undefined); + }); + + }); + +}); diff --git a/test/run.js b/test/run.js index da307f0d..057880ae 100644 --- a/test/run.js +++ b/test/run.js @@ -1,9 +1,9 @@ var Mocha = require("mocha"); -var fs = require("fs"); +var glob = require("glob"); var path = require("path"); var common = require("./common"); var logging = require("./logging"); -var location = path.normalize(path.join(__dirname, "integration")); +var location = path.normalize(path.join(__dirname, "integration", "**", "*.js")); var mocha = new Mocha({ reporter: "progress" }); @@ -20,14 +20,9 @@ switch (common.hasConfig(common.protocol())) { runTests(); function runTests() { - fs.readdirSync(location).filter(function (file) { - return file.substr(-3) === '.js'; - }).forEach(function (file) { + glob.sync(location).forEach(function (file) { if (!shouldRunTest(file)) return; - - mocha.addFile( - path.join(location, file) - ); + mocha.addFile(file); }); logging.info("Testing **%s**", common.getConnectionString()); @@ -38,11 +33,11 @@ function runTests() { } function shouldRunTest(file) { - var name = file.substr(0, file.length - 3); + var name = path.basename(file).slice(0, -3) var proto = common.protocol(); + var exclude = ['model-aggregate','property-number-size','smart-types']; - if (proto == "mongodb" && [ "model-aggregate", - "property-number-size", "smart-types" ].indexOf(name) >= 0) return false; + if (proto == "mongodb" && exclude.indexOf(name) >= 0) return false; return true; }