Skip to content

Commit c9f2516

Browse files
author
Lucas Cosmo Rocha
committed
Updated tests for 100% of coverage.
1 parent 27902db commit c9f2516

File tree

6 files changed

+124
-723
lines changed

6 files changed

+124
-723
lines changed

lib/mapper/filters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ function buildRegEx(value) {
8989

9090
function parseDate(query, options) {
9191
const result = []
92-
if (query.start_at === 'today') query.start_at = dateToString(new Date())
93-
if (query.end_at === 'today') query.end_at = dateToString(new Date())
92+
if (query.start_at === 'today') query.start_at = normalizeDate(dateToString(new Date()), true)
93+
if (query.end_at === 'today') query.end_at = normalizeDate(dateToString(new Date()), false)
9494
if (query.period) {
9595
if (query.start_at) {
9696
let date_start = query.start_at

test/integration/index.default.config.spec.js

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const request = require('supertest')
44
const express = require('express')
55
const app = express()
66

7-
app.use(qs(default_options))
7+
app.use(qs())
88

99
app.get('/', (req, res) => {
1010
res.status(200).send(req.query)
@@ -204,18 +204,38 @@ describe('queryFilter()', function () {
204204
})
205205
})
206206

207+
it('should return req.query with set end_at params as today', function () {
208+
const expect_filters = {
209+
$and: [
210+
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
211+
{created_at: {$gte: '2019-02-05T00:00:00'}}
212+
]
213+
}
214+
215+
const options = JSON.parse(JSON.stringify(default_options))
216+
options.default.filters = expect_filters
217+
218+
const query = '?start_at=2019-02-05&end_at=today'
219+
220+
return request(app)
221+
.get(query)
222+
.then(res => {
223+
validate(res.body, options)
224+
})
225+
})
226+
207227
it('should return req.query with set start_at params as date', function () {
208228
const expect_filters = {
209229
$and: [
210230
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
211-
{created_at: {$gte: '2018-12-05T00:00:00'}}
231+
{created_at: {$gte: '2018-12-05T00:00:01'}}
212232
]
213233
}
214234

215235
const options = JSON.parse(JSON.stringify(default_options))
216236
options.default.filters = expect_filters
217237

218-
const query = '?start_at=2018-12-05'
238+
const query = '?start_at=2018-12-05T00:00:01'
219239

220240
return request(app)
221241
.get(query)
@@ -302,7 +322,7 @@ describe('queryFilter()', function () {
302322
return request(app)
303323
.get(query)
304324
.then(res => {
305-
validate(res.body, options)
325+
validateWithPeriod(res.body, options)
306326
})
307327
})
308328

@@ -322,7 +342,7 @@ describe('queryFilter()', function () {
322342
return request(app)
323343
.get(query)
324344
.then(res => {
325-
validate(res.body, options)
345+
validateWithPeriod(res.body, options)
326346
})
327347
})
328348

@@ -342,7 +362,7 @@ describe('queryFilter()', function () {
342362
return request(app)
343363
.get(query)
344364
.then(res => {
345-
validate(res.body, options)
365+
validateWithPeriod(res.body, options)
346366
})
347367
})
348368

@@ -362,7 +382,7 @@ describe('queryFilter()', function () {
362382
return request(app)
363383
.get(query)
364384
.then(res => {
365-
validate(res.body, options)
385+
validateWithPeriod(res.body, options)
366386
})
367387
})
368388

@@ -382,27 +402,27 @@ describe('queryFilter()', function () {
382402
return request(app)
383403
.get(query)
384404
.then(res => {
385-
validate(res.body, options)
405+
validateWithPeriod(res.body, options)
386406
})
387407
})
388408

389409
it('should return req.query with period as month and end_at param', function () {
390410
const expect_filters = {
391411
$and: [
392-
{created_at: {$lt: '2019-02-24T23:59:59'}},
393-
{created_at: {$gte: '2019-01-24T00:00:00'}}
412+
{created_at: {$lt: '2019-01-24T23:59:59'}},
413+
{created_at: {$gte: '2018-12-24T00:00:00'}}
394414
]
395415
}
396416

397417
const options = JSON.parse(JSON.stringify(default_options))
398418
options.default.filters = expect_filters
399419

400-
const query = '?period=1m&end_at=2019-02-24'
420+
const query = '?period=1m&end_at=2019-01-24'
401421

402422
return request(app)
403423
.get(query)
404424
.then(res => {
405-
validate(res.body, options)
425+
validateWithPeriod(res.body, options)
406426
})
407427
})
408428

@@ -422,7 +442,7 @@ describe('queryFilter()', function () {
422442
return request(app)
423443
.get(query)
424444
.then(res => {
425-
validate(res.body, options)
445+
validateWithPeriod(res.body, options)
426446
})
427447
})
428448

@@ -442,7 +462,7 @@ describe('queryFilter()', function () {
442462
return request(app)
443463
.get(query)
444464
.then(res => {
445-
validate(res.body, options)
465+
validateWithPeriod(res.body, options)
446466
})
447467
})
448468

@@ -465,6 +485,26 @@ describe('queryFilter()', function () {
465485
validate(res.body, options)
466486
})
467487
})
488+
489+
it('should return req.query with today end_at for invalid period', function () {
490+
const expect_filters = {
491+
$and: [
492+
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
493+
{created_at: {$gte: '2018-12-05T00:00:01'}}
494+
]
495+
}
496+
497+
const options = JSON.parse(JSON.stringify(default_options))
498+
options.default.filters = expect_filters
499+
500+
const query = '?period=12&start_at=2018-12-05T00:00:01'
501+
502+
return request(app)
503+
.get(query)
504+
.then(res => {
505+
validate(res.body, options)
506+
})
507+
})
468508
})
469509
})
470510
})
@@ -480,6 +520,16 @@ function validate(query, options) {
480520
expect(query.filters).to.eql(options.default.filters)
481521
}
482522

523+
function validateWithPeriod(query, options) {
524+
expect(query).is.not.null
525+
expect(query).is.not.eql({})
526+
expect(query.pagination.limit).to.eql(options.default.pagination.limit)
527+
expect(query.pagination.skip).to.eql(options.default.pagination.skip)
528+
expect(query.sort).to.eql(options.default.sort)
529+
expect(query.fields).to.eql(options.default.fields)
530+
expect(query.filters).to.have.property('$and')
531+
}
532+
483533
function normalizeDate(date, isDateStart) {
484534
if (isDateStart) {
485535
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T00:00:00')

0 commit comments

Comments
 (0)