Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93610a5

Browse files
committed
Added half of launch query tests and added id field for projection info
1 parent e75731e commit 93610a5

File tree

4 files changed

+282
-2
lines changed

4 files changed

+282
-2
lines changed

src/builders/project-query.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Returns project query for past/upcoming launches
3+
*/
4+
5+
const lowerCase = require('lower-case');
6+
7+
exports.queryProject = (req) => {
8+
const query = {};
9+
10+
if (lowerCase(req.query.id) !== 'true') {
11+
// Mongo _id field requires underscore dangle
12+
// eslint-disable-next-line no-underscore-dangle
13+
query._id = 0;
14+
}
15+
16+
return query;
17+
};

src/helpers/launch-database.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
12
const launch = require('../builders/launch-query');
23
const sort = require('../builders/launch-sort');
4+
const project = require('../builders/project-query');
35

46
module.exports.fetchLaunch = async (collection, req) => {
57
return global.db
68
.collection(collection)
79
.find(launch.launchQuery(req))
8-
.project({ _id: 0 })
10+
.project(project.queryProject(req))
911
.sort(sort.launchSort(req))
1012
.toArray();
1113
};

src/v2-routes/v2-launches.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require('express');
44
const asyncHandle = require('express-async-handler');
5+
const project = require('../builders/project-query');
56
const { fetchLaunch } = require('../helpers/launch-database');
67

78
const v2 = express.Router();
@@ -11,7 +12,7 @@ v2.get('/latest', asyncHandle(async (req, res) => {
1112
const data = await global.db
1213
.collection('launch_v2')
1314
.find({})
14-
.project({ _id: 0 })
15+
.project(project.queryProject(req))
1516
.sort({ flight_number: -1 })
1617
.limit(1)
1718
.toArray();

test/builders/launch-query.test.js

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
2+
const request = require('supertest');
3+
const app = require('../../src/app');
4+
5+
beforeAll((done) => {
6+
app.on('ready', () => {
7+
done();
8+
});
9+
});
10+
11+
//------------------------------------------------------------
12+
// Launch Query Test
13+
//------------------------------------------------------------
14+
15+
test('It should return flight number 42', () => {
16+
return request(app).get('/v2/launches?flight_number=42').then((response) => {
17+
expect(response.statusCode).toBe(200);
18+
response.body.forEach((item) => {
19+
expect(item).toHaveProperty('flight_number', 42);
20+
});
21+
});
22+
});
23+
24+
test('It should return flight 42 in date range', () => {
25+
return request(app).get('/v2/launches?start=2017-06-22&final=2017-06-25').then((response) => {
26+
expect(response.statusCode).toBe(200);
27+
response.body.forEach((item) => {
28+
expect(item).toHaveProperty('flight_number', 42);
29+
});
30+
});
31+
});
32+
33+
test('It should return launches in 2017', () => {
34+
return request(app).get('/v2/launches?launch_year=2017').then((response) => {
35+
expect(response.statusCode).toBe(200);
36+
expect(response.body.length).toEqual(18);
37+
});
38+
});
39+
40+
test('It should return flight 42 with given launch date in UTC', () => {
41+
return request(app).get('/v2/launches?launch_date_utc=2017-06-23T19:10:00Z').then((response) => {
42+
expect(response.statusCode).toBe(200);
43+
response.body.forEach((item) => {
44+
expect(item).toHaveProperty('flight_number', 42);
45+
});
46+
});
47+
});
48+
49+
test('It should return flight 42 with given launch date in local time', () => {
50+
return request(app).get('/v2/launches?launch_date_local=2017-06-23T15:10:00-04:00').then((response) => {
51+
expect(response.statusCode).toBe(200);
52+
response.body.forEach((item) => {
53+
expect(item).toHaveProperty('flight_number', 42);
54+
});
55+
});
56+
});
57+
58+
test('It should return launches with falcon9 rocket id', () => {
59+
return request(app).get('/v2/launches?rocket_id=falcon9').then((response) => {
60+
expect(response.statusCode).toBe(200);
61+
response.body.forEach((item) => {
62+
expect(item).toHaveProperty('rocket.rocket_id', 'falcon9');
63+
});
64+
});
65+
});
66+
67+
test('It should return launches with falcon 9 rocket name', () => {
68+
return request(app).get('/v2/launches?rocket_name=Falcon+9').then((response) => {
69+
expect(response.statusCode).toBe(200);
70+
response.body.forEach((item) => {
71+
expect(item).toHaveProperty('rocket.rocket_name', 'Falcon 9');
72+
});
73+
});
74+
});
75+
76+
test('It should return launches with FT rocket type', () => {
77+
return request(app).get('/v2/launches?rocket_type=FT').then((response) => {
78+
expect(response.statusCode).toBe(200);
79+
response.body.forEach((item) => {
80+
expect(item).toHaveProperty('rocket.rocket_type', 'FT');
81+
});
82+
});
83+
});
84+
85+
test('It should return launches with core serial B1029', () => {
86+
return request(app).get('/v2/launches?core_serial=B1029').then((response) => {
87+
expect(response.statusCode).toBe(200);
88+
response.body.forEach((item) => {
89+
item.rocket.first_stage.cores.forEach((core) => {
90+
expect(core).toHaveProperty('core_serial', 'B1029');
91+
});
92+
});
93+
});
94+
});
95+
96+
test('It should return launches with cap serial C113', () => {
97+
return request(app).get('/v2/launches?cap_serial=C113').then((response) => {
98+
expect(response.statusCode).toBe(200);
99+
response.body.forEach((item) => {
100+
item.rocket.second_stage.payloads.forEach((cap) => {
101+
expect(cap).toHaveProperty('cap_serial', 'C113');
102+
});
103+
});
104+
});
105+
});
106+
107+
test('It should return launches with 2 core flights', () => {
108+
return request(app).get('/v2/launches?core_flight=2').then((response) => {
109+
expect(response.statusCode).toBe(200);
110+
response.body.forEach((item) => {
111+
item.rocket.first_stage.cores.forEach((core) => {
112+
expect(core).toHaveProperty('flight');
113+
});
114+
});
115+
});
116+
});
117+
118+
test('It should return launches with reused cores', () => {
119+
return request(app).get('/v2/launches?core_reuse=true').then((response) => {
120+
expect(response.statusCode).toBe(200);
121+
response.body.forEach((item) => {
122+
expect(item.reuse).toHaveProperty('core', true);
123+
});
124+
});
125+
});
126+
127+
test('It should return launches with reused side core 1', () => {
128+
return request(app).get('/v2/launches?side_core1_reuse=true').then((response) => {
129+
expect(response.statusCode).toBe(200);
130+
response.body.forEach((item) => {
131+
expect(item.reuse).toHaveProperty('side_core1', true);
132+
});
133+
});
134+
});
135+
136+
test('It should return launches with reused side core 2', () => {
137+
return request(app).get('/v2/launches?side_core2_reuse=true').then((response) => {
138+
expect(response.statusCode).toBe(200);
139+
response.body.forEach((item) => {
140+
expect(item.reuse).toHaveProperty('side_core2', true);
141+
});
142+
});
143+
});
144+
145+
test('It should return launches with no reused fairings', () => {
146+
return request(app).get('/v2/launches?fairings_reuse=false').then((response) => {
147+
expect(response.statusCode).toBe(200);
148+
response.body.forEach((item) => {
149+
expect(item.reuse).toHaveProperty('fairings', false);
150+
});
151+
});
152+
});
153+
154+
test('It should return launches with reused capsules', () => {
155+
return request(app).get('/v2/launches?capsule_reuse=true').then((response) => {
156+
expect(response.statusCode).toBe(200);
157+
response.body.forEach((item) => {
158+
expect(item.reuse).toHaveProperty('capsule', true);
159+
});
160+
});
161+
});
162+
163+
// test('It should return', () => {
164+
// return request(app).get('/v2/launches?').then((response) => {
165+
// expect(response.statusCode).toBe(200);
166+
// response.body.forEach((item) => {
167+
// expect(item).toHaveProperty('', '');
168+
// });
169+
// });
170+
// });
171+
172+
// test('It should return', () => {
173+
// return request(app).get('/v2/launches?').then((response) => {
174+
// expect(response.statusCode).toBe(200);
175+
// response.body.forEach((item) => {
176+
// expect(item).toHaveProperty('', '');
177+
// });
178+
// });
179+
// });
180+
181+
// test('It should return', () => {
182+
// return request(app).get('/v2/launches?').then((response) => {
183+
// expect(response.statusCode).toBe(200);
184+
// response.body.forEach((item) => {
185+
// expect(item).toHaveProperty('', '');
186+
// });
187+
// });
188+
// });
189+
190+
// test('It should return', () => {
191+
// return request(app).get('/v2/launches?').then((response) => {
192+
// expect(response.statusCode).toBe(200);
193+
// response.body.forEach((item) => {
194+
// expect(item).toHaveProperty('', '');
195+
// });
196+
// });
197+
// });
198+
199+
// test('It should return', () => {
200+
// return request(app).get('/v2/launches?').then((response) => {
201+
// expect(response.statusCode).toBe(200);
202+
// response.body.forEach((item) => {
203+
// expect(item).toHaveProperty('', '');
204+
// });
205+
// });
206+
// });
207+
208+
// test('It should return', () => {
209+
// return request(app).get('/v2/launches?').then((response) => {
210+
// expect(response.statusCode).toBe(200);
211+
// response.body.forEach((item) => {
212+
// expect(item).toHaveProperty('', '');
213+
// });
214+
// });
215+
// });
216+
217+
// test('It should return', () => {
218+
// return request(app).get('/v2/launches?').then((response) => {
219+
// expect(response.statusCode).toBe(200);
220+
// response.body.forEach((item) => {
221+
// expect(item).toHaveProperty('', '');
222+
// });
223+
// });
224+
// });
225+
226+
// test('It should return', () => {
227+
// return request(app).get('/v2/launches?').then((response) => {
228+
// expect(response.statusCode).toBe(200);
229+
// response.body.forEach((item) => {
230+
// expect(item).toHaveProperty('', '');
231+
// });
232+
// });
233+
// });
234+
235+
// test('It should return', () => {
236+
// return request(app).get('/v2/launches?').then((response) => {
237+
// expect(response.statusCode).toBe(200);
238+
// response.body.forEach((item) => {
239+
// expect(item).toHaveProperty('', '');
240+
// });
241+
// });
242+
// });
243+
244+
// test('It should return', () => {
245+
// return request(app).get('/v2/launches?').then((response) => {
246+
// expect(response.statusCode).toBe(200);
247+
// response.body.forEach((item) => {
248+
// expect(item).toHaveProperty('', '');
249+
// });
250+
// });
251+
// });
252+
253+
// test('It should return', () => {
254+
// return request(app).get('/v2/launches?').then((response) => {
255+
// expect(response.statusCode).toBe(200);
256+
// response.body.forEach((item) => {
257+
// expect(item).toHaveProperty('', '');
258+
// });
259+
// });
260+
// });

0 commit comments

Comments
 (0)