Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: require pagination #979

Merged
merged 11 commits into from
Dec 15, 2023
42 changes: 26 additions & 16 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,32 @@ export const baseSchema = {
timeStaged: Joi.date().timestamp().allow(null).optional(),
};

export const projectsGetQuerySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
warehouseProjectId: Joi.string(),
xls: Joi.boolean(),
projectIds: Joi.array().items(Joi.string()).single(),
order: Joi.string().regex(genericSortColumnRegex),
filter: Joi.string().regex(genericFilterRegex),
onlyMarketplaceProjects: Joi.boolean(),
})
.with('page', 'limit')
.with('limit', 'page');
export const projectsGetQuerySchema = Joi.object({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
warehouseProjectId: Joi.string(),
xls: Joi.boolean(),
projectIds: Joi.array().items(Joi.string()).single(),
order: Joi.string().regex(genericSortColumnRegex),
filter: Joi.string().regex(genericFilterRegex),
onlyMarketplaceProjects: Joi.boolean(),
})
.when(
Joi.object({
warehouseProjectId: Joi.string().min(1),
}).or('warehouseProjectId'),
{
then: Joi.object(),
otherwise: Joi.object({
page: Joi.number().required(),
limit: Joi.number().required(),
}),
},
)
.and('page', 'limit');

export const projectsPostSchema = Joi.object({
...baseSchema,
Expand Down
51 changes: 30 additions & 21 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,36 @@ export const unitsPostSchema = Joi.object({
...unitsBaseSchema,
});

export const unitsGetQuerySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
warehouseUnitId: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
order: Joi.alternatives().try(
// backwards compatibility for old order usage
Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
// new order usage
Joi.string().regex(genericSortColumnRegex),
),
xls: Joi.boolean(),
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
includeProjectInfoInSearch: Joi.boolean(),
filter: Joi.string().regex(genericFilterRegex),
})
.with('page', 'limit');
export const unitsGetQuerySchema = Joi.object({
page: Joi.number().min(1),
limit: Joi.number().max(100).min(1),
search: Joi.string(),
warehouseUnitId: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
order: Joi.alternatives().try(
Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
Joi.string().regex(genericSortColumnRegex),
),
xls: Joi.boolean(),
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
includeProjectInfoInSearch: Joi.boolean(),
filter: Joi.string().regex(genericFilterRegex),
})
.when(
Joi.object({
warehouseUnitId: Joi.string().min(1),
}).or('warehouseUnitId'),
{
then: Joi.object(),
otherwise: Joi.object({
page: Joi.number().min(1).required(),
limit: Joi.number().max(100).min(1).required(),
}),
},
)
.and('page', 'limit');

export const unitsUpdateSchema = Joi.object({
warehouseUnitId: Joi.string().required(),
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ describe('Unit Resource Integration Tests', function () {
await testFixtures.waitForDataLayerSync();

// Get a unit to split
const allUnitsResult = await supertest(app).get('/v1/units');
const allUnitsResult = await supertest(app)
.get('/v1/units')
.query({ page: 1, limit: 100 });

const unitRecord = _.head(allUnitsResult.body);
const unitRecord = _.head(allUnitsResult.body.data);

const warehouseUnitIdToSplit = unitRecord.warehouseUnitId;
const newUnitOwner = '35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf';
Expand Down
29 changes: 20 additions & 9 deletions tests/resources/projects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ describe('Project Resource CRUD', function () {
describe('error states', function () {
it('errors if there if there is no connection to the datalayer', async function () {
sinon.stub(datalayer, 'dataLayerAvailable').resolves(false);
const response = await supertest(app).get('/v1/projects');
const response = await supertest(app)
.get('/v1/projects')
.query({ page: 1, limit: 100 });
expect(response.statusCode).to.equal(400);

expect(response.body.error).to.equal(
Expand All @@ -55,34 +57,43 @@ describe('Project Resource CRUD', function () {
describe('success states', function () {
it('gets all the projects available', async function () {
// no query params
const projects = await testFixtures.getProjectByQuery();
expect(projects.length).to.equal(12);
const projects = await testFixtures.getProjectByQuery({
page: 1,
limit: 100,
});
expect(projects.data.length).to.equal(12);
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the projects filtered by orgUid', async function () {
// ?orgUid=XXXX
const projects = await testFixtures.getProjectByQuery({
orgUid: 'a807e453-6524-49df-a32d-785e56cf560e',
page: 1,
limit: 100,
});
expect(projects.length).to.equal(3);
expect(projects.data.length).to.equal(3);
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the projects for a search term', async function () {
// ?search=XXXX
const projects = await testFixtures.getProjectByQuery({
search: 'City of Arcata',
page: 1,
limit: 100,
});
expect(projects.length).to.equal(1);
expect(projects.data.length).to.equal(1);
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the projects for a search term filtered by orgUid', async function () {
// ?orgUid=XXXX&search=XXXX
const projects = await testFixtures.getProjectByQuery({
orgUid: 'a807e453-6524-49df-a32d-785e56cf560e',
search: 'City of Arcata',
page: 1,
limit: 100,
});

expect(projects.length).to.equal(1);
expect(projects.data.length).to.equal(1);
}).timeout(TEST_WAIT_TIME * 10);

it('gets optional paginated results', async function () {
Expand All @@ -99,7 +110,7 @@ describe('Project Resource CRUD', function () {
});

expect(projectsPage2.data.length).to.equal(3);
expect(projectsPage1.data).to.not.deep.equal(projectsPage2.data);
expect(projectsPage1).to.not.deep.equal(projectsPage2);

const projectsLimit2 = await testFixtures.getProjectByQuery({
page: 1,
Expand Down Expand Up @@ -222,12 +233,12 @@ describe('Project Resource CRUD', function () {
describe('error states', function () {
it('errors if no home organization exists', async function () {
const responseCreate = await supertest(app)
.get('/v1/projects')
.post('/v1/projects')
.send({
...newProject,
});

const warehouseProjectId = _.head(responseCreate.body);
const warehouseProjectId = responseCreate.body.uuid;

await Organization.destroy({
where: {},
Expand Down
58 changes: 37 additions & 21 deletions tests/resources/units.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ describe('Units Resource CRUD', function () {
await testFixtures.createNewUnit();
await testFixtures.commitStagingRecords();
await testFixtures.waitForDataLayerSync();
const result = await supertest(app).get('/v1/units');
response = result.body[0];
const result = await supertest(app)
.get('/v1/units')
.query({ page: 1, limit: 100 });
response = result.body.data[0];
});

afterEach(async function () {
Expand All @@ -44,17 +46,19 @@ describe('Units Resource CRUD', function () {

it('gets all the units available', async function () {
// no query params
const result = await supertest(app).get('/v1/units').query({});
const result = await supertest(app)
.get('/v1/units')
.query({ page: 1, limit: 100 });

expect(result.body.length).to.not.equal(0);
expect(result.body.data.length).to.not.equal(0);
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the units filtered by orgUid', async function () {
const result = await supertest(app)
.get('/v1/units')
.query({ orgUid: response.orgUid });
.query({ orgUid: response.orgUid, page: 1, limit: 100 });

expect(result.body.length).to.not.equal(1);
expect(result.body.data.length).to.not.equal(1);
// ?orgUid=XXXX
}).timeout(TEST_WAIT_TIME * 10);

Expand All @@ -73,11 +77,13 @@ describe('Units Resource CRUD', function () {

const result = await supertest(app)
.get('/v1/units')
.query({ order: 'SERIALNUMBER' });
.query({ order: 'SERIALNUMBER', page: 1, limit: 100 });

expect(result.body[0].serialNumberBlock).to.equal('AAAAA1-AAAAA2');
expect(result.body[1].serialNumberBlock).to.equal('AAAAA11-AAAAA21');
expect(result.body[2].serialNumberBlock).to.equal(
expect(result.body.data[0].serialNumberBlock).to.equal('AAAAA1-AAAAA2');
expect(result.body.data[1].serialNumberBlock).to.equal(
'AAAAA11-AAAAA21',
);
expect(result.body.data[2].serialNumberBlock).to.equal(
'AXJJFSLGHSHEJ1000-AXJJFSLGHSHEJ1010',
);
}).timeout(TEST_WAIT_TIME * 10);
Expand All @@ -86,33 +92,43 @@ describe('Units Resource CRUD', function () {
// ?search=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ search: 'Certification' });
.query({ search: 'Certification', page: 1, limit: 100 });

expect(result.body.length).to.not.equal(1);
expect(result.body.data.length).to.not.equal(1);
}).timeout(TEST_WAIT_TIME * 10);
it('gets all the units for a search term filtered by orgUid', async function () {
// ?orgUid=XXXX&search=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ orgUid: response.orgUid, search: 'Certification' });
const result = await supertest(app).get('/v1/units').query({
orgUid: response.orgUid,
search: 'Certification',
page: 1,
limit: 100,
});

expect(result.body.length).to.not.equal(1);
expect(result.body.data.length).to.not.equal(1);
}).timeout(TEST_WAIT_TIME * 10);
it('gets optional paginated results', async function () {
// ?page=X&limit=10
const result = await supertest(app)
.get('/v1/units')
.query({ page: 1, limit: 1 });

expect(result.body.length).to.not.equal(1);
expect(result.body.data.length).to.equal(1);
}).timeout(TEST_WAIT_TIME * 10);
it('finds a single result by warehouseUnitId', async function () {
// ?warehouseUnitId=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ warehouseUnitId: response.warehouseUnitId, limit: 1 });
const result = await supertest(app).get('/v1/units').query({
warehouseUnitId: response.warehouseUnitId,
});

// Check if data is an object
expect(result.body).to.be.an('object');

// Additionally, ensure it's not an array
expect(Array.isArray(result?.body?.data)).to.be.false;

expect(result.body.length).to.not.equal(1);
// Your existing assertion
expect(result.body?.data).to.not.equal(1);
}).timeout(TEST_WAIT_TIME * 10);
});
});
Expand Down
4 changes: 3 additions & 1 deletion tests/test-fixtures/project-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export const getProject = async (warehouseProjectId) => {
};

export const getProjectByQuery = async (query = {}) => {
const result = await supertest(app).get('/v1/projects').query(query);
const result = await supertest(app)
.get('/v1/projects')
.query({ page: 1, limit: 100, ...query });

// expect(result.body).to.be.an('array');
expect(result.statusCode).to.equal(200);
Expand Down
Loading