query.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. 'use strict';
  2. const { DataTypes } = require('@sequelize/core');
  3. const {
  4. AbstractQuery: Query,
  5. } = require('@sequelize/core/_non-semver-use-at-your-own-risk_/abstract-dialect/query.js');
  6. const Support = require('../../../support');
  7. const chai = require('chai');
  8. const { match, stub } = require('sinon');
  9. const current = Support.sequelize;
  10. const expect = chai.expect;
  11. describe('[ABSTRACT]', () => {
  12. describe('_groupJoinData', () => {
  13. it('should hash second nested set correctly, when has multiple primary keys and one is a Buffer', () => {
  14. const Team = current.define('team', {
  15. id: {
  16. primaryKey: true,
  17. type: DataTypes.STRING(1),
  18. },
  19. name: {
  20. type: DataTypes.TEXT,
  21. },
  22. });
  23. const Player = current.define('player', {
  24. id: {
  25. primaryKey: true,
  26. type: DataTypes.STRING(1),
  27. },
  28. });
  29. const Agent = current.define('agent', {
  30. uuid: {
  31. primaryKey: true,
  32. type: 'BINARY(16)',
  33. },
  34. id: {
  35. primaryKey: true,
  36. type: DataTypes.STRING(1),
  37. },
  38. });
  39. Team.Player = Team.hasMany(Player, { foreignKey: 'teamId' });
  40. Team.Agent = Team.hasMany(Agent, { foreignKey: 'teamId' });
  41. const includeOptions = {
  42. model: Team,
  43. includeMap: {
  44. players: {
  45. model: Player,
  46. association: Team.Player,
  47. },
  48. agents: {
  49. model: Agent,
  50. association: Team.Agent,
  51. },
  52. },
  53. };
  54. const agentOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  55. const agentTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  56. const data = [
  57. {
  58. id: 'a',
  59. 'players.id': '1-1',
  60. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  61. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  62. 'agents.uuid': agentOneUuid,
  63. name: 'vansh',
  64. 'agents.id': 'p',
  65. 'agents.name': 'One',
  66. },
  67. {
  68. id: 'a',
  69. 'players.id': '2-1',
  70. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  71. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  72. 'agents.uuid': agentTwoUuid,
  73. name: 'joe',
  74. 'agents.id': 'z',
  75. 'agents.name': 'Two',
  76. },
  77. ];
  78. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  79. expect(result.length).to.equal(1);
  80. expect(result[0]).to.have.property('id').and.be.equal('a');
  81. expect(result[0]).to.have.property('name').and.be.ok;
  82. expect(result[0].agents).to.be.deep.equal([
  83. {
  84. id: 'p',
  85. uuid: agentOneUuid,
  86. name: 'One',
  87. },
  88. {
  89. id: 'z',
  90. uuid: agentTwoUuid,
  91. name: 'Two',
  92. },
  93. ]);
  94. });
  95. it('should hash second nested set correctly, when primary is a Buffer', () => {
  96. const Team = current.define('team', {
  97. id: {
  98. primaryKey: true,
  99. type: DataTypes.STRING(1),
  100. },
  101. });
  102. const Player = current.define('player', {
  103. id: {
  104. primaryKey: true,
  105. type: DataTypes.STRING(1),
  106. },
  107. });
  108. const Agent = current.define('agent', {
  109. uuid: {
  110. primaryKey: true,
  111. type: 'BINARY(16)',
  112. },
  113. });
  114. Team.Player = Team.hasMany(Player, { foreignKey: 'teamId' });
  115. Team.Agent = Team.hasMany(Agent, { foreignKey: 'teamId' });
  116. const includeOptions = {
  117. model: Team,
  118. includeMap: {
  119. players: {
  120. model: Player,
  121. association: Team.Player,
  122. },
  123. agents: {
  124. model: Agent,
  125. association: Team.Agent,
  126. },
  127. },
  128. };
  129. const agentOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  130. const agentTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  131. const data = [
  132. {
  133. id: 'a',
  134. 'players.id': '1-1',
  135. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  136. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  137. 'agents.uuid': agentOneUuid,
  138. 'agents.name': 'One',
  139. },
  140. {
  141. id: 'a',
  142. 'players.id': '2-1',
  143. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  144. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  145. 'agents.uuid': agentTwoUuid,
  146. 'agents.name': 'Two',
  147. },
  148. ];
  149. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  150. expect(result.length).to.equal(1);
  151. expect(result[0]).to.have.property('id').and.be.equal('a');
  152. expect(result[0].agents).to.be.deep.equal([
  153. {
  154. uuid: agentOneUuid,
  155. name: 'One',
  156. },
  157. {
  158. uuid: agentTwoUuid,
  159. name: 'Two',
  160. },
  161. ]);
  162. });
  163. it('should hash parents correctly, when has multiple primary keys and one is a Buffer', () => {
  164. const Team = current.define('team', {
  165. uuid: {
  166. primaryKey: true,
  167. type: 'BINARY(16)',
  168. },
  169. id: {
  170. primaryKey: true,
  171. type: DataTypes.STRING(1),
  172. },
  173. });
  174. const Player = current.define('player', {
  175. id: {
  176. primaryKey: true,
  177. type: DataTypes.STRING(1),
  178. },
  179. });
  180. const association = Team.hasMany(Player, { foreignKey: 'teamId' });
  181. const includeOptions = {
  182. model: Team,
  183. includeMap: {
  184. players: {
  185. model: Player,
  186. association,
  187. },
  188. },
  189. };
  190. const teamOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  191. const teamTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  192. const data = [
  193. {
  194. uuid: teamOneUuid,
  195. id: 'x',
  196. 'players.id': '1-1',
  197. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  198. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  199. },
  200. {
  201. uuid: teamTwoUuid,
  202. id: 'y',
  203. 'players.id': '2-1',
  204. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  205. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  206. },
  207. {
  208. uuid: teamOneUuid,
  209. id: 'x',
  210. 'players.id': '1-2',
  211. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  212. 'players.lastModified': new Date('2017-08-24T11:16:44.000Z'),
  213. },
  214. ];
  215. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  216. expect(result.length).to.equal(2);
  217. expect(result[0]).to.have.property('uuid').and.be.equal(teamOneUuid);
  218. expect(result[0].players).to.be.deep.equal([
  219. {
  220. id: '1-1',
  221. created: new Date('2017-03-06T15:47:30.000Z'),
  222. lastModified: new Date('2017-03-06T15:47:30.000Z'),
  223. },
  224. {
  225. id: '1-2',
  226. created: new Date('2017-03-06T15:47:30.000Z'),
  227. lastModified: new Date('2017-08-24T11:16:44.000Z'),
  228. },
  229. ]);
  230. expect(result[1]).to.have.property('uuid').and.be.equal(teamTwoUuid);
  231. expect(result[1].players).to.be.deep.equal([
  232. {
  233. id: '2-1',
  234. created: new Date('2017-03-06T15:47:30.000Z'),
  235. lastModified: new Date('2017-08-22T11:16:44.000Z'),
  236. },
  237. ]);
  238. });
  239. it('should hash parents correctly, when primary key is a Buffer', () => {
  240. const Team = current.define('team', {
  241. uuid: {
  242. primaryKey: true,
  243. type: 'BINARY(16)',
  244. },
  245. });
  246. const Player = current.define('player', {
  247. id: {
  248. primaryKey: true,
  249. type: DataTypes.STRING(1),
  250. },
  251. });
  252. const association = Team.hasMany(Player, { foreignKey: 'teamId' });
  253. const includeOptions = {
  254. model: Team,
  255. includeMap: {
  256. players: {
  257. model: Player,
  258. association,
  259. },
  260. },
  261. };
  262. const teamOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  263. const teamTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  264. const data = [
  265. {
  266. uuid: teamOneUuid,
  267. 'players.id': '1-1',
  268. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  269. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  270. },
  271. {
  272. uuid: teamTwoUuid,
  273. 'players.id': '2-1',
  274. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  275. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  276. },
  277. {
  278. uuid: teamOneUuid,
  279. 'players.id': '1-2',
  280. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  281. 'players.lastModified': new Date('2017-08-24T11:16:44.000Z'),
  282. },
  283. ];
  284. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  285. expect(result.length).to.equal(2);
  286. expect(result[0]).to.have.property('uuid').and.be.equal(teamOneUuid);
  287. expect(result[0].players).to.be.deep.equal([
  288. {
  289. id: '1-1',
  290. created: new Date('2017-03-06T15:47:30.000Z'),
  291. lastModified: new Date('2017-03-06T15:47:30.000Z'),
  292. },
  293. {
  294. id: '1-2',
  295. created: new Date('2017-03-06T15:47:30.000Z'),
  296. lastModified: new Date('2017-08-24T11:16:44.000Z'),
  297. },
  298. ]);
  299. expect(result[1]).to.have.property('uuid').and.be.equal(teamTwoUuid);
  300. expect(result[1].players).to.be.deep.equal([
  301. {
  302. id: '2-1',
  303. created: new Date('2017-03-06T15:47:30.000Z'),
  304. lastModified: new Date('2017-08-22T11:16:44.000Z'),
  305. },
  306. ]);
  307. });
  308. it('should hash nested correctly, when primary key is a Buffer', () => {
  309. const Team = current.define('team', {
  310. id: {
  311. primaryKey: true,
  312. type: DataTypes.STRING(1),
  313. },
  314. });
  315. const Player = current.define('player', {
  316. uuid: {
  317. primaryKey: true,
  318. type: 'BINARY(16)',
  319. },
  320. });
  321. const association = Team.hasMany(Player, { foreignKey: 'teamId' });
  322. const includeOptions = {
  323. model: Team,
  324. includeMap: {
  325. players: {
  326. model: Player,
  327. association,
  328. },
  329. },
  330. };
  331. const playerOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  332. const playerTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  333. const data = [
  334. {
  335. id: '1',
  336. 'players.uuid': playerOneUuid,
  337. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  338. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  339. },
  340. {
  341. id: '1',
  342. 'players.uuid': playerTwoUuid,
  343. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  344. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  345. },
  346. ];
  347. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  348. expect(result.length).to.equal(1);
  349. expect(result[0]).to.have.property('id').and.be.equal('1');
  350. expect(result[0].players).to.be.deep.equal([
  351. {
  352. uuid: playerOneUuid,
  353. created: new Date('2017-03-06T15:47:30.000Z'),
  354. lastModified: new Date('2017-03-06T15:47:30.000Z'),
  355. },
  356. {
  357. uuid: playerTwoUuid,
  358. created: new Date('2017-03-06T15:47:30.000Z'),
  359. lastModified: new Date('2017-08-22T11:16:44.000Z'),
  360. },
  361. ]);
  362. });
  363. it('should hash nested correctly, when has multiple primary keys and one is a Buffer', () => {
  364. const Team = current.define('team', {
  365. id: {
  366. primaryKey: true,
  367. type: DataTypes.STRING(1),
  368. },
  369. });
  370. const Player = current.define('player', {
  371. uuid: {
  372. primaryKey: true,
  373. type: 'BINARY(16)',
  374. },
  375. id: {
  376. primaryKey: true,
  377. type: DataTypes.STRING(1),
  378. },
  379. });
  380. const association = Team.hasMany(Player, { foreignKey: 'teamId' });
  381. const includeOptions = {
  382. model: Team,
  383. includeMap: {
  384. players: {
  385. model: Player,
  386. association,
  387. },
  388. },
  389. };
  390. const playerOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
  391. const playerTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
  392. const data = [
  393. {
  394. id: '1',
  395. 'players.uuid': playerOneUuid,
  396. 'players.id': 'x',
  397. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  398. 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
  399. },
  400. {
  401. id: '1',
  402. 'players.uuid': playerTwoUuid,
  403. 'players.id': 'y',
  404. 'players.created': new Date('2017-03-06T15:47:30.000Z'),
  405. 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
  406. },
  407. ];
  408. const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
  409. expect(result.length).to.equal(1);
  410. expect(result[0]).to.have.property('id').and.be.equal('1');
  411. expect(result[0].players).to.be.deep.equal([
  412. {
  413. uuid: playerOneUuid,
  414. id: 'x',
  415. created: new Date('2017-03-06T15:47:30.000Z'),
  416. lastModified: new Date('2017-03-06T15:47:30.000Z'),
  417. },
  418. {
  419. uuid: playerTwoUuid,
  420. id: 'y',
  421. created: new Date('2017-03-06T15:47:30.000Z'),
  422. lastModified: new Date('2017-08-22T11:16:44.000Z'),
  423. },
  424. ]);
  425. });
  426. });
  427. describe('_logQuery', () => {
  428. beforeEach(function () {
  429. this.cls = class MyQuery extends Query {};
  430. this.sequelizeStub = {
  431. log: stub(),
  432. options: {},
  433. };
  434. this.connectionStub = {
  435. uuid: 'test',
  436. };
  437. });
  438. it('logs before and after', function () {
  439. const debugStub = stub();
  440. const qry = new this.cls(this.connectionStub, this.sequelizeStub, {});
  441. const complete = qry._logQuery('SELECT 1', debugStub);
  442. complete();
  443. expect(this.sequelizeStub.log).to.have.been.calledOnce;
  444. expect(this.sequelizeStub.log).to.have.been.calledWithMatch('Executing (test): SELECT 1');
  445. expect(debugStub).to.have.been.calledWith('Executing (test): SELECT 1');
  446. expect(debugStub).to.have.been.calledWith('Executed (test): SELECT 1');
  447. });
  448. it('logs before and after with benchmark', function () {
  449. const debugStub = stub();
  450. const qry = new this.cls(this.connectionStub, this.sequelizeStub, { benchmark: true });
  451. const complete = qry._logQuery('SELECT 1', debugStub);
  452. complete();
  453. expect(this.sequelizeStub.log).to.have.been.calledOnce;
  454. expect(this.sequelizeStub.log).to.have.been.calledWithMatch(
  455. 'Executed (test): SELECT 1',
  456. match.number,
  457. { benchmark: true },
  458. );
  459. expect(debugStub).to.have.been.calledWith('Executing (test): SELECT 1');
  460. expect(debugStub).to.have.been.calledWith('Executed (test): SELECT 1');
  461. });
  462. it('supports logging bigints', function () {
  463. // this test was added because while most of the time `bigint`s are stringified,
  464. // they're not always. For instance, if the PK is a bigint, calling .save()
  465. // will pass the PK as a bigint instead of a string as a parameter.
  466. // This is fine, as bigints should be supported natively,
  467. // but AbstractQuery#_logQuery used JSON.stringify on parameters,
  468. // which does not support serializing bigints (https://github.com/tc39/proposal-bigint/issues/24)
  469. // This test was added to ensure bigints don't cause a crash
  470. // when `logQueryParameters` is true.
  471. const sequelizeStub = {
  472. ...this.sequelizeStub,
  473. options: {
  474. ...this.sequelizeStub.options,
  475. logQueryParameters: true,
  476. },
  477. };
  478. const debugStub = stub();
  479. const qry = new this.cls(this.connectionStub, sequelizeStub, {});
  480. const complete = qry._logQuery('SELECT 1', debugStub, [1n]);
  481. complete();
  482. expect(debugStub).to.have.been.calledWith(
  483. 'Executing (test): SELECT 1; with parameters [ 1n ]',
  484. );
  485. expect(debugStub).to.have.been.calledWith(
  486. 'Executed (test): SELECT 1; with parameters [ 1n ]',
  487. );
  488. });
  489. });
  490. });