to-json.test.js
6.66 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Instance'), () => {
describe('toJSON', () => {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: { type: DataTypes.STRING },
age: DataTypes.INTEGER,
level: { type: DataTypes.INTEGER },
isUser: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
isAdmin: { type: DataTypes.BOOLEAN }
}, {
timestamps: false
});
this.Project = this.sequelize.define('NiceProject', { title: DataTypes.STRING }, { timestamps: false });
this.User.hasMany(this.Project, { as: 'Projects', foreignKey: 'lovelyUserId' });
this.Project.belongsTo(this.User, { as: 'LovelyUser', foreignKey: 'lovelyUserId' });
await this.User.sync({ force: true });
await this.Project.sync({ force: true });
});
it("doesn't return instance that isn't defined", async function() {
const project0 = await this.Project.create({ lovelyUserId: null });
const project = await this.Project.findOne({
where: {
id: project0.id
},
include: [
{ model: this.User, as: 'LovelyUser' }
]
});
const json = project.toJSON();
expect(json.LovelyUser).to.be.equal(null);
});
it("doesn't return instances that aren't defined", async function() {
const user0 = await this.User.create({ username: 'cuss' });
const user = await this.User.findOne({
where: {
id: user0.id
},
include: [
{ model: this.Project, as: 'Projects' }
]
});
expect(user.Projects).to.be.instanceof(Array);
expect(user.Projects).to.be.length(0);
});
describe('build', () => {
it('returns an object containing all values', function() {
const user = this.User.build({
username: 'Adam',
age: 22,
level: -1,
isUser: false,
isAdmin: true
});
expect(user.toJSON()).to.deep.equal({
id: null,
username: 'Adam',
age: 22,
level: -1,
isUser: false,
isAdmin: true
});
});
it('returns a response that can be stringified', function() {
const user = this.User.build({
username: 'test.user',
age: 99,
isAdmin: true,
isUser: false
});
expect(JSON.stringify(user)).to.deep.equal('{"id":null,"username":"test.user","age":99,"isAdmin":true,"isUser":false}');
});
it('returns a response that can be stringified and then parsed', function() {
const user = this.User.build({ username: 'test.user', age: 99, isAdmin: true });
expect(JSON.parse(JSON.stringify(user))).to.deep.equal({ username: 'test.user', age: 99, isAdmin: true, isUser: false, id: null });
});
});
describe('create', () => {
it('returns an object containing all values', async function() {
const user = await this.User.create({
username: 'Adam',
age: 22,
level: -1,
isUser: false,
isAdmin: true
});
expect(user.toJSON()).to.deep.equal({
id: user.get('id'),
username: 'Adam',
age: 22,
isUser: false,
isAdmin: true,
level: -1
});
});
it('returns a response that can be stringified', async function() {
const user = await this.User.create({
username: 'test.user',
age: 99,
isAdmin: true,
isUser: false,
level: null
});
expect(JSON.stringify(user)).to.deep.equal(`{"id":${user.get('id')},"username":"test.user","age":99,"isAdmin":true,"isUser":false,"level":null}`);
});
it('returns a response that can be stringified and then parsed', async function() {
const user = await this.User.create({
username: 'test.user',
age: 99,
isAdmin: true,
level: null
});
expect(JSON.parse(JSON.stringify(user))).to.deep.equal({
age: 99,
id: user.get('id'),
isAdmin: true,
isUser: false,
level: null,
username: 'test.user'
});
});
});
describe('find', () => {
it('returns an object containing all values', async function() {
const user0 = await this.User.create({
username: 'Adam',
age: 22,
level: -1,
isUser: false,
isAdmin: true
});
const user = await this.User.findByPk(user0.get('id'));
expect(user.toJSON()).to.deep.equal({
id: user.get('id'),
username: 'Adam',
age: 22,
level: -1,
isUser: false,
isAdmin: true
});
});
it('returns a response that can be stringified', async function() {
const user0 = await this.User.create({
username: 'test.user',
age: 99,
isAdmin: true,
isUser: false
});
const user = await this.User.findByPk(user0.get('id'));
expect(JSON.stringify(user)).to.deep.equal(`{"id":${user.get('id')},"username":"test.user","age":99,"level":null,"isUser":false,"isAdmin":true}`);
});
it('returns a response that can be stringified and then parsed', async function() {
const user0 = await this.User.create({
username: 'test.user',
age: 99,
isAdmin: true
});
const user = await this.User.findByPk(user0.get('id'));
expect(JSON.parse(JSON.stringify(user))).to.deep.equal({
id: user.get('id'),
username: 'test.user',
age: 99,
isAdmin: true,
isUser: false,
level: null
});
});
});
it('includes the eagerly loaded associations', async function() {
const user = await this.User.create({ username: 'fnord', age: 1, isAdmin: true });
const project = await this.Project.create({ title: 'fnord' });
await user.setProjects([project]);
const users = await this.User.findAll({ include: [{ model: this.Project, as: 'Projects' }] });
const _user = users[0];
expect(_user.Projects).to.exist;
expect(JSON.parse(JSON.stringify(_user)).Projects).to.exist;
const projects = await this.Project.findAll({ include: [{ model: this.User, as: 'LovelyUser' }] });
const _project = projects[0];
expect(_project.LovelyUser).to.exist;
expect(JSON.parse(JSON.stringify(_project)).LovelyUser).to.exist;
});
});
});