|
3 | 3 | */
|
4 | 4 | import chai from 'chai';
|
5 | 5 | import request from 'supertest';
|
| 6 | +import config from 'config'; |
| 7 | +import _ from 'lodash'; |
6 | 8 |
|
7 | 9 | import models from '../../models';
|
8 | 10 | import server from '../../app';
|
9 | 11 | import testUtil from '../../tests/util';
|
10 | 12 |
|
11 | 13 | const should = chai.should();
|
12 | 14 |
|
| 15 | +const ES_TIMELINE_INDEX = config.get('elasticsearchConfig.timelineIndexName'); |
| 16 | +const ES_TIMELINE_TYPE = config.get('elasticsearchConfig.timelineDocType'); |
| 17 | + |
| 18 | +const timelines = [ |
| 19 | + { |
| 20 | + name: 'name 1', |
| 21 | + description: 'description 1', |
| 22 | + startDate: '2018-05-11T00:00:00.000Z', |
| 23 | + endDate: '2018-05-12T00:00:00.000Z', |
| 24 | + reference: 'project', |
| 25 | + referenceId: 1, |
| 26 | + createdBy: 1, |
| 27 | + updatedBy: 1, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'name 2', |
| 31 | + description: 'description 2', |
| 32 | + startDate: '2018-05-12T00:00:00.000Z', |
| 33 | + endDate: '2018-05-13T00:00:00.000Z', |
| 34 | + reference: 'phase', |
| 35 | + referenceId: 1, |
| 36 | + createdBy: 1, |
| 37 | + updatedBy: 1, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'name 3', |
| 41 | + description: 'description 3', |
| 42 | + startDate: '2018-05-13T00:00:00.000Z', |
| 43 | + endDate: '2018-05-14T00:00:00.000Z', |
| 44 | + reference: 'phase', |
| 45 | + referenceId: 1, |
| 46 | + createdBy: 1, |
| 47 | + updatedBy: 1, |
| 48 | + deletedAt: '2018-05-14T00:00:00.000Z', |
| 49 | + }, |
| 50 | +]; |
13 | 51 | const milestones = [
|
14 | 52 | {
|
15 | 53 | id: 1,
|
@@ -143,41 +181,37 @@ describe('GET timeline', () => {
|
143 | 181 | ]))
|
144 | 182 | .then(() =>
|
145 | 183 | // Create timelines
|
146 |
| - models.Timeline.bulkCreate([ |
147 |
| - { |
148 |
| - name: 'name 1', |
149 |
| - description: 'description 1', |
150 |
| - startDate: '2018-05-11T00:00:00.000Z', |
151 |
| - endDate: '2018-05-12T00:00:00.000Z', |
152 |
| - reference: 'project', |
153 |
| - referenceId: 1, |
154 |
| - createdBy: 1, |
155 |
| - updatedBy: 1, |
156 |
| - }, |
157 |
| - { |
158 |
| - name: 'name 2', |
159 |
| - description: 'description 2', |
160 |
| - startDate: '2018-05-12T00:00:00.000Z', |
161 |
| - endDate: '2018-05-13T00:00:00.000Z', |
162 |
| - reference: 'phase', |
163 |
| - referenceId: 1, |
164 |
| - createdBy: 1, |
165 |
| - updatedBy: 1, |
166 |
| - }, |
167 |
| - { |
168 |
| - name: 'name 3', |
169 |
| - description: 'description 3', |
170 |
| - startDate: '2018-05-13T00:00:00.000Z', |
171 |
| - endDate: '2018-05-14T00:00:00.000Z', |
172 |
| - reference: 'phase', |
173 |
| - referenceId: 1, |
174 |
| - createdBy: 1, |
175 |
| - updatedBy: 1, |
176 |
| - deletedAt: '2018-05-14T00:00:00.000Z', |
177 |
| - }, |
178 |
| - ])) |
179 |
| - .then(() => models.Milestone.bulkCreate(milestones)) |
180 |
| - .then(() => done()); |
| 184 | + // Create timelines |
| 185 | + models.Timeline.bulkCreate(timelines, { returning: true }) |
| 186 | + .then(createdTimelines => ( |
| 187 | + // create milestones after timelines |
| 188 | + models.Milestone.bulkCreate(milestones)) |
| 189 | + .then(createdMilestones => [createdTimelines, createdMilestones]), |
| 190 | + ), |
| 191 | + ).then(([createdTimelines, createdMilestones]) => |
| 192 | + // Index to ES |
| 193 | + Promise.all(_.map(createdTimelines, async (createdTimeline) => { |
| 194 | + const timelineJson = _.omit(createdTimeline.toJSON(), 'deletedAt', 'deletedBy'); |
| 195 | + timelineJson.projectId = createdTimeline.id !== 3 ? 1 : 2; |
| 196 | + if (timelineJson.id === 1) { |
| 197 | + timelineJson.milestones = _.map( |
| 198 | + createdMilestones, |
| 199 | + cm => _.omit(cm.toJSON(), 'deletedAt', 'deletedBy'), |
| 200 | + ); |
| 201 | + } else if (timelineJson.id === 2) { |
| 202 | + timelineJson.description = 'from ES'; |
| 203 | + } |
| 204 | + |
| 205 | + await server.services.es.index({ |
| 206 | + index: ES_TIMELINE_INDEX, |
| 207 | + type: ES_TIMELINE_TYPE, |
| 208 | + id: timelineJson.id, |
| 209 | + body: timelineJson, |
| 210 | + }); |
| 211 | + })) |
| 212 | + .then(() => { |
| 213 | + done(); |
| 214 | + })); |
181 | 215 | });
|
182 | 216 | });
|
183 | 217 | });
|
@@ -316,5 +350,53 @@ describe('GET timeline', () => {
|
316 | 350 | })
|
317 | 351 | .expect(200, done);
|
318 | 352 | });
|
| 353 | + |
| 354 | + it('should return data from ES when db param is not set', (done) => { |
| 355 | + request(server) |
| 356 | + .get('/v5/timelines/2') |
| 357 | + .set({ |
| 358 | + Authorization: `Bearer ${testUtil.jwts.admin}`, |
| 359 | + }) |
| 360 | + .expect(200) |
| 361 | + .end((err, res) => { |
| 362 | + const resJson = res.body; |
| 363 | + resJson.id.should.be.eql(2); |
| 364 | + resJson.name.should.be.eql('name 2'); |
| 365 | + resJson.description.should.be.eql('from ES'); |
| 366 | + |
| 367 | + resJson.startDate.should.be.eql('2018-05-12T00:00:00.000Z'); |
| 368 | + resJson.endDate.should.be.eql('2018-05-13T00:00:00.000Z'); |
| 369 | + resJson.reference.should.be.eql('phase'); |
| 370 | + resJson.referenceId.should.be.eql(1); |
| 371 | + |
| 372 | + resJson.createdBy.should.be.eql(1); |
| 373 | + should.exist(resJson.createdAt); |
| 374 | + resJson.updatedBy.should.be.eql(1); |
| 375 | + should.exist(resJson.updatedAt); |
| 376 | + should.not.exist(resJson.deletedBy); |
| 377 | + should.not.exist(resJson.deletedAt); |
| 378 | + |
| 379 | + should.not.exist(resJson.milestones); |
| 380 | + |
| 381 | + done(); |
| 382 | + }); |
| 383 | + }); |
| 384 | + |
| 385 | + it('should return data from DB without calling ES when db param is set', (done) => { |
| 386 | + request(server) |
| 387 | + .get('/v5/timelines/2?db=true') |
| 388 | + .set({ |
| 389 | + Authorization: `Bearer ${testUtil.jwts.admin}`, |
| 390 | + }) |
| 391 | + .expect(200) |
| 392 | + .end((err, res) => { |
| 393 | + const resJson = res.body; |
| 394 | + resJson.id.should.be.eql(2); |
| 395 | + resJson.name.should.be.eql('name 2'); |
| 396 | + resJson.description.should.be.eql('description 2'); |
| 397 | + |
| 398 | + done(); |
| 399 | + }); |
| 400 | + }); |
319 | 401 | });
|
320 | 402 | });
|
0 commit comments