underscored.test.js
3.34 KB
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
106
107
108
109
110
111
112
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types'),
Sequelize = require('../../../index');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('options.underscored', () => {
beforeEach(function() {
this.N = this.sequelize.define('N', {
id: {
type: DataTypes.CHAR(10),
primaryKey: true,
field: 'n_id'
}
}, {
underscored: true
});
this.M = this.sequelize.define('M', {
id: {
type: Sequelize.CHAR(20),
primaryKey: true,
field: 'm_id'
}
}, {
underscored: true
});
this.NM = this.sequelize.define('NM', {});
});
it('should properly set field when defining', function() {
expect(this.N.rawAttributes['id'].field).to.equal('n_id');
expect(this.M.rawAttributes['id'].field).to.equal('m_id');
});
it('hasOne does not override already defined field', function() {
this.N.rawAttributes['mId'] = {
type: Sequelize.CHAR(20),
field: 'n_m_id'
};
this.N.refreshAttributes();
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
this.M.hasOne(this.N, { foreignKey: 'mId' });
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
});
it('belongsTo does not override already defined field', function() {
this.N.rawAttributes['mId'] = {
type: Sequelize.CHAR(20),
field: 'n_m_id'
};
this.N.refreshAttributes();
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
this.N.belongsTo(this.M, { foreignKey: 'mId' });
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
});
it('hasOne/belongsTo does not override already defined field', function() {
this.N.rawAttributes['mId'] = {
type: Sequelize.CHAR(20),
field: 'n_m_id'
};
this.N.refreshAttributes();
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
this.N.belongsTo(this.M, { foreignKey: 'mId' });
this.M.hasOne(this.N, { foreignKey: 'mId' });
expect(this.N.rawAttributes['mId'].field).to.equal('n_m_id');
});
it('hasMany does not override already defined field', function() {
this.M.rawAttributes['nId'] = {
type: Sequelize.CHAR(20),
field: 'nana_id'
};
this.M.refreshAttributes();
expect(this.M.rawAttributes['nId'].field).to.equal('nana_id');
this.N.hasMany(this.M, { foreignKey: 'nId' });
this.M.belongsTo(this.N, { foreignKey: 'nId' });
expect(this.M.rawAttributes['nId'].field).to.equal('nana_id');
});
it('belongsToMany does not override already defined field', function() {
this.NM = this.sequelize.define('NM', {
n_id: {
type: Sequelize.CHAR(10),
field: 'nana_id'
},
m_id: {
type: Sequelize.CHAR(20),
field: 'mama_id'
}
}, {
underscored: true
});
this.N.belongsToMany(this.M, { through: this.NM, foreignKey: 'n_id' });
this.M.belongsToMany(this.N, { through: this.NM, foreignKey: 'm_id' });
expect(this.NM.rawAttributes['n_id'].field).to.equal('nana_id');
expect(this.NM.rawAttributes['m_id'].field).to.equal('mama_id');
});
});
});