associations.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const dialect = Support.getTestDialect();
  6. const { DataTypes } = require('@sequelize/core');
  7. if (dialect === 'mysql') {
  8. describe('[MYSQL Specific] Associations', () => {
  9. describe('many-to-many', () => {
  10. describe('where tables have the same prefix', () => {
  11. it('should create a table wp_table1wp_table2s', async function () {
  12. const Table2 = this.sequelize.define('wp_table2', { foo: DataTypes.STRING });
  13. const Table1 = this.sequelize.define('wp_table1', { foo: DataTypes.STRING });
  14. Table1.belongsToMany(Table2, { through: 'wp_table1swp_table2s' });
  15. Table2.belongsToMany(Table1, { through: 'wp_table1swp_table2s' });
  16. await Table1.sync({ force: true });
  17. await Table2.sync({ force: true });
  18. expect(this.sequelize.models.get('wp_table1swp_table2s')).to.exist;
  19. });
  20. });
  21. describe('when join table name is specified', () => {
  22. beforeEach(async function () {
  23. const Table2 = this.sequelize.define('ms_table1', { foo: DataTypes.STRING });
  24. const Table1 = this.sequelize.define('ms_table2', { foo: DataTypes.STRING });
  25. Table1.belongsToMany(Table2, { through: 'table1_to_table2' });
  26. Table2.belongsToMany(Table1, { through: 'table1_to_table2' });
  27. await Table1.sync({ force: true });
  28. await Table2.sync({ force: true });
  29. });
  30. it('should not use only a specified name', function () {
  31. expect(this.sequelize.models.get('ms_table1sms_table2s')).not.to.exist;
  32. expect(this.sequelize.models.get('table1_to_table2')).to.exist;
  33. });
  34. });
  35. });
  36. describe('HasMany', () => {
  37. beforeEach(async function () {
  38. // prevent periods from occurring in the table name since they are used to delimit (table.column)
  39. this.User = this.sequelize.define(`User${Math.ceil(Math.random() * 10_000_000)}`, {
  40. name: DataTypes.STRING,
  41. });
  42. this.Task = this.sequelize.define(`Task${Math.ceil(Math.random() * 10_000_000)}`, {
  43. name: DataTypes.STRING,
  44. });
  45. this.users = null;
  46. this.tasks = null;
  47. this.User.belongsToMany(this.Task, { as: 'Tasks', through: 'UserTasks' });
  48. this.Task.belongsToMany(this.User, { as: 'Users', through: 'UserTasks' });
  49. const users = [];
  50. const tasks = [];
  51. for (let i = 0; i < 5; ++i) {
  52. users[i] = { name: `User${Math.random()}` };
  53. tasks[i] = { name: `Task${Math.random()}` };
  54. }
  55. await this.sequelize.sync({ force: true });
  56. await this.User.bulkCreate(users);
  57. await this.Task.bulkCreate(tasks);
  58. });
  59. describe('addDAO / getModel', () => {
  60. beforeEach(async function () {
  61. this.user = null;
  62. this.task = null;
  63. const _users = await this.User.findAll();
  64. const _tasks = await this.Task.findAll();
  65. this.user = _users[0];
  66. this.task = _tasks[0];
  67. });
  68. it('should correctly add an association to the dao', async function () {
  69. expect(await this.user.getTasks()).to.have.length(0);
  70. await this.user.addTask(this.task);
  71. expect(await this.user.getTasks()).to.have.length(1);
  72. });
  73. });
  74. describe('removeDAO', () => {
  75. beforeEach(async function () {
  76. this.user = null;
  77. this.tasks = null;
  78. const _users = await this.User.findAll();
  79. const _tasks = await this.Task.findAll();
  80. this.user = _users[0];
  81. this.tasks = _tasks;
  82. });
  83. it('should correctly remove associated objects', async function () {
  84. expect(await this.user.getTasks()).to.have.length(0);
  85. await this.user.setTasks(this.tasks);
  86. expect(await this.user.getTasks()).to.have.length(this.tasks.length);
  87. await this.user.removeTask(this.tasks[0]);
  88. expect(await this.user.getTasks()).to.have.length(this.tasks.length - 1);
  89. await this.user.removeTasks([this.tasks[1], this.tasks[2]]);
  90. expect(await this.user.getTasks()).to.have.length(this.tasks.length - 3);
  91. });
  92. });
  93. });
  94. });
  95. }