attributes.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. const chai = require('chai');
  3. const { DataTypes } = require('@sequelize/core');
  4. const expect = chai.expect;
  5. const Support = require('../support');
  6. describe(Support.getTestDialectTeaser('Model'), () => {
  7. describe('attributes', () => {
  8. describe('set', () => {
  9. it('should only be called once when used on a join model called with an association getter', async function () {
  10. let callCount = 0;
  11. this.Student = this.sequelize.define(
  12. 'student',
  13. {
  14. no: { type: DataTypes.INTEGER, primaryKey: true },
  15. name: DataTypes.STRING,
  16. },
  17. {
  18. tableName: 'student',
  19. timestamps: false,
  20. },
  21. );
  22. this.Course = this.sequelize.define(
  23. 'course',
  24. {
  25. no: { type: DataTypes.INTEGER, primaryKey: true },
  26. name: DataTypes.STRING,
  27. },
  28. {
  29. tableName: 'course',
  30. timestamps: false,
  31. },
  32. );
  33. this.Score = this.sequelize.define(
  34. 'score',
  35. {
  36. score: DataTypes.INTEGER,
  37. test_value: {
  38. type: DataTypes.INTEGER,
  39. set(v) {
  40. callCount++;
  41. this.setDataValue('test_value', v + 1);
  42. },
  43. },
  44. },
  45. {
  46. tableName: 'score',
  47. timestamps: false,
  48. },
  49. );
  50. this.Student.belongsToMany(this.Course, {
  51. through: this.Score,
  52. foreignKey: 'StudentId',
  53. otherKey: 'CourseId',
  54. });
  55. await this.sequelize.sync({ force: true });
  56. const [student, course] = await Promise.all([
  57. this.Student.create({ no: 1, name: 'ryan' }),
  58. this.Course.create({ no: 100, name: 'history' }),
  59. ]);
  60. await student.addCourse(course, { through: { score: 98, test_value: 1000 } });
  61. expect(callCount).to.equal(1);
  62. const score0 = await this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } });
  63. expect(score0.test_value).to.equal(1001);
  64. const [courses, score] = await Promise.all([
  65. this.Student.build({ no: 1 }).getCourses({ where: { no: 100 } }),
  66. this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } }),
  67. ]);
  68. expect(score.test_value).to.equal(1001);
  69. expect(courses[0].studentCourse.toJSON().test_value).to.equal(1001);
  70. expect(callCount).to.equal(1);
  71. });
  72. it('allows for an attribute to be called "toString"', async function () {
  73. const Person = this.sequelize.define(
  74. 'person',
  75. {
  76. name: DataTypes.STRING,
  77. nick: DataTypes.STRING,
  78. },
  79. {
  80. timestamps: false,
  81. },
  82. );
  83. await this.sequelize.sync({ force: true });
  84. await Person.create({ name: 'Jozef', nick: 'Joe' });
  85. const person = await Person.findOne({
  86. attributes: ['nick', ['name', 'toString']],
  87. where: {
  88. name: 'Jozef',
  89. },
  90. });
  91. expect(person.dataValues.toString).to.equal('Jozef');
  92. expect(person.get('toString')).to.equal('Jozef');
  93. });
  94. it('allows for an attribute to be called "toString" with associations', async function () {
  95. const Person = this.sequelize.define('person', {
  96. name: DataTypes.STRING,
  97. nick: DataTypes.STRING,
  98. });
  99. const Computer = this.sequelize.define('computer', {
  100. hostname: DataTypes.STRING,
  101. });
  102. Person.hasMany(Computer);
  103. await this.sequelize.sync({ force: true });
  104. const person = await Person.create({ name: 'Jozef', nick: 'Joe' });
  105. await person.createComputer({ hostname: 'laptop' });
  106. const result = await Person.findAll({
  107. attributes: ['nick', ['name', 'toString']],
  108. include: {
  109. model: Computer,
  110. },
  111. where: {
  112. name: 'Jozef',
  113. },
  114. });
  115. expect(result.length).to.equal(1);
  116. expect(result[0].dataValues.toString).to.equal('Jozef');
  117. expect(result[0].get('toString')).to.equal('Jozef');
  118. expect(result[0].get('computers')[0].hostname).to.equal('laptop');
  119. });
  120. });
  121. });
  122. });