alias.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. describe(Support.getTestDialectTeaser('Alias'), () => {
  6. it('should uppercase the first letter in alias getter, but not in eager loading', async function () {
  7. const User = this.sequelize.define('user', {});
  8. const Task = this.sequelize.define('task', {});
  9. User.hasMany(Task, { as: 'assignments', foreignKey: 'userId' });
  10. Task.belongsTo(User, { as: 'owner', foreignKey: 'userId' });
  11. await this.sequelize.sync({ force: true });
  12. const user0 = await User.create({ id: 1 });
  13. expect(user0.getAssignments).to.be.ok;
  14. const task0 = await Task.create({ id: 1, userId: 1 });
  15. expect(task0.getOwner).to.be.ok;
  16. const [user, task] = await Promise.all([
  17. User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }),
  18. Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] }),
  19. ]);
  20. expect(user.assignments).to.be.ok;
  21. expect(task.owner).to.be.ok;
  22. });
  23. it('shouldnt touch the passed alias', async function () {
  24. const User = this.sequelize.define('user', {});
  25. const Task = this.sequelize.define('task', {});
  26. User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' });
  27. Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' });
  28. await this.sequelize.sync({ force: true });
  29. const user0 = await User.create({ id: 1 });
  30. expect(user0.getASSIGNMENTS).to.be.ok;
  31. const task0 = await Task.create({ id: 1, userId: 1 });
  32. expect(task0.getOWNER).to.be.ok;
  33. const [user, task] = await Promise.all([
  34. User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }),
  35. Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] }),
  36. ]);
  37. expect(user.ASSIGNMENTS).to.be.ok;
  38. expect(task.OWNER).to.be.ok;
  39. });
  40. it('should allow me to pass my own plural and singular forms to hasMany', async function () {
  41. const User = this.sequelize.define('user', {});
  42. const Task = this.sequelize.define('task', {});
  43. User.hasMany(Task, { as: { singular: 'task', plural: 'taskz' } });
  44. await this.sequelize.sync({ force: true });
  45. const user0 = await User.create({ id: 1 });
  46. expect(user0.getTaskz).to.be.ok;
  47. expect(user0.addTask).to.be.ok;
  48. expect(user0.addTaskz).to.be.ok;
  49. const user = await User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
  50. expect(user.taskz).to.be.ok;
  51. });
  52. it('should allow me to define plural and singular forms on the model', async function () {
  53. const User = this.sequelize.define('user', {});
  54. const Task = this.sequelize.define(
  55. 'task',
  56. {},
  57. {
  58. name: {
  59. singular: 'assignment',
  60. plural: 'assignments',
  61. },
  62. },
  63. );
  64. User.hasMany(Task);
  65. await this.sequelize.sync({ force: true });
  66. const user0 = await User.create({ id: 1 });
  67. expect(user0.getAssignments).to.be.ok;
  68. expect(user0.addAssignment).to.be.ok;
  69. expect(user0.addAssignments).to.be.ok;
  70. const user = await User.findOne({ where: { id: 1 }, include: [Task] });
  71. expect(user.assignments).to.be.ok;
  72. });
  73. });