query.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const { MsSqlQuery: Query } = require('@sequelize/mssql');
  3. const Support = require('../../../support');
  4. const dialect = Support.getTestDialect();
  5. const sequelize = Support.sequelize;
  6. const expect = require('chai').expect;
  7. const tedious = require('tedious');
  8. const connectionStub = { lib: tedious };
  9. let query;
  10. if (dialect === 'mssql') {
  11. describe('[MSSQL Specific] Query', () => {
  12. beforeEach(() => {
  13. const options = {
  14. transaction: { name: 'transactionName' },
  15. isolationLevel: 'REPEATABLE_READ',
  16. logging: false,
  17. };
  18. query = new Query(connectionStub, sequelize, options);
  19. });
  20. describe('getSQLTypeFromJsType', () => {
  21. const TYPES = tedious.TYPES;
  22. it('should return correct parameter type', () => {
  23. expect(query.getSQLTypeFromJsType(2_147_483_647, TYPES)).to.eql({
  24. type: TYPES.Int,
  25. typeOptions: {},
  26. value: 2_147_483_647,
  27. });
  28. expect(query.getSQLTypeFromJsType(-2_147_483_648, TYPES)).to.eql({
  29. type: TYPES.Int,
  30. typeOptions: {},
  31. value: -2_147_483_648,
  32. });
  33. expect(query.getSQLTypeFromJsType(2_147_483_648, TYPES)).to.eql({
  34. type: TYPES.BigInt,
  35. typeOptions: {},
  36. value: 2_147_483_648,
  37. });
  38. expect(query.getSQLTypeFromJsType(-2_147_483_649, TYPES)).to.eql({
  39. type: TYPES.BigInt,
  40. typeOptions: {},
  41. value: -2_147_483_649,
  42. });
  43. expect(query.getSQLTypeFromJsType(2_147_483_647n, TYPES)).to.eql({
  44. type: TYPES.Int,
  45. typeOptions: {},
  46. value: 2_147_483_647,
  47. });
  48. expect(query.getSQLTypeFromJsType(-2_147_483_648n, TYPES)).to.eql({
  49. type: TYPES.Int,
  50. typeOptions: {},
  51. value: -2_147_483_648,
  52. });
  53. expect(query.getSQLTypeFromJsType(BigInt(Number.MAX_SAFE_INTEGER), TYPES)).to.eql({
  54. type: TYPES.BigInt,
  55. typeOptions: {},
  56. value: Number.MAX_SAFE_INTEGER,
  57. });
  58. expect(query.getSQLTypeFromJsType(BigInt(Number.MIN_SAFE_INTEGER), TYPES)).to.eql({
  59. type: TYPES.BigInt,
  60. typeOptions: {},
  61. value: Number.MIN_SAFE_INTEGER,
  62. });
  63. const overMaxSafe = BigInt(Number.MAX_SAFE_INTEGER) + 1n;
  64. expect(query.getSQLTypeFromJsType(overMaxSafe, TYPES)).to.eql({
  65. type: TYPES.VarChar,
  66. typeOptions: {},
  67. value: overMaxSafe.toString(),
  68. });
  69. const underMinSafe = BigInt(Number.MIN_SAFE_INTEGER) - 1n;
  70. expect(query.getSQLTypeFromJsType(underMinSafe, TYPES)).to.eql({
  71. type: TYPES.VarChar,
  72. typeOptions: {},
  73. value: underMinSafe.toString(),
  74. });
  75. const buffer = Buffer.from('abc');
  76. expect(query.getSQLTypeFromJsType(buffer, TYPES)).to.eql({
  77. type: TYPES.VarBinary,
  78. typeOptions: {},
  79. value: buffer,
  80. });
  81. });
  82. it('should return parameter type correct scale for float', () => {
  83. expect(query.getSQLTypeFromJsType(1.23, TYPES)).to.eql({
  84. type: TYPES.Numeric,
  85. typeOptions: { precision: 30, scale: 2 },
  86. value: 1.23,
  87. });
  88. expect(query.getSQLTypeFromJsType(0.300_000_000_000_000_04, TYPES)).to.eql({
  89. type: TYPES.Numeric,
  90. typeOptions: { precision: 30, scale: 17 },
  91. value: 0.300_000_000_000_000_04,
  92. });
  93. expect(query.getSQLTypeFromJsType(2.5e-15, TYPES)).to.eql({
  94. type: TYPES.Numeric,
  95. typeOptions: { precision: 30, scale: 16 },
  96. value: 2.5e-15,
  97. });
  98. });
  99. });
  100. });
  101. }