get-attributes.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import type { DataType, NormalizedAttributeOptions } from '@sequelize/core';
  2. import { DataTypes } from '@sequelize/core';
  3. import { expect } from 'chai';
  4. import { getTestDialectTeaser, sequelize } from '../../support';
  5. function assertDataType(property: NormalizedAttributeOptions, dataType: DataType) {
  6. expect(property.type).to.be.instanceof(dataType);
  7. }
  8. describe(getTestDialectTeaser('Model'), () => {
  9. describe('getAttributes', () => {
  10. it(`returns the model's attributes`, () => {
  11. const Model = sequelize.define('User', { username: DataTypes.STRING }, { timestamps: false });
  12. const attributes = Model.getAttributes();
  13. expect(Object.keys(attributes)).to.eql(['id', 'username']);
  14. // Type must be casted or it will cause circular references errors
  15. assertDataType(attributes.id, DataTypes.INTEGER);
  16. assertDataType(attributes.username, DataTypes.STRING);
  17. expect(attributes.id).to.include({
  18. Model,
  19. allowNull: false,
  20. primaryKey: true,
  21. autoIncrement: true,
  22. _autoGenerated: true,
  23. fieldName: 'id',
  24. _modelAttribute: true,
  25. field: 'id',
  26. });
  27. expect(attributes.username).to.include({
  28. Model,
  29. fieldName: 'username',
  30. _modelAttribute: true,
  31. field: 'username',
  32. });
  33. });
  34. it('contains timestamps if enabled', () => {
  35. const Model = sequelize.define('User', { username: DataTypes.STRING });
  36. const attributes = Model.getAttributes();
  37. expect(Object.keys(attributes)).to.include.members(['createdAt', 'updatedAt']);
  38. // Type must be casted or it will cause circular references errors
  39. assertDataType(attributes.createdAt, DataTypes.DATE);
  40. assertDataType(attributes.updatedAt, DataTypes.DATE);
  41. expect(attributes.createdAt).to.include({
  42. allowNull: false,
  43. _autoGenerated: true,
  44. Model,
  45. fieldName: 'createdAt',
  46. _modelAttribute: true,
  47. field: 'createdAt',
  48. });
  49. expect(attributes.updatedAt).to.include({
  50. allowNull: false,
  51. _autoGenerated: true,
  52. Model,
  53. fieldName: 'updatedAt',
  54. _modelAttribute: true,
  55. field: 'updatedAt',
  56. });
  57. });
  58. it('contains virtual attributes', () => {
  59. const Model = sequelize.define(
  60. 'User',
  61. {
  62. username: DataTypes.STRING,
  63. virtual: {
  64. type: DataTypes.VIRTUAL,
  65. get() {
  66. return 1;
  67. },
  68. },
  69. },
  70. { timestamps: false },
  71. );
  72. const attributes = Model.getAttributes();
  73. expect(Object.keys(attributes)).to.include.members(['virtual']);
  74. assertDataType(attributes.virtual, DataTypes.VIRTUAL);
  75. expect(attributes.virtual).to.include({
  76. Model,
  77. fieldName: 'virtual',
  78. _modelAttribute: true,
  79. field: 'virtual',
  80. });
  81. });
  82. });
  83. });