build.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const { DataTypes, sql } = require('@sequelize/core');
  6. const current = Support.sequelize;
  7. const dialect = current.dialect;
  8. describe(Support.getTestDialectTeaser('Instance'), () => {
  9. describe('build', () => {
  10. it('should populate NOW default values', async () => {
  11. const Model = current.define(
  12. 'Model',
  13. {
  14. created_time: {
  15. type: DataTypes.DATE,
  16. allowNull: true,
  17. defaultValue: DataTypes.NOW,
  18. },
  19. updated_time: {
  20. type: DataTypes.DATE,
  21. allowNull: true,
  22. defaultValue: DataTypes.NOW,
  23. },
  24. ip: {
  25. type: DataTypes.STRING,
  26. validate: {
  27. isIP: true,
  28. },
  29. },
  30. ip2: {
  31. type: DataTypes.STRING,
  32. validate: {
  33. isIP: {
  34. msg: 'test',
  35. },
  36. },
  37. },
  38. },
  39. {
  40. timestamp: false,
  41. },
  42. );
  43. const instance = Model.build({ ip: '127.0.0.1', ip2: '0.0.0.0' });
  44. expect(instance.get('created_time')).to.be.an.instanceof(
  45. Date,
  46. 'created_time should be a date',
  47. );
  48. expect(instance.get('updated_time')).to.be.an.instanceof(
  49. Date,
  50. 'updated_time should be a date',
  51. );
  52. await instance.validate();
  53. });
  54. it('should populate explicitly undefined UUID primary keys', () => {
  55. const Model = current.define('Model', {
  56. id: {
  57. type: DataTypes.UUID,
  58. primaryKey: true,
  59. allowNull: false,
  60. defaultValue: sql.uuidV4,
  61. },
  62. });
  63. const instance = Model.build({
  64. id: undefined,
  65. });
  66. expect(instance.get('id')).not.to.be.undefined;
  67. expect(instance.get('id')).to.be.ok;
  68. });
  69. it('should populate undefined columns with default value', () => {
  70. const Model = current.define('Model', {
  71. number1: {
  72. type: DataTypes.INTEGER,
  73. defaultValue: 1,
  74. },
  75. number2: {
  76. type: DataTypes.INTEGER,
  77. defaultValue: 2,
  78. },
  79. });
  80. const instance = Model.build({
  81. number1: undefined,
  82. });
  83. expect(instance.get('number1')).not.to.be.undefined;
  84. expect(instance.get('number1')).to.equal(1);
  85. expect(instance.get('number2')).not.to.be.undefined;
  86. expect(instance.get('number2')).to.equal(2);
  87. });
  88. if (dialect.supports.dataTypes.JSONB) {
  89. it('should clone the default values', () => {
  90. const Model = current.define('Model', {
  91. data: {
  92. type: DataTypes.JSONB,
  93. defaultValue: { foo: 'bar' },
  94. },
  95. });
  96. const instance = Model.build();
  97. instance.data.foo = 'biz';
  98. expect(instance.get('data')).to.eql({ foo: 'biz' });
  99. expect(Model.build().get('data')).to.eql({ foo: 'bar' });
  100. });
  101. }
  102. });
  103. });