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
/
school.js
106 lines (86 loc) · 3.13 KB
/
school.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
/*
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 PostFactory from '../../../../test-helpers/factories/post';
import SchoolFactory from '../../../../test-helpers/factories/school';
import initBookshelf from '../../../../src/api/db';
const bookshelf = initBookshelf($dbConfig);
const Post = bookshelf.model('Post');
const School = bookshelf.model('School');
describe('School', () => {
let schools = [];
let posts = [];
let postIds = [];
before(async () => {
for (let i = 0; i < 2; ++i) {
const school = await new School(SchoolFactory.build()).save(null, { method: 'insert' });
schools.push(school);
}
for (let i = 0; i < 3; ++i) {
let post = await new Post(PostFactory.build()).save(null, { method: 'insert' });
posts.push(post);
postIds.push(post.id);
}
});
after(async () => {
for (let post of posts) {
await post.destroy();
}
for (let school of schools) {
await school.destroy();
}
});
afterEach(async () => {
for (let school of schools) {
await school.posts().detach(postIds);
await school.save({ post_count: 0 });
}
});
describe('.updatePostCounters', () => {
it('sets post_count to a correct number of posts for each school', async () => {
for (let school of schools) {
await school.posts().attach(postIds);
}
await School.updatePostCounters();
for (let school of schools) {
await school.refresh();
expect(school.get('post_count'), 'to equal', postIds.length);
}
});
});
describe('.updateUpdatedAt', () => {
async function setCreatedAt(schoolId, postId, createdAt) {
await bookshelf.knex('posts_schools')
.where('school_id', schoolId)
.where('post_id', postId)
.update({ created_at: createdAt });
}
it('sets updated_at to latest created_at from posts_schools', async () => {
const school = schools[0];
const dates = [
new Date(2000, 1, 1),
new Date(2016, 1, 1)
];
await school.posts().attach(posts.slice(0, 2));
await setCreatedAt(school.id, posts[0].id, dates[0]);
await setCreatedAt(school.id, posts[1].id, dates[1]);
await School.updateUpdatedAt([school.get('id')]);
await school.refresh();
expect(school.get('updated_at'), 'to equal', dates[1]);
});
});
});