Skip to content

Commit 7aaa0f4

Browse files
authored
[O2B-1452] Add filter option by creation date interval for retrieving environments (#1931)
* Add API tests for expected filtering on created * Add tests for usecase on get all environments * Add functionality on filtering environments by creation date * Remove unwanted comment * Add tests for empty result case * Use convention for missing values
1 parent 7772bed commit 7aaa0f4

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

lib/domain/dtos/GetAllEnvironmentsDto.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
const Joi = require('joi');
1515
const PaginationDto = require('./PaginationDto');
16+
const { FromToFilterDto } = require('./filters/FromToFilterDto');
1617

1718
/**
1819
* Separate filter DTO for get all environments, because EnvironmentsFilterDto
@@ -23,6 +24,7 @@ const FilterDto = Joi.object({
2324
currentStatus: Joi.string().trim().optional(),
2425
statusHistory: Joi.string().trim().optional(),
2526
runNumbers: Joi.string().trim().optional(),
27+
created: FromToFilterDto,
2628
});
2729

2830
const QueryDto = Joi.object({

lib/usecases/environment/GetAllEnvironmentsUseCase.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,17 @@ class GetAllEnvironmentsUseCase {
8585
currentStatus: currentStatusExpression,
8686
statusHistory,
8787
runNumbers: runNumbersExpression,
88+
created,
8889
} = filter;
8990

9091
const filterQueryBuilder = prepareQueryBuilder();
9192

93+
if (created) {
94+
const from = created.from !== undefined ? created.from : 0;
95+
const to = created.to !== undefined ? created.to : Date.now();
96+
filterQueryBuilder.where('createdAt').between(from, to);
97+
}
98+
9299
if (idsExpression) {
93100
const filters = idsExpression.split(',').map((id) => id.trim());
94101

@@ -167,12 +174,18 @@ class GetAllEnvironmentsUseCase {
167174
}
168175

169176
const filteredEnvironmentsIds = (await EnvironmentRepository.findAll(filterQueryBuilder)).map(({ id }) => id);
177+
// If no environments match the filter, return an empty result
178+
if (filteredEnvironmentsIds.length === 0) {
179+
return {
180+
count: 0,
181+
environments: [],
182+
};
183+
}
170184
fetchQueryBuilder.where('id').oneOf(filteredEnvironmentsIds);
171185
}
172186

173187
fetchQueryBuilder.include({ association: 'runs' });
174188
fetchQueryBuilder.include({ association: 'historyItems' });
175-
176189
const { count, rows } = await EnvironmentRepository.findAndCountAll(fetchQueryBuilder);
177190
return {
178191
count,

test/api/environments.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,56 @@ module.exports = () => {
3636
expect(environments[0].id).to.equal('CmCvjNbg');
3737
});
3838

39+
it('should successfully return environments that were created between two provided dates', async () => {
40+
const from = new Date('2019-08-09 15:50:00').getTime();
41+
const to = new Date('2019-08-09 19:00:00').getTime();
42+
const response = await request(server).get(`/api/environments?filter[created][from]=${from}&filter[created][to]=${to}`);
43+
44+
expect(response.status).to.equal(200);
45+
const environments = response.body.data;
46+
expect(environments).to.lengthOf(2);
47+
expect(environments[0].id).to.equal('TDI59So3d');
48+
expect(environments[1].id).to.equal('EIDO13i3D');
49+
});
50+
51+
it('should successfully return environments that were created from provided date', async () => {
52+
const from = new Date('2019-08-09 15:50:00').getTime();
53+
const response = await request(server).get(`/api/environments?filter[created][from]=${from}`);
54+
55+
expect(response.status).to.equal(200);
56+
const environments = response.body.data;
57+
expect(environments).to.lengthOf(3);
58+
expect(environments[0].id).to.equal('CmCvjNbg');
59+
expect(environments[1].id).to.equal('TDI59So3d');
60+
expect(environments[2].id).to.equal('EIDO13i3D');
61+
});
62+
63+
it('should successfully return environments that were created to provided date', async () => {
64+
const to = new Date('2019-08-09 19:00:00').getTime();
65+
const response = await request(server).get(`/api/environments?filter[created][to]=${to}`);
66+
67+
expect(response.status).to.equal(200);
68+
const environments = response.body.data;
69+
expect(environments).to.lengthOf(8);
70+
expect(environments[0].id).to.equal('TDI59So3d');
71+
expect(environments[1].id).to.equal('EIDO13i3D');
72+
expect(environments[2].id).to.equal('KGIS12DS');
73+
expect(environments[3].id).to.equal('VODdsO12d');
74+
expect(environments[4].id).to.equal('GIDO1jdkD');
75+
expect(environments[5].id).to.equal('8E4aZTjY');
76+
expect(environments[6].id).to.equal('Dxi029djX');
77+
expect(environments[7].id).to.equal('eZF99lH6');
78+
});
79+
80+
it('should successfully return no environments for filter not matching', async () => {
81+
const from = Date.now();
82+
const response = await request(server).get(`/api/environments?filter[created][from]=${from}`);
83+
84+
expect(response.status).to.equal(200);
85+
const environments = response.body.data;
86+
expect(environments).to.lengthOf(0);
87+
});
88+
3989
it('should successfully apply filter for environments ids', async () => {
4090
const response = await request(server).get('/api/environments?filter[ids]=8E4aZTjY');
4191

test/lib/usecases/environment/GetAllEnvironmentsUseCase.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,41 @@ module.exports = () => {
161161
// Should include all environments with run numbers containing the substring 10
162162
expect(environments.map(({ id }) => id)).to.have.members(['TDI59So3d', 'Dxi029djX']);
163163
});
164+
165+
it('should successfully filter environments on created from and to', async () => {
166+
const from = Date.now() - 24 * 60 * 60 * 1000; // environment from 24h ago which was created by CreateEnvironmentUseCase.test.js
167+
const to = Date.now() - 10;
168+
getAllEnvsDto.query = { filter: { created: { from, to } } };
169+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
170+
expect(environments).to.be.an('array');
171+
expect(environments.length).to.be.equal(2);
172+
expect(environments[0].id).to.be.equal('newId');
173+
expect(environments[1].id).to.be.equal('SomeId');
174+
});
175+
176+
it('should successfully filter environments on created from', async () => {
177+
const from = Date.now() - 24 * 60 * 60 * 1000; // environment from 24h ago which was created by CreateEnvironmentUseCase.test.js
178+
getAllEnvsDto.query = { filter: { created: { from } } };
179+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
180+
expect(environments).to.be.an('array');
181+
expect(environments.length).to.be.equal(2);
182+
expect(environments[0].id).to.be.equal('newId');
183+
expect(environments[1].id).to.be.equal('SomeId');
184+
});
185+
186+
it('should successfully filter environments on created to', async () => {
187+
const to = Date.now() - 24 * 60 * 60 * 1000; // environment until 24h are created by seeders
188+
getAllEnvsDto.query = { filter: { created: { to } } };
189+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
190+
expect(environments).to.be.an('array');
191+
expect(environments.length).to.be.equal(9); // Environments from seeders
192+
});
193+
194+
it('should successfully return empty environment array if filter does not match', async () => {
195+
const from = Date.now() - 10;
196+
getAllEnvsDto.query = { filter: { created: { from } } };
197+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
198+
expect(environments).to.be.an('array');
199+
expect(environments.length).to.be.equal(0); // Environments from seeders
200+
});
164201
};

0 commit comments

Comments
 (0)