associations.test.js 4.8 KB

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