|
| 1 | +var _ = require('lodash'); |
| 2 | +var should = require('should'); |
| 3 | +var Driver = require('../../../lib/Drivers/DML/postgres').Driver; |
| 4 | +var helper = require('../../support/spec_helper'); |
| 5 | +var common = require('../../common'); |
| 6 | + |
| 7 | +if (common.protocol() == "mongodb") return; |
| 8 | + |
| 9 | +describe("Postgres driver", function() { |
| 10 | + describe("#propertyToValue", function () { |
| 11 | + describe("type object", function () { |
| 12 | + function evaluate (input) { |
| 13 | + var driver = new Driver({}, {}, {}); |
| 14 | + return driver.propertyToValue(input, { type: 'object' }); |
| 15 | + } |
| 16 | + |
| 17 | + it("should not change null", function () { |
| 18 | + should.strictEqual(evaluate(null), null); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should not change buffer", function () { |
| 22 | + var b = new Buffer('abc'); |
| 23 | + should.strictEqual(evaluate(b), b); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should encode everything else as a Buffer", function () { |
| 27 | + var input = { abc: 123 }; |
| 28 | + var out = evaluate(input); |
| 29 | + |
| 30 | + should(out instanceof Buffer); |
| 31 | + should.equal(JSON.stringify(input), out.toString()); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("date", function () { |
| 36 | + function evaluate (input, opts) { |
| 37 | + if (!opts) opts = {}; |
| 38 | + var driver = new Driver(opts.config, {}, {}); |
| 39 | + return driver.propertyToValue(input, { type: 'date' }); |
| 40 | + } |
| 41 | + |
| 42 | + it("should do nothing when timezone isn't configured", function () { |
| 43 | + var input = new Date(); |
| 44 | + var inputStr = input.toString(); |
| 45 | + var out = evaluate(input); |
| 46 | + |
| 47 | + should.strictEqual(input, out); |
| 48 | + should.equal(inputStr, out.toString()); |
| 49 | + }); |
| 50 | + |
| 51 | + // I'm not sure if the timezone tests are right |
| 52 | + it("should offset time by specified timezone amount for + timezones", function () { |
| 53 | + var input = new Date(Date.UTC(2014,2,5,13,14,0,0)); |
| 54 | + var inputStr = input.toUTCString(); |
| 55 | + var out = evaluate(input, { config: { timezone: '+06:11' } }); |
| 56 | + |
| 57 | + should.equal(out.toUTCString(), 'Wed, 05 Mar 2014 08:25:00 GMT'); |
| 58 | + }); |
| 59 | + |
| 60 | + // The HH:mm for this one seem odd |
| 61 | + it("should offset time by specified timezone amount for + timezones", function () { |
| 62 | + var input = new Date(Date.UTC(2014,2,5,13,14,0,0)); |
| 63 | + var inputStr = input.toUTCString(); |
| 64 | + var out = evaluate(input, { config: { timezone: '-06:11' } }); |
| 65 | + |
| 66 | + should.equal(out.toUTCString(), 'Tue, 04 Mar 2014 20:03:00 GMT'); |
| 67 | + }); |
| 68 | + // -------------- |
| 69 | + }); |
| 70 | + |
| 71 | + describe("type point", function () { |
| 72 | + function evaluate (input) { |
| 73 | + var driver = new Driver({}, {}, {}); |
| 74 | + return driver.propertyToValue(input, { type: 'point' }); |
| 75 | + } |
| 76 | + |
| 77 | + it("should encode correctly", function () { |
| 78 | + var out = evaluate({ x: 5, y: 7 }); |
| 79 | + |
| 80 | + should(out instanceof Function); |
| 81 | + should.equal(out(), "POINT(5, 7)"); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("custom type", function () { |
| 86 | + var customType = { |
| 87 | + propertyToValue: function (input) { |
| 88 | + return input + ' QWERTY'; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + function evaluate (input, customTypes) { |
| 93 | + var driver = new Driver({}, {}, {}); |
| 94 | + if (customType) { |
| 95 | + for (var k in customTypes) { |
| 96 | + driver.customTypes[k] = customTypes[k]; |
| 97 | + } |
| 98 | + } |
| 99 | + return driver.propertyToValue(input, { type: 'qwerty' }); |
| 100 | + } |
| 101 | + |
| 102 | + it("should do custom type conversion if provided", function () { |
| 103 | + var opts = { qwerty: customType }; |
| 104 | + var out = evaluate('f', opts); |
| 105 | + |
| 106 | + should.equal(out, 'f QWERTY'); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should not do custom type conversion if not provided", function () { |
| 110 | + var opts = { qwerty: {} }; |
| 111 | + var out = evaluate('f', opts); |
| 112 | + |
| 113 | + should.equal(out, 'f'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should do nothing for other types", function () { |
| 118 | + function evaluate (input, type) { |
| 119 | + var driver = new Driver({}, {}, {}); |
| 120 | + return driver.propertyToValue(input, { type: type }); |
| 121 | + } |
| 122 | + |
| 123 | + should.strictEqual(evaluate('abc', { type: 'string' }), 'abc'); |
| 124 | + should.strictEqual(evaluate(42, { type: 'number' }), 42); |
| 125 | + should.strictEqual(evaluate(undefined, { type: 'bleh' }), undefined); |
| 126 | + }); |
| 127 | + |
| 128 | + }); |
| 129 | + |
| 130 | +}); |
0 commit comments