Add test suites for __lte, __gte, __eq, __neq operators
This commit is contained in:
@@ -83,7 +83,14 @@ const listTableRows = async (req, res) => {
|
||||
const re = /,(?![^\[]*?\])/;
|
||||
try {
|
||||
filters = _filters.split(re).map((filter) => {
|
||||
let [key, value] = filter.split(':');
|
||||
//NOTE: When using the _filter parameter, the values are split using the ":" sign, like this (_filters=Total__eq:1). However, if the user sends a date value, such as (_filters=InvoiceDate__eq:2010-01-08 00:00:00), there will be additional colon (":") signs present.
|
||||
let [key, ...value] = filter.split(':');
|
||||
if (value.length === 1) {
|
||||
value = value[0];
|
||||
} else {
|
||||
value = value.map((element) => element).join(':');
|
||||
}
|
||||
|
||||
let field = key.split('__')[0];
|
||||
let fieldOperator = key.split('__')[1];
|
||||
|
||||
@@ -109,8 +116,6 @@ const listTableRows = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
//console.log('Filters ', filters);
|
||||
|
||||
let whereString = '';
|
||||
const whereStringValues = [];
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const { not } = require('joi');
|
||||
const supertest = require('supertest');
|
||||
|
||||
const app = require('../index');
|
||||
@@ -69,11 +70,86 @@ describe('Rows Endpoints', () => {
|
||||
expect(res.body.data[0].lastName).not.toBeNull();
|
||||
});
|
||||
|
||||
it('GET /tables/:name/rows: should filter items by createdAt date', async () => {
|
||||
it('GET /tables/:name/rows: should successfully retrieve users created after 2010-01-01 00:00:00.', async () => {
|
||||
const date = '2010-01-01 00:00:00';
|
||||
const res = await requestWithSupertest.get(
|
||||
'/api/tables/users/rows?_filters=createdAt__gte:2009-01-01 00:00:00'
|
||||
`/api/tables/users/rows?_filters=createdAt__gte:${date}`
|
||||
);
|
||||
|
||||
res.body.data.map((user) => {
|
||||
const createdAt = new Date(user.createdAt);
|
||||
const referenceDate = new Date(date);
|
||||
expect(createdAt.getTime()).toBeGreaterThan(referenceDate.getTime());
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.data[0]).toHaveProperty('id');
|
||||
expect(res.body.data[0]).toHaveProperty('firstName');
|
||||
expect(res.body.data[0]).toHaveProperty('lastName');
|
||||
expect(res.body.data[0]).toHaveProperty('createdAt');
|
||||
});
|
||||
|
||||
it('GET /tables/:name/rows: should successfully retrieve users created before 2008-01-20 00:00:00.', async () => {
|
||||
const date = '2008-01-20 00:00:00';
|
||||
const res = await requestWithSupertest.get(
|
||||
`/api/tables/users/rows?_filters=createdAt__lte:${date}`
|
||||
);
|
||||
|
||||
res.body.data.map((user) => {
|
||||
const createdAt = new Date(user.createdAt);
|
||||
const referenceDate = new Date(date);
|
||||
expect(createdAt.getTime()).toBeLessThan(referenceDate.getTime());
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.data[0]).toHaveProperty('id');
|
||||
expect(res.body.data[0]).toHaveProperty('firstName');
|
||||
expect(res.body.data[0]).toHaveProperty('lastName');
|
||||
expect(res.body.data[0]).toHaveProperty('createdAt');
|
||||
});
|
||||
|
||||
it('GET /tables/:name/rows: should successfully retrieve users created at 2013-01-08 00:00:00', async () => {
|
||||
const date = '2013-01-08 00:00:00';
|
||||
const res = await requestWithSupertest.get(
|
||||
`/api/tables/users/rows?_filters=createdAt__eq:${date}`
|
||||
);
|
||||
|
||||
res.body.data.map((user) => {
|
||||
const createdAt = new Date(user.createdAt);
|
||||
const referenceDate = new Date(date);
|
||||
expect(createdAt.getTime()).toEqual(referenceDate.getTime());
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.data[0]).toHaveProperty('id');
|
||||
expect(res.body.data[0]).toHaveProperty('firstName');
|
||||
expect(res.body.data[0]).toHaveProperty('lastName');
|
||||
expect(res.body.data[0]).toHaveProperty('createdAt');
|
||||
});
|
||||
|
||||
it('GET /tables/:name/rows: should successfully retrieve users created at 2007-01-08 00:00:00', async () => {
|
||||
const date = '2007-01-08 00:00:00';
|
||||
const res = await requestWithSupertest.get(
|
||||
`/api/tables/users/rows?_filters=createdAt__eq:${date}`
|
||||
);
|
||||
|
||||
//There are no users that are created at 2007-01-08 00:00:00 so the API should return empty data
|
||||
expect(res.body.data).toHaveLength(0);
|
||||
expect(res.status).toEqual(200);
|
||||
});
|
||||
|
||||
it('GET /tables/:name/rows: should successfully retrieve users that are not created at 2021-01-08 00:00:00', async () => {
|
||||
const date = '2021-01-08 00:00:00';
|
||||
const res = await requestWithSupertest.get(
|
||||
`/api/tables/users/rows?_filters=createdAt__neq:${date}`
|
||||
);
|
||||
|
||||
res.body.data.map((user) => {
|
||||
const createdAt = new Date(user.createdAt);
|
||||
const referenceDate = new Date(date);
|
||||
expect(createdAt.getTime()).not.toEqual(referenceDate.getTime());
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.data[0]).toHaveProperty('id');
|
||||
expect(res.body.data[0]).toHaveProperty('firstName');
|
||||
|
||||
@@ -40,7 +40,7 @@ const testNames = [
|
||||
},
|
||||
{ firstName: 'Liam', lastName: 'Gonzalez', createdAt: '2020-01-08 00:00:00' },
|
||||
{ firstName: 'Emma', lastName: 'Gomez', createdAt: '2021-01-08 00:00:00' },
|
||||
{ firstName: 'Noah', lastName: 'Perez', createdAt: '2022-01-08 00:00:00' },
|
||||
{ firstName: 'Noah', lastName: 'Perez', createdAt: '2021-01-08 00:00:00' },
|
||||
{ firstName: 'Avery', lastName: 'Ramirez', createdAt: '2023-02-08 00:00:00' },
|
||||
{ firstName: 'Jacob', lastName: 'Turner', createdAt: '2023-03-08 00:00:00' },
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user