forked from sequelize/sequelize
/
get-attributes.test.js
105 lines (93 loc) · 2.91 KB
/
get-attributes.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../support');
const current = Support.sequelize;
const _ = require('lodash');
const DataTypes = require('sequelize/lib/data-types');
function assertDataType(property, dataType) {
expect(property.type.constructor.key).to.equal(dataType.key);
}
describe(Support.getTestDialectTeaser('Model'), () => {
describe('getAttributes', () => {
it('should return attributes with getAttributes()', () => {
const Model = current.define(
'User',
{ username: DataTypes.STRING },
{ timestamps: false }
);
const attributes = Model.getAttributes();
expect(Object.keys(attributes)).to.eql(['id', 'username']);
// Type must be casted or it will cause circular references errors
assertDataType(attributes.id, DataTypes.INTEGER);
assertDataType(attributes.username, DataTypes.STRING);
expect(attributes.id).to.include({
Model,
allowNull: false,
primaryKey: true,
autoIncrement: true,
_autoGenerated: true,
fieldName: 'id',
_modelAttribute: true,
field: 'id'
});
expect(attributes.username).to.include({
Model,
fieldName: 'username',
_modelAttribute: true,
field: 'username'
});
});
it('will contain timestamps if enabled', () => {
const Model = current.define('User', { username: DataTypes.STRING });
const attributes = Model.getAttributes();
expect(Object.keys(attributes)).to.include.members([
'createdAt',
'updatedAt'
]);
// Type must be casted or it will cause circular references errors
assertDataType(attributes.createdAt, DataTypes.DATE);
assertDataType(attributes.updatedAt, DataTypes.DATE);
expect(attributes.createdAt).to.include({
allowNull: false,
_autoGenerated: true,
Model,
fieldName: 'createdAt',
_modelAttribute: true,
field: 'createdAt'
});
expect(attributes.updatedAt).to.include({
allowNull: false,
_autoGenerated: true,
Model,
fieldName: 'updatedAt',
_modelAttribute: true,
field: 'updatedAt'
});
});
it('will contain timestamps if enabled', () => {
const Model = current.define(
'User',
{
username: DataTypes.STRING,
virtual: {
type: DataTypes.VIRTUAL,
get() {
return 1;
}
}
},
{ timestamps: false }
);
const attributes = Model.getAttributes();
expect(Object.keys(attributes)).to.include.members(['virtual']);
assertDataType(attributes.virtual, DataTypes.VIRTUAL);
expect(attributes.virtual).to.include({
Model,
fieldName: 'virtual',
_modelAttribute: true,
field: 'virtual'
});
});
});
});