Skip to content

Commit

Permalink
Add date parse utility + tests for better reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Aug 11, 2018
1 parent 083c282 commit 0d8ebc0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 42 deletions.
21 changes: 3 additions & 18 deletions src/builders/history-query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const dateRange = require('../utilities/date_range');

/**
* Builds Mongo query for history endpints
* @param {Object} query Koa querystring object from ctx.request
Expand All @@ -8,25 +10,8 @@
module.exports = (q) => {
const query = {};

// Allow date range comparisons using a variety of date formats
if (q.start && (q.final || q.end)) {
let startParsed;
let endParsed;
// Matches any string of consecutive numbers ex. 1520314380
if (/^[0-9]*$/.test(q.start && (q.final || q.end))) {
// If the date is unix, it is converted to a compatible date object
startParsed = new Date(q.start * 1000);
endParsed = new Date(q.final * 1000 || q.end * 1000);
} else {
// If not unix, a date object is created from the input
startParsed = new Date(q.start);
endParsed = new Date(q.final || q.end);
}
try {
query.event_date_utc = { $gte: startParsed.toISOString(), $lte: endParsed.toISOString() };
} catch (e) {
console.log(e);
}
query.event_date_utc = dateRange(q);
}

if (q.flight_number) {
Expand Down
25 changes: 6 additions & 19 deletions src/builders/launch-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

// Required to correctly output ObjectID's
const ObjectId = require('mongodb').ObjectID;
const dayjs = require('dayjs');
const dateRange = require('../utilities/date_range');

/**
* Builds mongo query for past/upcoming launch endpoints from querystrings
Expand All @@ -17,25 +19,10 @@ module.exports = (q) => {
query._id = ObjectId(q.flight_id);
}

// Allow date range comparisons using a variety of date formats
if (q.start && (q.final || q.end)) {
let startParsed;
let endParsed;
// Matches any string of consecutive numbers ex. 1520314380
// If the date is unix, it is converted to a compatible date constructor param
if (/^[0-9]*$/.test(q.start && (q.final || q.end))) {
startParsed = new Date(q.start * 1000);
endParsed = new Date(q.final * 1000 || q.end * 1000);
} else {
// If not unix, a date is created from the input
startParsed = new Date(q.start);
endParsed = new Date(q.final || q.end);
}
try {
query.launch_date_utc = { $gte: startParsed.toISOString(), $lte: endParsed.toISOString() };
} catch (e) {
console.log(e);
}
query.launch_date_utc = dateRange(q);
console.log(q);
console.log(dateRange(q));
}

if (q.flight_number) {
Expand All @@ -48,7 +35,7 @@ module.exports = (q) => {

if (q.launch_date_utc) {
// Allow any valid date format
const date = new Date(q.launch_date_utc);
const date = dayjs(q.launch_date_utc);
try {
query.launch_date_utc = date.toISOString();
} catch (e) {
Expand Down
23 changes: 23 additions & 0 deletions src/utilities/date_range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

const dayjs = require('dayjs');

/**
* Return date range object for mongo find query
* @param {Object} query Koa querystring object from ctx.request
* @return {Object} Mongo compatible date range query object
*/

module.exports = (q) => {
let start;
let end;
// See if date is unix, if so, add miliseconds
if (/^[0-9]*$/.test(q.start && (q.final || q.end))) {
start = dayjs(q.start * 1000);
end = dayjs(q.final * 1000 || q.end * 1000);
} else {
start = dayjs(q.start);
end = dayjs(q.final || q.end);
}

return { $gte: start.toISOString(), $lte: end.toISOString() };
};
2 changes: 1 addition & 1 deletion test/builders/history-query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ test('It should return Falcon Heavy Test Flight', async () => {
test('It should return an empty array due to invalid date', async () => {
const response = await request(app.callback()).get('/v2/info/history?start=2020-25-23&end=2020-25-24');
expect(response.statusCode).toBe(200);
expect(response.body.length).toBeGreaterThanOrEqual(19);
expect(response.body).toEqual([]);
});
8 changes: 4 additions & 4 deletions test/routes/v2-launches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ test('It should return the all launches', async () => {
});
});

test('It should return all launches due to invalid date', async () => {
test('It should return no launches due to invalid date', async () => {
const response = await request(app.callback()).get('/v2/launches?start=2020-25-23&end=2020-25-24');
expect(response.statusCode).toBe(200);
expect(response.body.length).toBeGreaterThan(65);
expect(response.body).toEqual([]);
});

test('It should return all launches due to invalid UTC date', async () => {
test('It should return no launches due to invalid UTC date', async () => {
const response = await request(app.callback()).get('/v2/launches?launch_date_utc=2011-25-05T14:48:00.000Z');
expect(response.statusCode).toBe(200);
expect(response.body.length).toBeGreaterThan(65);
expect(response.body.length).toBeGreaterThanOrEqual(66);
});

//------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions test/utilities/date_parse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

const dateParse = require('../../src/utilities/date_range');

test('It should return the correct date comparison object', () => {
const query = {
start: '2017-06-22',
final: '2017-06-25',
};
const response = dateParse(query);
expect(response).toEqual({ $gte: '2017-06-22T05:00:00.000Z', $lte: '2017-06-25T05:00:00.000Z' });
});

0 comments on commit 0d8ebc0

Please sign in to comment.