underscored.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('Model'), () => {
  7. describe('options.underscored', () => {
  8. beforeEach(function () {
  9. this.N = this.sequelize.define(
  10. 'N',
  11. {
  12. id: {
  13. type: DataTypes.STRING(10),
  14. primaryKey: true,
  15. field: 'n_id',
  16. },
  17. },
  18. {
  19. underscored: true,
  20. },
  21. );
  22. this.M = this.sequelize.define(
  23. 'M',
  24. {
  25. id: {
  26. type: DataTypes.STRING(20),
  27. primaryKey: true,
  28. field: 'm_id',
  29. },
  30. },
  31. {
  32. underscored: true,
  33. },
  34. );
  35. this.NM = this.sequelize.define('NM', {});
  36. });
  37. it('should properly set field when defining', function () {
  38. expect(this.N.getAttributes().id.field).to.equal('n_id');
  39. expect(this.M.getAttributes().id.field).to.equal('m_id');
  40. });
  41. it('hasOne does not override already defined field', function () {
  42. this.N.modelDefinition.rawAttributes.mId = {
  43. type: DataTypes.STRING(20),
  44. field: 'n_m_id',
  45. };
  46. this.N.modelDefinition.refreshAttributes();
  47. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  48. this.M.hasOne(this.N, { foreignKey: 'mId' });
  49. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  50. });
  51. it('belongsTo does not override already defined field', function () {
  52. this.N.modelDefinition.rawAttributes.mId = {
  53. type: DataTypes.STRING(20),
  54. field: 'n_m_id',
  55. };
  56. this.N.modelDefinition.refreshAttributes();
  57. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  58. this.N.belongsTo(this.M, { foreignKey: 'mId' });
  59. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  60. });
  61. it('hasOne/belongsTo does not override already defined field', function () {
  62. this.N.modelDefinition.rawAttributes.mId = {
  63. type: DataTypes.STRING(20),
  64. field: 'n_m_id',
  65. };
  66. this.N.modelDefinition.refreshAttributes();
  67. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  68. this.N.belongsTo(this.M, { foreignKey: 'mId' });
  69. this.M.hasOne(this.N, { foreignKey: 'mId' });
  70. expect(this.N.getAttributes().mId.field).to.equal('n_m_id');
  71. });
  72. it('hasMany does not override already defined field', function () {
  73. this.M.modelDefinition.rawAttributes.nId = {
  74. type: DataTypes.STRING(20),
  75. field: 'nana_id',
  76. };
  77. this.M.modelDefinition.refreshAttributes();
  78. expect(this.M.getAttributes().nId.field).to.equal('nana_id');
  79. this.N.hasMany(this.M, { foreignKey: 'nId' });
  80. this.M.belongsTo(this.N, { foreignKey: 'nId' });
  81. expect(this.M.getAttributes().nId.field).to.equal('nana_id');
  82. });
  83. it('belongsToMany does not override already defined field', function () {
  84. this.NM = this.sequelize.define(
  85. 'NM',
  86. {
  87. n_id: {
  88. type: DataTypes.STRING(10),
  89. field: 'nana_id',
  90. },
  91. m_id: {
  92. type: DataTypes.STRING(20),
  93. field: 'mama_id',
  94. },
  95. },
  96. {
  97. underscored: true,
  98. },
  99. );
  100. this.N.belongsToMany(this.M, { through: this.NM, foreignKey: 'n_id', otherKey: 'm_id' });
  101. expect(this.NM.getAttributes().n_id.field).to.equal('nana_id');
  102. expect(this.NM.getAttributes().m_id.field).to.equal('mama_id');
  103. });
  104. });
  105. });