dont-modify-options.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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('associations'), () => {
  7. describe('Test options.foreignKey', () => {
  8. beforeEach(function () {
  9. this.A = this.sequelize.define('A', {
  10. id: {
  11. type: DataTypes.STRING(20),
  12. primaryKey: true,
  13. },
  14. });
  15. this.B = this.sequelize.define('B', {
  16. id: {
  17. type: DataTypes.STRING(20),
  18. primaryKey: true,
  19. },
  20. });
  21. this.C = this.sequelize.define('C', {});
  22. });
  23. it('should not be overwritten for belongsTo', function () {
  24. const reqValidForeignKey = { foreignKey: { allowNull: false } };
  25. this.A.belongsTo(this.B, reqValidForeignKey);
  26. this.A.belongsTo(this.C, reqValidForeignKey);
  27. expect(this.A.getAttributes().cId.type instanceof this.C.getAttributes().id.type.constructor);
  28. });
  29. it('should not be overwritten for belongsToMany', function () {
  30. const reqValidForeignKey = { foreignKey: { allowNull: false }, through: 'ABBridge' };
  31. this.B.belongsToMany(this.A, reqValidForeignKey);
  32. this.A.belongsTo(this.C, reqValidForeignKey);
  33. expect(this.A.getAttributes().cId.type instanceof this.C.getAttributes().id.type.constructor);
  34. });
  35. it('should not be overwritten for hasOne', function () {
  36. const reqValidForeignKey = { foreignKey: { allowNull: false } };
  37. this.B.hasOne(this.A, reqValidForeignKey);
  38. this.A.belongsTo(this.C, reqValidForeignKey);
  39. expect(this.A.getAttributes().cId.type instanceof this.C.getAttributes().id.type.constructor);
  40. });
  41. it('should not be overwritten for hasMany', function () {
  42. const reqValidForeignKey = { foreignKey: { allowNull: false } };
  43. this.B.hasMany(this.A, reqValidForeignKey);
  44. this.A.belongsTo(this.C, reqValidForeignKey);
  45. expect(this.A.getAttributes().cId.type instanceof this.C.getAttributes().id.type.constructor);
  46. });
  47. });
  48. });