types.js
1 'use strict' 2 3 exports['string/varchar'] = { 4 format: 'text', 5 id: 1043, 6 tests: [ 7 ['bang', 'bang'] 8 ] 9 } 10 11 exports['integer/int4'] = { 12 format: 'text', 13 id: 23, 14 tests: [ 15 ['2147483647', 2147483647] 16 ] 17 } 18 19 exports['smallint/int2'] = { 20 format: 'text', 21 id: 21, 22 tests: [ 23 ['32767', 32767] 24 ] 25 } 26 27 exports['bigint/int8'] = { 28 format: 'text', 29 id: 20, 30 tests: [ 31 ['9223372036854775807', '9223372036854775807'] 32 ] 33 } 34 35 exports.oid = { 36 format: 'text', 37 id: 26, 38 tests: [ 39 ['103', 103] 40 ] 41 } 42 43 var bignum = '31415926535897932384626433832795028841971693993751058.16180339887498948482045868343656381177203091798057628' 44 exports.numeric = { 45 format: 'text', 46 id: 1700, 47 tests: [ 48 [bignum, bignum] 49 ] 50 } 51 52 exports['real/float4'] = { 53 format: 'text', 54 id: 700, 55 tests: [ 56 ['123.456', 123.456] 57 ] 58 } 59 60 exports['double precision / float 8'] = { 61 format: 'text', 62 id: 701, 63 tests: [ 64 ['12345678.12345678', 12345678.12345678] 65 ] 66 } 67 68 exports.boolean = { 69 format: 'text', 70 id: 16, 71 tests: [ 72 ['TRUE', true], 73 ['t', true], 74 ['true', true], 75 ['y', true], 76 ['yes', true], 77 ['on', true], 78 ['1', true], 79 ['f', false], 80 [null, null] 81 ] 82 } 83 84 exports.timestamptz = { 85 format: 'text', 86 id: 1184, 87 tests: [ 88 [ 89 '2010-10-31 14:54:13.74-05:30', 90 dateEquals(2010, 9, 31, 20, 24, 13, 740) 91 ], 92 [ 93 '2011-01-23 22:05:00.68-06', 94 dateEquals(2011, 0, 24, 4, 5, 0, 680) 95 ], 96 [ 97 '2010-10-30 14:11:12.730838Z', 98 dateEquals(2010, 9, 30, 14, 11, 12, 730) 99 ], 100 [ 101 '2010-10-30 13:10:01+05', 102 dateEquals(2010, 9, 30, 8, 10, 1, 0) 103 ] 104 ] 105 } 106 107 exports.timestamp = { 108 format: 'text', 109 id: 1114, 110 tests: [ 111 [ 112 '2010-10-31 00:00:00', 113 function (t, value) { 114 t.equal( 115 value.toUTCString(), 116 new Date(2010, 9, 31, 0, 0, 0, 0, 0).toUTCString() 117 ) 118 t.equal( 119 value.toString(), 120 new Date(2010, 9, 31, 0, 0, 0, 0, 0, 0).toString() 121 ) 122 } 123 ] 124 ] 125 } 126 127 exports.date = { 128 format: 'text', 129 id: 1082, 130 tests: [ 131 ['2010-10-31', function (t, value) { 132 var now = new Date(2010, 9, 31) 133 dateEquals( 134 2010, 135 now.getUTCMonth(), 136 now.getUTCDate(), 137 now.getUTCHours(), 0, 0, 0)(t, value) 138 t.equal(value.getHours(), now.getHours()) 139 }] 140 ] 141 } 142 143 exports.inet = { 144 format: 'text', 145 id: 869, 146 tests: [ 147 ['8.8.8.8', '8.8.8.8'], 148 ['2001:4860:4860::8888', '2001:4860:4860::8888'], 149 ['127.0.0.1', '127.0.0.1'], 150 ['fd00:1::40e', 'fd00:1::40e'], 151 ['1.2.3.4', '1.2.3.4'] 152 ] 153 } 154 155 exports.cidr = { 156 format: 'text', 157 id: 650, 158 tests: [ 159 ['172.16.0.0/12', '172.16.0.0/12'], 160 ['fe80::/10', 'fe80::/10'], 161 ['fc00::/7', 'fc00::/7'], 162 ['192.168.0.0/24', '192.168.0.0/24'], 163 ['10.0.0.0/8', '10.0.0.0/8'] 164 ] 165 } 166 167 exports.macaddr = { 168 format: 'text', 169 id: 829, 170 tests: [ 171 ['08:00:2b:01:02:03', '08:00:2b:01:02:03'], 172 ['16:10:9f:0d:66:00', '16:10:9f:0d:66:00'] 173 ] 174 } 175 176 exports.numrange = { 177 format: 'text', 178 id: 3906, 179 tests: [ 180 ['[,]', '[,]'], 181 ['(,)', '(,)'], 182 ['(,]', '(,]'], 183 ['[1,)', '[1,)'], 184 ['[,1]', '[,1]'], 185 ['(1,2)', '(1,2)'], 186 ['(1,20.5]', '(1,20.5]'] 187 ] 188 } 189 190 exports.interval = { 191 format: 'text', 192 id: 1186, 193 tests: [ 194 ['01:02:03', function (t, value) { 195 t.equal(value.toPostgres(), '3 seconds 2 minutes 1 hours') 196 t.deepEqual(value, {hours: 1, minutes: 2, seconds: 3}) 197 }], 198 ['01:02:03.456', function (t, value) { 199 t.deepEqual(value, {hours: 1, minutes:2, seconds: 3, milliseconds: 456}) 200 }], 201 ['1 year -32 days', function (t, value) { 202 t.equal(value.toPostgres(), '-32 days 1 years') 203 t.deepEqual(value, {years: 1, days: -32}) 204 }], 205 ['1 day -00:00:03', function (t, value) { 206 t.equal(value.toPostgres(), '-3 seconds 1 days') 207 t.deepEqual(value, {days: 1, seconds: -3}) 208 }] 209 ] 210 } 211 212 exports.bytea = { 213 format: 'text', 214 id: 17, 215 tests: [ 216 ['foo\\000\\200\\\\\\377', function (t, value) { 217 var buffer = new Buffer([102, 111, 111, 0, 128, 92, 255]) 218 t.ok(buffer.equals(value)) 219 }], 220 ['', function (t, value) { 221 var buffer = new Buffer(0) 222 t.ok(buffer.equals(value)) 223 }] 224 ] 225 } 226 227 exports['array/boolean'] = { 228 format: 'text', 229 id: 1000, 230 tests: [ 231 ['{true,false}', function (t, value) { 232 t.deepEqual(value, [true, false]) 233 }] 234 ] 235 } 236 237 exports['array/char'] = { 238 format: 'text', 239 id: 1014, 240 tests: [ 241 ['{foo,bar}', function (t, value) { 242 t.deepEqual(value, ['foo', 'bar']) 243 }] 244 ] 245 } 246 247 exports['array/varchar'] = { 248 format: 'text', 249 id: 1015, 250 tests: [ 251 ['{foo,bar}', function (t, value) { 252 t.deepEqual(value, ['foo', 'bar']) 253 }] 254 ] 255 } 256 257 exports['array/text'] = { 258 format: 'text', 259 id: 1008, 260 tests: [ 261 ['{foo}', function (t, value) { 262 t.deepEqual(value, ['foo']) 263 }] 264 ] 265 } 266 267 exports['array/bytea'] = { 268 format: 'text', 269 id: 1001, 270 tests: [ 271 ['{"\\\\x00000000"}', function (t, value) { 272 var buffer = new Buffer('00000000', 'hex') 273 t.ok(Array.isArray(value)) 274 t.equal(value.length, 1) 275 t.ok(buffer.equals(value[0])) 276 }], 277 ['{NULL,"\\\\x4e554c4c"}', function (t, value) { 278 var buffer = new Buffer('4e554c4c', 'hex') 279 t.ok(Array.isArray(value)) 280 t.equal(value.length, 2) 281 t.equal(value[0], null) 282 t.ok(buffer.equals(value[1])) 283 }], 284 ] 285 } 286 287 exports['array/numeric'] = { 288 format: 'text', 289 id: 1231, 290 tests: [ 291 ['{1.2,3.4}', function (t, value) { 292 t.deepEqual(value, [1.2, 3.4]) 293 }] 294 ] 295 } 296 297 exports['array/int2'] = { 298 format: 'text', 299 id: 1005, 300 tests: [ 301 ['{-32768, -32767, 32766, 32767}', function (t, value) { 302 t.deepEqual(value, [-32768, -32767, 32766, 32767]) 303 }] 304 ] 305 } 306 307 exports['array/int4'] = { 308 format: 'text', 309 id: 1005, 310 tests: [ 311 ['{-2147483648, -2147483647, 2147483646, 2147483647}', function (t, value) { 312 t.deepEqual(value, [-2147483648, -2147483647, 2147483646, 2147483647]) 313 }] 314 ] 315 } 316 317 exports['array/int8'] = { 318 format: 'text', 319 id: 1016, 320 tests: [ 321 [ 322 '{-9223372036854775808, -9223372036854775807, 9223372036854775806, 9223372036854775807}', 323 function (t, value) { 324 t.deepEqual(value, [ 325 '-9223372036854775808', 326 '-9223372036854775807', 327 '9223372036854775806', 328 '9223372036854775807' 329 ]) 330 } 331 ] 332 ] 333 } 334 335 exports['array/json'] = { 336 format: 'text', 337 id: 199, 338 tests: [ 339 [ 340 '{{1,2},{[3],"[4,5]"},{null,NULL}}', 341 function (t, value) { 342 t.deepEqual(value, [ 343 [1, 2], 344 [[3], [4, 5]], 345 [null, null], 346 ]) 347 } 348 ] 349 ] 350 } 351 352 exports['array/jsonb'] = { 353 format: 'text', 354 id: 3807, 355 tests: exports['array/json'].tests 356 } 357 358 exports['array/point'] = { 359 format: 'text', 360 id: 1017, 361 tests: [ 362 ['{"(25.1,50.5)","(10.1,40)"}', function (t, value) { 363 t.deepEqual(value, [{x: 25.1, y: 50.5}, {x: 10.1, y: 40}]) 364 }] 365 ] 366 } 367 368 exports['array/oid'] = { 369 format: 'text', 370 id: 1028, 371 tests: [ 372 ['{25864,25860}', function (t, value) { 373 t.deepEqual(value, [25864, 25860]) 374 }] 375 ] 376 } 377 378 exports['array/float4'] = { 379 format: 'text', 380 id: 1021, 381 tests: [ 382 ['{1.2, 3.4}', function (t, value) { 383 t.deepEqual(value, [1.2, 3.4]) 384 }] 385 ] 386 } 387 388 exports['array/float8'] = { 389 format: 'text', 390 id: 1022, 391 tests: [ 392 ['{-12345678.1234567, 12345678.12345678}', function (t, value) { 393 t.deepEqual(value, [-12345678.1234567, 12345678.12345678]) 394 }] 395 ] 396 } 397 398 exports['array/date'] = { 399 format: 'text', 400 id: 1182, 401 tests: [ 402 ['{2014-01-01,2015-12-31}', function (t, value) { 403 var expecteds = [new Date(2014, 0, 1), new Date(2015, 11, 31)] 404 t.equal(value.length, 2) 405 value.forEach(function (date, index) { 406 var expected = expecteds[index] 407 dateEquals( 408 expected.getUTCFullYear(), 409 expected.getUTCMonth(), 410 expected.getUTCDate(), 411 expected.getUTCHours(), 0, 0, 0)(t, date) 412 }) 413 }] 414 ] 415 } 416 417 exports['array/interval'] = { 418 format: 'text', 419 id: 1187, 420 tests: [ 421 ['{01:02:03,1 day -00:00:03}', function (t, value) { 422 var expecteds = [{hours: 1, minutes: 2, seconds: 3}, 423 {days: 1, seconds: -3}] 424 t.equal(value.length, 2) 425 t.deepEqual(value, expecteds); 426 }] 427 ] 428 } 429 430 exports['array/inet'] = { 431 format: 'text', 432 id: 1041, 433 tests: [ 434 ['{8.8.8.8}', function (t, value) { 435 t.deepEqual(value, ['8.8.8.8']); 436 }], 437 ['{2001:4860:4860::8888}', function (t, value) { 438 t.deepEqual(value, ['2001:4860:4860::8888']); 439 }], 440 ['{127.0.0.1,fd00:1::40e,1.2.3.4}', function (t, value) { 441 t.deepEqual(value, ['127.0.0.1', 'fd00:1::40e', '1.2.3.4']); 442 }] 443 ] 444 } 445 446 exports['array/cidr'] = { 447 format: 'text', 448 id: 651, 449 tests: [ 450 ['{172.16.0.0/12}', function (t, value) { 451 t.deepEqual(value, ['172.16.0.0/12']); 452 }], 453 ['{fe80::/10}', function (t, value) { 454 t.deepEqual(value, ['fe80::/10']); 455 }], 456 ['{10.0.0.0/8,fc00::/7,192.168.0.0/24}', function (t, value) { 457 t.deepEqual(value, ['10.0.0.0/8', 'fc00::/7', '192.168.0.0/24']); 458 }] 459 ] 460 } 461 462 exports['array/macaddr'] = { 463 format: 'text', 464 id: 1040, 465 tests: [ 466 ['{08:00:2b:01:02:03,16:10:9f:0d:66:00}', function (t, value) { 467 t.deepEqual(value, ['08:00:2b:01:02:03', '16:10:9f:0d:66:00']); 468 }] 469 ] 470 } 471 472 exports['array/numrange'] = { 473 format: 'text', 474 id: 3907, 475 tests: [ 476 ['{"[1,2]","(4.5,8)","[10,40)","(-21.2,60.3]"}', function (t, value) { 477 t.deepEqual(value, ['[1,2]', '(4.5,8)', '[10,40)', '(-21.2,60.3]']); 478 }], 479 ['{"[,20]","[3,]","[,]","(,35)","(1,)","(,)"}', function (t, value) { 480 t.deepEqual(value, ['[,20]', '[3,]', '[,]', '(,35)', '(1,)', '(,)']); 481 }], 482 ['{"[,20)","[3,)","[,)","[,35)","[1,)","[,)"}', function (t, value) { 483 t.deepEqual(value, ['[,20)', '[3,)', '[,)', '[,35)', '[1,)', '[,)']); 484 }] 485 ] 486 } 487 488 exports['binary-string/varchar'] = { 489 format: 'binary', 490 id: 1043, 491 tests: [ 492 ['bang', 'bang'] 493 ] 494 } 495 496 exports['binary-integer/int4'] = { 497 format: 'binary', 498 id: 23, 499 tests: [ 500 [[0, 0, 0, 100], 100] 501 ] 502 } 503 504 exports['binary-smallint/int2'] = { 505 format: 'binary', 506 id: 21, 507 tests: [ 508 [[0, 101], 101] 509 ] 510 } 511 512 exports['binary-bigint/int8'] = { 513 format: 'binary', 514 id: 20, 515 tests: [ 516 [new Buffer([0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), '9223372036854775807'] 517 ] 518 } 519 520 exports['binary-oid'] = { 521 format: 'binary', 522 id: 26, 523 tests: [ 524 [[0, 0, 0, 103], 103] 525 ] 526 } 527 528 exports['binary-numeric'] = { 529 format: 'binary', 530 id: 1700, 531 tests: [ 532 [ 533 [0, 2, 0, 0, 0, 0, 0, hex('0x64'), 0, 12, hex('0xd'), hex('0x48'), 0, 0, 0, 0], 534 12.34 535 ] 536 ] 537 } 538 539 exports['binary-real/float4'] = { 540 format: 'binary', 541 id: 700, 542 tests: [ 543 [['0x41', '0x48', '0x00', '0x00'].map(hex), 12.5] 544 ] 545 } 546 547 exports['binary-boolean'] = { 548 format: 'binary', 549 id: 16, 550 tests: [ 551 [[1], true], 552 [[0], false], 553 [null, null] 554 ] 555 } 556 557 exports['binary-string'] = { 558 format: 'binary', 559 id: 25, 560 tests: [ 561 [ 562 new Buffer(['0x73', '0x6c', '0x61', '0x64', '0x64', '0x61'].map(hex)), 563 'sladda' 564 ] 565 ] 566 } 567 568 exports.point = { 569 format: 'text', 570 id: 600, 571 tests: [ 572 ['(25.1,50.5)', function (t, value) { 573 t.deepEqual(value, {x: 25.1, y: 50.5}) 574 }] 575 ] 576 } 577 578 exports.circle = { 579 format: 'text', 580 id: 718, 581 tests: [ 582 ['<(25,10),5>', function (t, value) { 583 t.deepEqual(value, {x: 25, y: 10, radius: 5}) 584 }] 585 ] 586 } 587 588 function hex (string) { 589 return parseInt(string, 16) 590 } 591 592 function dateEquals () { 593 var timestamp = Date.UTC.apply(Date, arguments) 594 return function (t, value) { 595 t.equal(value.toUTCString(), new Date(timestamp).toUTCString()) 596 } 597 }