Add Schedule plurality checks

This commit is contained in:
ingalls
2024-06-18 09:26:33 -06:00
parent bba6b8795c
commit cc37155734
2 changed files with 46 additions and 101 deletions

View File

@@ -33,6 +33,13 @@ export default class Schedule {
if (!['second', 'minute', 'hour', 'day'].includes(unit)) throw new Err(400, null, 'Unknown Unit in Rate');
// AWS went to grammar school and rate(1 minutes) won't be accepted
if (freq === 1 && schedules[1].match(/s$/)) {
throw new Err(400, null, 'A frequency value of 1 cannot have a plural unit');
} else if (freq > 1 && !schedules[1].match(/s$/)) {
throw new Err(400, null, 'A frequency value of >1 must have a plural unit');
}
return { unit, freq };
}

View File

@@ -1,112 +1,50 @@
import test from 'tape';
import test from 'node:test';
import assert from 'node:assert';
import Schedule from '../lib/schedule.js';
test('Schedule#is_rate', async (t) => {
try {
t.notOk(Schedule.is_rate('cron(15 10 * * ? *)'));
t.notOk(Schedule.is_rate('cron(0 18 ? * MON-FRI *)'));
t.notOk(Schedule.is_rate('cron(0 8 1 * ? *)'));
t.notOk(Schedule.is_rate('cron(0/10 * ? * MON-FRI *)'));
t.notOk(Schedule.is_rate('cron(0/5 8-17 ? * MON-FRI *)'));
t.notOk(Schedule.is_rate('cron(0 9 ? * 2#1 *)'));
t.ok(Schedule.is_rate('rate(5 minutes)'));
t.ok(Schedule.is_rate('rate(1 hour)'));
t.ok(Schedule.is_rate('rate(7 days)'));
} catch (err) {
t.error(err);
}
t.end();
// Tests if the intent is Rate - not if the rate is valid
test('Schedule.is_rate()', async () => {
assert.ok(Schedule.is_rate('rate(1 hour)'))
assert.ok(Schedule.is_rate('rate(2 hour)'))
assert.ok(!Schedule.is_rate('cron(2 hour)'))
});
test('Schedule#is_cron', async (t) => {
try {
t.ok(Schedule.is_cron('cron(15 10 * * ? *)'));
t.ok(Schedule.is_cron('cron(0 18 ? * MON-FRI *)'));
t.ok(Schedule.is_cron('cron(0 8 1 * ? *)'));
t.ok(Schedule.is_cron('cron(0/10 * ? * MON-FRI *)'));
t.ok(Schedule.is_cron('cron(0/5 8-17 ? * MON-FRI *)'));
t.ok(Schedule.is_cron('cron(0 9 ? * 2#1 *)'));
t.notOk(Schedule.is_cron('rate(5 minutes)'));
t.notOk(Schedule.is_cron('rate(1 hour)'));
t.notOk(Schedule.is_cron('rate(7 days)'));
} catch (err) {
t.error(err);
}
t.end();
// Tests if the intent is cron - not if the cron is valid
test('Schedule.is_cron()', async () => {
assert.ok(Schedule.is_cron('cron(1 hour)'))
assert.ok(!Schedule.is_cron('rate(1 hour)'))
});
test('Schedule#parse_rate', async (t) => {
try {
t.ok(Schedule.parse_rate('cron(15 10 * * ? *)'));
t.fail();
} catch (err) {
t.equals(err.message, 'Schedule is not a rate');
}
test('Schedule.parse_rate()', async () => {
assert.throws(() => {
Schedule.parse_rate('rate(1 seconds)');
}, /A frequency value of 1/);
try {
t.deepEquals(Schedule.parse_rate('rate(1 second)'), {
unit: 'second',
freq: 1
});
t.deepEquals(Schedule.parse_rate('rate(2 seconds)'), {
unit: 'second',
freq: 2
});
assert.throws(() => {
Schedule.parse_rate('rate(1 minutes)');
}, /A frequency value of 1/);
t.deepEquals(Schedule.parse_rate('rate(1 minute)'), {
unit: 'minute',
freq: 1
});
t.deepEquals(Schedule.parse_rate('rate(2 minutes)'), {
unit: 'minute',
freq: 2
});
assert.throws(() => {
Schedule.parse_rate('rate(1 hours)');
}, /A frequency value of 1/);
t.deepEquals(Schedule.parse_rate('rate(1 hour)'), {
unit: 'hour',
freq: 1
});
t.deepEquals(Schedule.parse_rate('rate(2 hour)'), {
unit: 'hour',
freq: 2
});
assert.throws(() => {
Schedule.parse_rate('rate(1 days)');
}, /A frequency value of 1/);
t.deepEquals(Schedule.parse_rate('rate(1 day)'), {
unit: 'day',
freq: 1
});
t.deepEquals(Schedule.parse_rate('rate(2 day)'), {
unit: 'day',
freq: 2
});
} catch (err) {
t.error(err);
}
t.end();
});
test('Schedule#is_aws', async (t) => {
try {
t.ok(Schedule.is_aws('cron(15 10 * * ? *)'));
t.ok(Schedule.is_aws('cron(0 18 ? * MON-FRI *)'));
t.ok(Schedule.is_aws('cron(0 8 1 * ? *)'));
t.ok(Schedule.is_aws('cron(0/10 * ? * MON-FRI *)'));
t.ok(Schedule.is_aws('cron(0/5 8-17 ? * MON-FRI *)'));
t.ok(Schedule.is_aws('cron(0 9 ? * 2#1 *)'));
t.ok(Schedule.is_aws('rate(5 minutes)'));
t.ok(Schedule.is_aws('rate(1 hour)'));
t.ok(Schedule.is_aws('rate(7 days)'));
t.notOk(Schedule.is_aws('rate(1 second)'));
t.notOk(Schedule.is_aws('rate(2 seconds)'));
} catch (err) {
t.error(err);
}
t.end();
assert.throws(() => {
Schedule.parse_rate('rate(2 second)');
}, /A frequency value of >1/);
assert.throws(() => {
Schedule.parse_rate('rate(2 minute)');
}, /A frequency value of >1/);
assert.throws(() => {
Schedule.parse_rate('rate(2 hour)');
}, /A frequency value of >1/);
assert.throws(() => {
Schedule.parse_rate('rate(2 day)');
}, /A frequency value of >1/);
});