|
1 |
| -require(__dirname + '/test-helper'); |
| 1 | +var helper = require(__dirname + '/test-helper'); |
2 | 2 | var utils = require(__dirname + "/../../lib/utils");
|
3 | 3 | var defaults = require(__dirname + "/../../lib").defaults;
|
4 | 4 |
|
@@ -48,3 +48,126 @@ test('normalizing query configs', function() {
|
48 | 48 | config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}, callback)
|
49 | 49 | assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback})
|
50 | 50 | })
|
| 51 | + |
| 52 | +test('prepareValues: buffer prepared properly', function() { |
| 53 | + var buf = new Buffer("quack"); |
| 54 | + var out = utils.prepareValue(buf); |
| 55 | + assert.strictEqual(buf, out); |
| 56 | +}); |
| 57 | + |
| 58 | +test('prepareValues: date prepared properly', function() { |
| 59 | + helper.setTimezoneOffset(-330); |
| 60 | + |
| 61 | + var date = new Date(2014, 1, 1, 11, 11, 1, 7); |
| 62 | + var out = utils.prepareValue(date); |
| 63 | + assert.strictEqual(out, "2014-02-01T11:11:01.007+05:30"); |
| 64 | + |
| 65 | + helper.resetTimezoneOffset(); |
| 66 | +}); |
| 67 | + |
| 68 | +test('prepareValues: undefined prepared properly', function() { |
| 69 | + var out = utils.prepareValue(void 0); |
| 70 | + assert.strictEqual(out, null); |
| 71 | +}); |
| 72 | + |
| 73 | +test('prepareValue: null prepared properly', function() { |
| 74 | + var out = utils.prepareValue(null); |
| 75 | + assert.strictEqual(out, null); |
| 76 | +}); |
| 77 | + |
| 78 | +test('prepareValue: true prepared properly', function() { |
| 79 | + var out = utils.prepareValue(true); |
| 80 | + assert.strictEqual(out, 'true'); |
| 81 | +}); |
| 82 | + |
| 83 | +test('prepareValue: false prepared properly', function() { |
| 84 | + var out = utils.prepareValue(false); |
| 85 | + assert.strictEqual(out, 'false'); |
| 86 | +}); |
| 87 | + |
| 88 | +test('prepareValue: number prepared properly', function () { |
| 89 | + var out = utils.prepareValue(3.042); |
| 90 | + assert.strictEqual(out, '3.042'); |
| 91 | +}); |
| 92 | + |
| 93 | +test('prepareValue: string prepared properly', function() { |
| 94 | + var out = utils.prepareValue('big bad wolf'); |
| 95 | + assert.strictEqual(out, 'big bad wolf'); |
| 96 | +}); |
| 97 | + |
| 98 | +test('prepareValue: simple array prepared properly', function() { |
| 99 | + var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]); |
| 100 | + assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}'); |
| 101 | +}); |
| 102 | + |
| 103 | +test('prepareValue: complex array prepared properly', function() { |
| 104 | + var out = utils.prepareValue([{ x: 42 }, { y: 84 }]); |
| 105 | + assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}'); |
| 106 | +}); |
| 107 | + |
| 108 | +test('prepareValue: date array prepared properly', function() { |
| 109 | + helper.setTimezoneOffset(-330); |
| 110 | + |
| 111 | + var date = new Date(2014, 1, 1, 11, 11, 1, 7); |
| 112 | + var out = utils.prepareValue([date]); |
| 113 | + assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}'); |
| 114 | + |
| 115 | + helper.resetTimezoneOffset(); |
| 116 | +}); |
| 117 | + |
| 118 | +test('prepareValue: arbitrary objects prepared properly', function() { |
| 119 | + var out = utils.prepareValue({ x: 42 }); |
| 120 | + assert.strictEqual(out, '{"x":42}'); |
| 121 | +}); |
| 122 | + |
| 123 | +test('prepareValue: objects with simple toPostgres prepared properly', function() { |
| 124 | + var customType = { |
| 125 | + toPostgres: function() { |
| 126 | + return "zomgcustom!"; |
| 127 | + } |
| 128 | + }; |
| 129 | + var out = utils.prepareValue(customType); |
| 130 | + assert.strictEqual(out, "zomgcustom!"); |
| 131 | +}); |
| 132 | + |
| 133 | +test('prepareValue: objects with complex toPostgres prepared properly', function() { |
| 134 | + var buf = new Buffer("zomgcustom!"); |
| 135 | + var customType = { |
| 136 | + toPostgres: function() { |
| 137 | + return [1, 2]; |
| 138 | + } |
| 139 | + }; |
| 140 | + var out = utils.prepareValue(customType); |
| 141 | + assert.strictEqual(out, '{"1","2"}'); |
| 142 | +}); |
| 143 | + |
| 144 | +test('prepareValue: objects with toPostgres receive prepareValue', function() { |
| 145 | + var customRange = { |
| 146 | + lower: { toPostgres: function() { return 5; } }, |
| 147 | + upper: { toPostgres: function() { return 10; } }, |
| 148 | + toPostgres: function(prepare) { |
| 149 | + return "[" + prepare(this.lower) + "," + prepare(this.upper) + "]"; |
| 150 | + } |
| 151 | + }; |
| 152 | + var out = utils.prepareValue(customRange); |
| 153 | + assert.strictEqual(out, "[5,10]"); |
| 154 | +}); |
| 155 | + |
| 156 | +test('prepareValue: objects with circular toPostgres rejected', function() { |
| 157 | + var buf = new Buffer("zomgcustom!"); |
| 158 | + var customType = { |
| 159 | + toPostgres: function() { |
| 160 | + return { toPostgres: function () { return customType; } }; |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + //can't use `assert.throws` since we need to distinguish circular reference |
| 165 | + //errors from call stack exceeded errors |
| 166 | + try { |
| 167 | + utils.prepareValue(customType); |
| 168 | + } catch (e) { |
| 169 | + assert.ok(e.message.match(/circular/), "Expected circular reference error but got " + e); |
| 170 | + return; |
| 171 | + } |
| 172 | + throw new Error("Expected prepareValue to throw exception"); |
| 173 | +}); |
0 commit comments