has-one.test.js
1.58 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
_ = require('lodash'),
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
current = Support.sequelize;
describe(Support.getTestDialectTeaser('hasOne'), () => {
it('throws when invalid model is passed', () => {
const User = current.define('User');
expect(() => {
User.hasOne();
}).to.throw('User.hasOne called with something that\'s not a subclass of Sequelize.Model');
});
it('properly use the `as` key to generate foreign key name', () => {
const User = current.define('User', { username: DataTypes.STRING }),
Task = current.define('Task', { title: DataTypes.STRING });
User.hasOne(Task);
expect(Task.rawAttributes.UserId).not.to.be.empty;
User.hasOne(Task, {as: 'Shabda'});
expect(Task.rawAttributes.ShabdaId).not.to.be.empty;
});
it('should not override custom methods with association mixin', () => {
const methods = {
getTask: 'get',
setTask: 'set',
createTask: 'create'
};
const User = current.define('User');
const Task = current.define('Task');
_.each(methods, (alias, method) => {
User.prototype[method] = function() {
const realMethod = this.constructor.associations.task[alias];
expect(realMethod).to.be.a('function');
return realMethod;
};
});
User.hasOne(Task, { as: 'task' });
const user = User.build();
_.each(methods, (alias, method) => {
expect(user[method]()).to.be.a('function');
});
});
});