-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cast iso date strings to actual dates in query objects
- Loading branch information
1 parent
a842d03
commit 91084ea
Showing
3 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import test from 'ava' | ||
|
||
import prepareFilter from './prepareFilter' | ||
|
||
test('should return type filter', (t) => { | ||
const params = {type: 'entry'} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = {type: 'entry'} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return _id filter', (t) => { | ||
const params = {type: 'entry', id: 'ent1'} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = {_id: 'entry:ent1'} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should use endpoint.query as filter', (t) => { | ||
const params = {type: 'entry', id: 'ent1'} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database', | ||
query: [ | ||
{path: 'type', value: 'other'}, | ||
{path: 'attributes\\.parentType', param: 'type'} | ||
] | ||
} | ||
const expected = { | ||
type: 'other', | ||
'attributes.parentType': 'entry' | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should add params query to endpoint query filter', (t) => { | ||
const params = { | ||
type: 'entry', | ||
query: {'attributes.section': 'news'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database', | ||
query: [ | ||
{path: 'type', value: 'other'}, | ||
{path: 'attributes\\.parentType', param: 'type'} | ||
] | ||
} | ||
const expected = { | ||
type: 'other', | ||
'attributes.parentType': 'entry', | ||
'attributes.section': 'news' | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should add params query to type filter', (t) => { | ||
const params = { | ||
type: 'entry', | ||
query: {'attributes.section': 'news'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = { | ||
type: 'entry', | ||
'attributes.section': 'news' | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should add params query to id filter', (t) => { | ||
const params = { | ||
id: 'ent1', | ||
type: 'entry', | ||
query: {'attributes.section': 'news'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = { | ||
_id: 'entry:ent1', | ||
'attributes.section': 'news' | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should cast date strings as Date', (t) => { | ||
const params = { | ||
type: 'entry', | ||
query: {'attributes.updatedAt': '2017-11-13T18:43:01.000Z'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = { | ||
type: 'entry', | ||
'attributes.updatedAt': new Date('2017-11-13T18:43:01.000Z') | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should cast date strings without microseconds as Date', (t) => { | ||
const params = { | ||
type: 'entry', | ||
query: {'attributes.updatedAt': '2017-11-13T18:43:01Z'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = { | ||
type: 'entry', | ||
'attributes.updatedAt': new Date('2017-11-13T18:43:01Z') | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should cast date strings with other time zone as Date', (t) => { | ||
const params = { | ||
type: 'entry', | ||
query: {'attributes.updatedAt': '2017-11-13T18:43:01.000+01:00'} | ||
} | ||
const endpoint = { | ||
collection: 'documents', | ||
db: 'database' | ||
} | ||
const expected = { | ||
type: 'entry', | ||
'attributes.updatedAt': new Date('2017-11-13T18:43:01.000+01:00') | ||
} | ||
|
||
const ret = prepareFilter(params, endpoint, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters