feat(spec): refs #6005 add spec to backup Notify
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Pablo Natek 2024-03-11 12:18:03 +01:00
parent 8529bed895
commit 8815f07850
3 changed files with 129 additions and 28 deletions

View File

@ -45,7 +45,6 @@ module.exports = Self => {
});
availableNotificationsMap.delete(active.notificationFk);
}
console.log(activeNotificationsMap);
return {
active: [...activeNotificationsMap.entries()],
available: [...availableNotificationsMap.entries()]

View File

@ -1,6 +1,38 @@
const models = require('vn-loopback/server/server').models;
describe('Notification Send()', () => {
beforeAll(async() => {
await models.NotificationQueue.destroyAll();
await models.NotificationQueue.create(
[
{
id: 1,
params: '{"id": "1"}',
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: 9
},
{
id: 2,
params: '{"id": "2"}',
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: null
},
{
id: 3,
params: null,
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: null
}
]
);
});
it('should send notification', async() => {
const statusPending = 'pending';
const tx = await models.NotificationQueue.beginTransaction({});

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models;
fdescribe('Operator', () => {
describe('Operator', () => {
const authorFk = 9;
const sectorId = 1;
const labeler = 1;
@ -13,6 +13,8 @@ fdescribe('Operator', () => {
sectorFk: sectorId
};
const errorStatus = 'error';
async function createOperator(labelerFk, options) {
operator.labelerFk = labelerFk;
await models.Operator.create(operator, options);
@ -62,31 +64,6 @@ fdescribe('Operator', () => {
}
});
fit('should not create notification when is already notified by another worker', async() => {
const tx = await models.Operator.beginTransaction({});
try {
const options = {transaction: tx, accessToken: {userId: authorFk}};
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 10}),
created: Date.vnNow(),
}, options);
await createOperator(labeler, options);
await models.Notification.send(options);
const lastNotification = await models.NotificationQueue.find({order: 'id DESC'}, options);
console.log('lastNotification: ', lastNotification);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should create notification when delay is null', async() => {
const tx = await models.Operator.beginTransaction({});
@ -96,7 +73,7 @@ fdescribe('Operator', () => {
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 10}),
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: Date.vnNow(),
}, options);
@ -118,4 +95,97 @@ fdescribe('Operator', () => {
throw e;
}
});
it('should not sent notification when is already notified by another worker', async() => {
await models.NotificationQueue.create({
authorFk: 2,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}),
created: '2001-01-01 12:30:00',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual(errorStatus);
});
it('should send a notification when the previous one is on errorStatus status', async() => {
await models.NotificationQueue.create({
authorFk: 2,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}),
created: '2001-01-01 12:30:00',
status: errorStatus
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('sent');
});
it('should send a notification when the previous one has distinct params', async() => {
await models.NotificationQueue.create({
authorFk: 2,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': 2, 'workerId': 1}),
created: '2001-01-01 12:30:00',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('sent');
});
it('should respect de configured delay for the notification', async() => {
await models.NotificationQueue.create({
authorFk: 2,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}),
created: '2001-01-01 12:30:00',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 13:29:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('error');
});
});