counter-cache.test.js
3.13 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
/* jshint camelcase: false, expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, Sequelize = require('../../index')
, Promise = Sequelize.Promise
, assert = require('assert');
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser("CounterCache"), function() {
it('adds an integer column', function() {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
User.hasMany(Group, { counterCache: true });
expect(Object.keys(User.attributes)).to.contain('countGroups');
expect(User.attributes.countGroups.type).to.equal(DataTypes.INTEGER);
});
it('supports `as`', function() {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
User.hasMany(Group, { counterCache: { as: 'countDemGroups' } });
expect(Object.keys(User.attributes)).to.contain('countDemGroups');
});
it('inits at 0', function() {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
User.hasMany(Group, { counterCache: true });
return this.sequelize.sync({ force: true }).then(function () {
return User.create();
}).then(function (user) {
expect(user.countGroups).to.equal(0);
});
});
describe('hooks', function () {
var User, Group;
beforeEach(function() {
User = this.sequelize.define('User', {});
Group = this.sequelize.define('Group', {});
User.hasMany(Group, { counterCache: true });
return this.sequelize.sync({ force: true });
});
it('increments', function() {
return User.create().then(function (user) {
expect(user.countGroups).to.equal(0);
return user.createGroup().return(user);
}).then(function (user) {
return User.find(user.id);
}).then(function (user) {
expect(user.countGroups).to.equal(1);
});
});
it('decrements', function() {
var user;
return User.create().then(function (tmpUser) {
user = tmpUser;
return user.createGroup();
}).then(function (group) {
return group.destroy();
}).then(function () {
return user.reload();
}).then(function () {
expect(user.countGroups).to.equal(0);
});
});
it('works on update', function () {
var user, otherUser;
return User.create().then(function (tmpUser) {
otherUser = tmpUser;
return User.create();
}).then(function (tmpUser) {
user = tmpUser;
return user.createGroup();
}).tap(function (group) {
return user.reload();
}).tap(function () {
expect(user.countGroups).to.equal(1);
}).then(function (group) {
group.UserId = otherUser.id;
return group.save();
}).then(function () {
return Promise.all([user.reload(), otherUser.reload()]);
}).then(function () {
expect(user.countGroups).to.equal(0);
expect(otherUser.countGroups).to.equal(1);
});
});
});
})