A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.8 KiB

12 years ago
  1. var common = require('../../common');
  2. var test = require('utest');
  3. var assert = require('assert');
  4. var SqlString = require(common.lib + '/protocol/SqlString');
  5. test('SqlString.escape', {
  6. 'undefined -> NULL': function() {
  7. assert.equal(SqlString.escape(undefined), 'NULL');
  8. },
  9. 'null -> NULL': function() {
  10. assert.equal(SqlString.escape(null), 'NULL');
  11. },
  12. 'booleans convert to strings': function() {
  13. assert.equal(SqlString.escape(false), 'false');
  14. assert.equal(SqlString.escape(true), 'true');
  15. },
  16. 'numbers convert to strings': function() {
  17. assert.equal(SqlString.escape(5), '5');
  18. },
  19. 'objects are turned into key value pairs': function() {
  20. assert.equal(SqlString.escape({a: 'b', c: 'd'}), "`a` = 'b', `c` = 'd'");
  21. },
  22. 'objects function properties are ignored': function() {
  23. assert.equal(SqlString.escape({a: 'b', c: function() {}}), "`a` = 'b'");
  24. },
  25. 'nested objects are cast to strings': function() {
  26. assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'");
  27. },
  28. 'arrays are turned into lists': function() {
  29. assert.equal(SqlString.escape([1, 2, 'c']), "1, 2, 'c'");
  30. },
  31. 'nested arrays are turned into grouped lists': function() {
  32. assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')");
  33. },
  34. 'nested objects inside arrays are cast to strings': function() {
  35. assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '[object Object]', 2");
  36. },
  37. 'strings are quoted': function() {
  38. assert.equal(SqlString.escape('Super'), "'Super'");
  39. },
  40. '\0 gets escaped': function() {
  41. assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'");
  42. },
  43. '\b gets escaped': function() {
  44. assert.equal(SqlString.escape('Sup\ber'), "'Sup\\ber'");
  45. },
  46. '\n gets escaped': function() {
  47. assert.equal(SqlString.escape('Sup\ner'), "'Sup\\ner'");
  48. },
  49. '\r gets escaped': function() {
  50. assert.equal(SqlString.escape('Sup\rer'), "'Sup\\rer'");
  51. },
  52. '\t gets escaped': function() {
  53. assert.equal(SqlString.escape('Sup\ter'), "'Sup\\ter'");
  54. },
  55. '\\ gets escaped': function() {
  56. assert.equal(SqlString.escape('Sup\\er'), "'Sup\\\\er'");
  57. },
  58. '\u001a (ascii 26) gets replaced with \\Z': function() {
  59. assert.equal(SqlString.escape('Sup\u001aer'), "'Sup\\Zer'");
  60. },
  61. 'single quotes get escaped': function() {
  62. assert.equal(SqlString.escape('Sup\'er'), "'Sup\\'er'");
  63. },
  64. 'double quotes get escaped': function() {
  65. assert.equal(SqlString.escape('Sup"er'), "'Sup\\\"er'");
  66. },
  67. 'dates are converted to YYYY-MM-DD HH:II:SS': function() {
  68. var expected = '2012-05-07 11:42:03';
  69. var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3));
  70. var string = SqlString.escape(date);
  71. assert.strictEqual(string, "'" + expected + "'");
  72. },
  73. 'buffers are converted to hex': function() {
  74. var buffer = new Buffer([0, 1, 254, 255]);
  75. var string = SqlString.escape(buffer);
  76. assert.strictEqual(string, "X'0001feff'");
  77. },
  78. 'NaN -> NaN': function() {
  79. assert.equal(SqlString.escape(NaN), 'NaN');
  80. },
  81. 'Infinity -> Infinity': function() {
  82. assert.equal(SqlString.escape(Infinity), 'Infinity');
  83. }
  84. });
  85. test('SqlString.format', {
  86. 'question marks are replaced with escaped array values': function() {
  87. var sql = SqlString.format('? and ?', ['a', 'b']);
  88. assert.equal(sql, "'a' and 'b'");
  89. },
  90. 'extra question marks are left untouched': function() {
  91. var sql = SqlString.format('? and ?', ['a']);
  92. assert.equal(sql, "'a' and ?");
  93. },
  94. 'extra arguments are not used': function() {
  95. var sql = SqlString.format('? and ?', ['a', 'b', 'c']);
  96. assert.equal(sql, "'a' and 'b'");
  97. },
  98. 'question marks within values do not cause issues': function() {
  99. var sql = SqlString.format('? and ?', ['hello?', 'b']);
  100. assert.equal(sql, "'hello?' and 'b'");
  101. },
  102. });