This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
post.js
277 lines (222 loc) · 8.2 KB
/
post.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
This file is a part of libertysoil.org website
Copyright (C) 2015 Loki Education (Social Enterprise)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-env node, mocha */
/* global $dbConfig */
import expect from '../../../../test-helpers/expect';
import GeotagFactory from '../../../../test-helpers/factories/geotag';
import PostFactory from '../../../../test-helpers/factories/post';
import SchoolFactory from '../../../../test-helpers/factories/school';
import HashtagFactory from '../../../../test-helpers/factories/hashtag';
import initBookshelf from '../../../../src/api/db';
const bookshelf = initBookshelf($dbConfig);
const Geotag = bookshelf.model('Geotag');
const Post = bookshelf.model('Post');
const School = bookshelf.model('School');
const Hashtag = bookshelf.model('Hashtag');
describe('Post', () => {
let post, school;
before(async () => {
post = await new Post(PostFactory.build({})).save(null, {method: 'insert'});
school = await new School(SchoolFactory.build({updated_at: null})).save(null, {method: 'insert'});
});
after(async () => {
await post.destroy();
await school.destroy();
});
it('attachSchool updates school updated_at field', async () => {
await school.refresh(); // refrsesh from database
expect(school.get('updated_at'), 'to be null');
await post.attachSchools(school.get('name'));
await school.refresh();
expect(school.get('updated_at'), 'not to be null');
});
describe('geotags', () => {
let geotags = [];
let geotagIds = [];
before(async () => {
for (let i = 0; i < 2; ++i) {
let geotag = await new Geotag(GeotagFactory.build()).save(null, { method: 'insert' });
await geotag.refresh();
geotags.push(geotag);
geotagIds.push(geotag.id);
}
});
after(async () => {
for (let geotag of geotags) {
await geotag.destroy();
}
});
afterEach(async () => {
await post.geotags().detach(geotagIds);
for (let geotag of geotags) {
await geotag.save({ post_count: 0 });
}
});
describe('#attachGeotags', () => {
it('increments post_count', async () => {
expect(geotags[0].get('post_count'), 'to equal', 0);
await post.attachGeotags(geotagIds);
await geotags[0].refresh();
expect(geotags[0].get('post_count'), 'to equal', 1);
});
});
describe('#detachGeotags', () => {
it('decrements post_count', async () => {
await post.attachGeotags(geotagIds);
await geotags[0].refresh();
expect(geotags[0].get('post_count'), 'to equal', 1);
await post.detachGeotags(geotagIds);
await geotags[0].refresh();
expect(geotags[0].get('post_count'), 'to equal', 0);
});
});
describe('#updateGeotags', () => {
it('sets correct post_count', async () => {
await post.updateGeotags(geotagIds);
for (let geotag of geotags) {
await geotag.refresh();
expect(geotag.get('post_count'), 'to equal', 1);
}
let newGeotag = await new Geotag(GeotagFactory.build()).save(null, { method: 'insert' });
await post.updateGeotags([newGeotag.id]);
for (let geotag of geotags) {
await geotag.refresh();
expect(geotag.get('post_count'), 'to equal', 0);
}
await newGeotag.refresh();
expect(newGeotag.get('post_count'), 'to equal', 1);
await newGeotag.destroy();
});
});
});
describe('schools', () => {
let schools = [];
let schoolNames = [];
before(async () => {
for (let i = 0; i < 2; ++i) {
let school = await new School(SchoolFactory.build()).save(null, { method: 'insert' });
await school.refresh();
schools.push(school);
schoolNames.push(school.get('name'));
}
});
after(async () => {
for (let school of schools) {
await school.destroy();
}
});
afterEach(async () => {
await post.schools().detach(schools.map(school => school.id));
for (let school of schools) {
await school.save({ post_count: 0 });
}
});
describe('#attachSchools', () => {
it('increments post_count', async () => {
expect(schools[0].get('post_count'), 'to equal', 0);
await post.attachSchools(schoolNames);
await schools[0].refresh();
expect(schools[0].get('post_count'), 'to equal', 1);
});
});
describe('#detachSchools', () => {
it('decrements post_count', async () => {
await post.attachSchools(schoolNames);
await schools[0].refresh();
expect(schools[0].get('post_count'), 'to equal', 1);
await post.detachSchools(schoolNames);
await schools[0].refresh();
expect(schools[0].get('post_count'), 'to equal', 0);
});
});
describe('#updateSchools', () => {
it('sets correct post_count', async () => {
await post.updateSchools(schoolNames);
for (let school of schools) {
await school.refresh();
expect(school.get('post_count'), 'to equal', 1);
}
let newSchool = await new School(SchoolFactory.build()).save(null, { method: 'insert' });
await post.updateSchools([newSchool.get('name')]);
for (let school of schools) {
await school.refresh();
expect(school.get('post_count'), 'to equal', 0);
}
await newSchool.refresh();
expect(newSchool.get('post_count'), 'to equal', 1);
await newSchool.destroy();
});
});
});
describe('hashtags', () => {
let hashtags = [];
let hashtagNames = [];
before(async () => {
for (let i = 0; i < 2; ++i) {
let hashtag = await new Hashtag(HashtagFactory.build()).save(null, { method: 'insert' });
await hashtag.refresh();
hashtags.push(hashtag);
hashtagNames.push(hashtag.get('name'));
}
});
after(async () => {
for (let hashtag of hashtags) {
await hashtag.destroy();
}
});
afterEach(async () => {
await post.hashtags().detach(hashtags.map(hashtag => hashtag.id));
for (let hashtag of hashtags) {
await hashtag.save({ post_count: 0 });
}
});
describe('#attachHashtags', () => {
it('increments post_count', async () => {
expect(hashtags[0].get('post_count'), 'to equal', 0);
await post.attachHashtags(hashtagNames);
await hashtags[0].refresh();
expect(hashtags[0].get('post_count'), 'to equal', 1);
});
});
describe('#detachHashtags', () => {
it('decrements post_count', async () => {
await post.attachHashtags(hashtagNames);
await hashtags[0].refresh();
expect(hashtags[0].get('post_count'), 'to equal', 1);
await post.detachHashtags(hashtagNames);
await hashtags[0].refresh();
expect(hashtags[0].get('post_count'), 'to equal', 0);
});
});
describe('#updateHashtags', () => {
it('sets correct post_count', async () => {
await post.updateHashtags(hashtagNames);
for (let hashtag of hashtags) {
await hashtag.refresh();
expect(hashtag.get('post_count'), 'to equal', 1);
}
let newHashtag = await new Hashtag(HashtagFactory.build()).save(null, { method: 'insert' });
await post.updateHashtags([newHashtag.get('name')]);
for (let hashtag of hashtags) {
await hashtag.refresh();
expect(hashtag.get('post_count'), 'to equal', 0);
}
await newHashtag.refresh();
expect(newHashtag.get('post_count'), 'to equal', 1);
await newHashtag.destroy();
});
});
});
});