123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- 'use strict';
- const { DataTypes } = require('@sequelize/core');
- const {
- AbstractQuery: Query,
- } = require('@sequelize/core/_non-semver-use-at-your-own-risk_/abstract-dialect/query.js');
- const Support = require('../../../support');
- const chai = require('chai');
- const { match, stub } = require('sinon');
- const current = Support.sequelize;
- const expect = chai.expect;
- describe('[ABSTRACT]', () => {
- describe('_groupJoinData', () => {
- it('should hash second nested set correctly, when has multiple primary keys and one is a Buffer', () => {
- const Team = current.define('team', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- name: {
- type: DataTypes.TEXT,
- },
- });
- const Player = current.define('player', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Agent = current.define('agent', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- Team.Player = Team.hasMany(Player, { foreignKey: 'teamId' });
- Team.Agent = Team.hasMany(Agent, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association: Team.Player,
- },
- agents: {
- model: Agent,
- association: Team.Agent,
- },
- },
- };
- const agentOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const agentTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- id: 'a',
- 'players.id': '1-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- 'agents.uuid': agentOneUuid,
- name: 'vansh',
- 'agents.id': 'p',
- 'agents.name': 'One',
- },
- {
- id: 'a',
- 'players.id': '2-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- 'agents.uuid': agentTwoUuid,
- name: 'joe',
- 'agents.id': 'z',
- 'agents.name': 'Two',
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(1);
- expect(result[0]).to.have.property('id').and.be.equal('a');
- expect(result[0]).to.have.property('name').and.be.ok;
- expect(result[0].agents).to.be.deep.equal([
- {
- id: 'p',
- uuid: agentOneUuid,
- name: 'One',
- },
- {
- id: 'z',
- uuid: agentTwoUuid,
- name: 'Two',
- },
- ]);
- });
- it('should hash second nested set correctly, when primary is a Buffer', () => {
- const Team = current.define('team', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Player = current.define('player', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Agent = current.define('agent', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- });
- Team.Player = Team.hasMany(Player, { foreignKey: 'teamId' });
- Team.Agent = Team.hasMany(Agent, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association: Team.Player,
- },
- agents: {
- model: Agent,
- association: Team.Agent,
- },
- },
- };
- const agentOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const agentTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- id: 'a',
- 'players.id': '1-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- 'agents.uuid': agentOneUuid,
- 'agents.name': 'One',
- },
- {
- id: 'a',
- 'players.id': '2-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- 'agents.uuid': agentTwoUuid,
- 'agents.name': 'Two',
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(1);
- expect(result[0]).to.have.property('id').and.be.equal('a');
- expect(result[0].agents).to.be.deep.equal([
- {
- uuid: agentOneUuid,
- name: 'One',
- },
- {
- uuid: agentTwoUuid,
- name: 'Two',
- },
- ]);
- });
- it('should hash parents correctly, when has multiple primary keys and one is a Buffer', () => {
- const Team = current.define('team', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Player = current.define('player', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const association = Team.hasMany(Player, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association,
- },
- },
- };
- const teamOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const teamTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- uuid: teamOneUuid,
- id: 'x',
- 'players.id': '1-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- uuid: teamTwoUuid,
- id: 'y',
- 'players.id': '2-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- },
- {
- uuid: teamOneUuid,
- id: 'x',
- 'players.id': '1-2',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-24T11:16:44.000Z'),
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(2);
- expect(result[0]).to.have.property('uuid').and.be.equal(teamOneUuid);
- expect(result[0].players).to.be.deep.equal([
- {
- id: '1-1',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- id: '1-2',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-24T11:16:44.000Z'),
- },
- ]);
- expect(result[1]).to.have.property('uuid').and.be.equal(teamTwoUuid);
- expect(result[1].players).to.be.deep.equal([
- {
- id: '2-1',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-22T11:16:44.000Z'),
- },
- ]);
- });
- it('should hash parents correctly, when primary key is a Buffer', () => {
- const Team = current.define('team', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- });
- const Player = current.define('player', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const association = Team.hasMany(Player, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association,
- },
- },
- };
- const teamOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const teamTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- uuid: teamOneUuid,
- 'players.id': '1-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- uuid: teamTwoUuid,
- 'players.id': '2-1',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- },
- {
- uuid: teamOneUuid,
- 'players.id': '1-2',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-24T11:16:44.000Z'),
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(2);
- expect(result[0]).to.have.property('uuid').and.be.equal(teamOneUuid);
- expect(result[0].players).to.be.deep.equal([
- {
- id: '1-1',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- id: '1-2',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-24T11:16:44.000Z'),
- },
- ]);
- expect(result[1]).to.have.property('uuid').and.be.equal(teamTwoUuid);
- expect(result[1].players).to.be.deep.equal([
- {
- id: '2-1',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-22T11:16:44.000Z'),
- },
- ]);
- });
- it('should hash nested correctly, when primary key is a Buffer', () => {
- const Team = current.define('team', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Player = current.define('player', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- });
- const association = Team.hasMany(Player, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association,
- },
- },
- };
- const playerOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const playerTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- id: '1',
- 'players.uuid': playerOneUuid,
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- id: '1',
- 'players.uuid': playerTwoUuid,
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(1);
- expect(result[0]).to.have.property('id').and.be.equal('1');
- expect(result[0].players).to.be.deep.equal([
- {
- uuid: playerOneUuid,
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- uuid: playerTwoUuid,
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-22T11:16:44.000Z'),
- },
- ]);
- });
- it('should hash nested correctly, when has multiple primary keys and one is a Buffer', () => {
- const Team = current.define('team', {
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const Player = current.define('player', {
- uuid: {
- primaryKey: true,
- type: 'BINARY(16)',
- },
- id: {
- primaryKey: true,
- type: DataTypes.STRING(1),
- },
- });
- const association = Team.hasMany(Player, { foreignKey: 'teamId' });
- const includeOptions = {
- model: Team,
- includeMap: {
- players: {
- model: Player,
- association,
- },
- },
- };
- const playerOneUuid = Buffer.from('966ea4c3028c11e7bc99a99d4c0d78cf', 'hex');
- const playerTwoUuid = Buffer.from('966ecbd0028c11e7bc99a99d4c0d78cf', 'hex');
- const data = [
- {
- id: '1',
- 'players.uuid': playerOneUuid,
- 'players.id': 'x',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- id: '1',
- 'players.uuid': playerTwoUuid,
- 'players.id': 'y',
- 'players.created': new Date('2017-03-06T15:47:30.000Z'),
- 'players.lastModified': new Date('2017-08-22T11:16:44.000Z'),
- },
- ];
- const result = Query._groupJoinData(data, includeOptions, { checkExisting: true });
- expect(result.length).to.equal(1);
- expect(result[0]).to.have.property('id').and.be.equal('1');
- expect(result[0].players).to.be.deep.equal([
- {
- uuid: playerOneUuid,
- id: 'x',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-03-06T15:47:30.000Z'),
- },
- {
- uuid: playerTwoUuid,
- id: 'y',
- created: new Date('2017-03-06T15:47:30.000Z'),
- lastModified: new Date('2017-08-22T11:16:44.000Z'),
- },
- ]);
- });
- });
- describe('_logQuery', () => {
- beforeEach(function () {
- this.cls = class MyQuery extends Query {};
- this.sequelizeStub = {
- log: stub(),
- options: {},
- };
- this.connectionStub = {
- uuid: 'test',
- };
- });
- it('logs before and after', function () {
- const debugStub = stub();
- const qry = new this.cls(this.connectionStub, this.sequelizeStub, {});
- const complete = qry._logQuery('SELECT 1', debugStub);
- complete();
- expect(this.sequelizeStub.log).to.have.been.calledOnce;
- expect(this.sequelizeStub.log).to.have.been.calledWithMatch('Executing (test): SELECT 1');
- expect(debugStub).to.have.been.calledWith('Executing (test): SELECT 1');
- expect(debugStub).to.have.been.calledWith('Executed (test): SELECT 1');
- });
- it('logs before and after with benchmark', function () {
- const debugStub = stub();
- const qry = new this.cls(this.connectionStub, this.sequelizeStub, { benchmark: true });
- const complete = qry._logQuery('SELECT 1', debugStub);
- complete();
- expect(this.sequelizeStub.log).to.have.been.calledOnce;
- expect(this.sequelizeStub.log).to.have.been.calledWithMatch(
- 'Executed (test): SELECT 1',
- match.number,
- { benchmark: true },
- );
- expect(debugStub).to.have.been.calledWith('Executing (test): SELECT 1');
- expect(debugStub).to.have.been.calledWith('Executed (test): SELECT 1');
- });
- it('supports logging bigints', function () {
- // this test was added because while most of the time `bigint`s are stringified,
- // they're not always. For instance, if the PK is a bigint, calling .save()
- // will pass the PK as a bigint instead of a string as a parameter.
- // This is fine, as bigints should be supported natively,
- // but AbstractQuery#_logQuery used JSON.stringify on parameters,
- // which does not support serializing bigints (https://github.com/tc39/proposal-bigint/issues/24)
- // This test was added to ensure bigints don't cause a crash
- // when `logQueryParameters` is true.
- const sequelizeStub = {
- ...this.sequelizeStub,
- options: {
- ...this.sequelizeStub.options,
- logQueryParameters: true,
- },
- };
- const debugStub = stub();
- const qry = new this.cls(this.connectionStub, sequelizeStub, {});
- const complete = qry._logQuery('SELECT 1', debugStub, [1n]);
- complete();
- expect(debugStub).to.have.been.calledWith(
- 'Executing (test): SELECT 1; with parameters [ 1n ]',
- );
- expect(debugStub).to.have.been.calledWith(
- 'Executed (test): SELECT 1; with parameters [ 1n ]',
- );
- });
- });
- });
|