self.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes } = require('@sequelize/core');
  6. describe(Support.getTestDialectTeaser('Self'), () => {
  7. it('supports freezeTableName', async function () {
  8. const Group = this.sequelize.define(
  9. 'Group',
  10. {},
  11. {
  12. tableName: 'user_group',
  13. timestamps: false,
  14. underscored: true,
  15. freezeTableName: true,
  16. },
  17. );
  18. Group.belongsTo(Group, { as: 'Parent', foreignKey: 'parent_id' });
  19. await Group.sync({ force: true });
  20. await Group.findAll({
  21. include: [
  22. {
  23. model: Group,
  24. as: 'Parent',
  25. },
  26. ],
  27. });
  28. });
  29. it('can handle 1:m associations', async function () {
  30. const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
  31. Person.hasMany(Person, { as: 'children', foreignKey: 'parent_id', inverse: { as: 'parent' } });
  32. expect(Person.getAttributes().parent_id).to.be.ok;
  33. await this.sequelize.sync({ force: true });
  34. const [mary, john, chris] = await Promise.all([
  35. Person.create({ name: 'Mary' }),
  36. Person.create({ name: 'John' }),
  37. Person.create({ name: 'Chris' }),
  38. ]);
  39. await mary.setChildren([john, chris]);
  40. });
  41. it('can handle n:m associations', async function () {
  42. const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
  43. Person.belongsToMany(Person, {
  44. as: 'Parents',
  45. through: 'Family',
  46. foreignKey: 'ChildId',
  47. otherKey: 'PersonId',
  48. inverse: { as: 'Childs' },
  49. });
  50. expect(Person.associations.Parents.otherKey).to.eq('PersonId');
  51. expect(Person.associations.Childs.otherKey).to.eq('ChildId');
  52. const rawAttributes = Object.keys(this.sequelize.models.get('Family').getAttributes());
  53. expect(rawAttributes).to.have.members(['createdAt', 'updatedAt', 'PersonId', 'ChildId']);
  54. expect(rawAttributes.length).to.equal(4);
  55. await this.sequelize.sync({ force: true });
  56. const [mary, john, chris] = await Promise.all([
  57. Person.create({ name: 'Mary' }),
  58. Person.create({ name: 'John' }),
  59. Person.create({ name: 'Chris' }),
  60. ]);
  61. await mary.setParents([john]);
  62. await chris.addParent(john);
  63. const children = await john.getChilds();
  64. expect(children.map(v => v.id)).to.have.members([mary.id, chris.id]);
  65. });
  66. it('can handle n:m associations with pre-defined through table', async function () {
  67. const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
  68. const Family = this.sequelize.define(
  69. 'Family',
  70. {
  71. preexisting_child: {
  72. type: DataTypes.INTEGER,
  73. primaryKey: true,
  74. },
  75. preexisting_parent: {
  76. type: DataTypes.INTEGER,
  77. primaryKey: true,
  78. },
  79. },
  80. { timestamps: false },
  81. );
  82. Person.belongsToMany(Person, {
  83. as: 'Parents',
  84. through: Family,
  85. foreignKey: 'preexisting_child',
  86. otherKey: 'preexisting_parent',
  87. inverse: { as: 'Children' },
  88. });
  89. expect(Person.associations.Parents.otherKey).to.eq('preexisting_parent');
  90. expect(Person.associations.Children.otherKey).to.eq('preexisting_child');
  91. const rawAttributes = Object.keys(Family.getAttributes());
  92. expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']);
  93. expect(rawAttributes.length).to.equal(2);
  94. let count = 0;
  95. await this.sequelize.sync({ force: true });
  96. const [mary, john, chris] = await Promise.all([
  97. Person.create({ name: 'Mary' }),
  98. Person.create({ name: 'John' }),
  99. Person.create({ name: 'Chris' }),
  100. ]);
  101. this.mary = mary;
  102. this.chris = chris;
  103. this.john = john;
  104. await mary.setParents([john], {
  105. logging(sql) {
  106. if (/INSERT/.test(sql)) {
  107. count++;
  108. expect(sql).to.have.string('preexisting_child');
  109. expect(sql).to.have.string('preexisting_parent');
  110. }
  111. },
  112. });
  113. await this.mary.addParent(this.chris, {
  114. logging(sql) {
  115. if (/INSERT/.test(sql)) {
  116. count++;
  117. expect(sql).to.have.string('preexisting_child');
  118. expect(sql).to.have.string('preexisting_parent');
  119. }
  120. },
  121. });
  122. const children = await this.john.getChildren({
  123. logging(sql) {
  124. count++;
  125. const whereClause = sql.split('FROM')[1];
  126. // look only in the whereClause
  127. expect(whereClause).to.have.string('preexisting_child');
  128. expect(whereClause).to.have.string('preexisting_parent');
  129. },
  130. });
  131. expect(count).to.equal(3);
  132. expect(children.map(v => v.id)).to.have.members([this.mary.id]);
  133. });
  134. });