-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-calendar-validation.js
135 lines (109 loc) · 6.1 KB
/
json-calendar-validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
const _ = require('lodash');
const fs = require('fs');
const jetpack = require('fs-jetpack');
const jsonFolder = './json';
const assert = require('chai').assert;
// Valid event summary values to check against
const validSummaryTitles = [
'Black Bin Day',
'Green Bin Day',
'Green Bin + Glass Box Day',
'Garden Waste Collection',
'Black Bin Day (Changed Collection)',
'Green Bin Day (Changed Collection)',
'Green Bin + Glass Box Day (Changed Collection)',
'Garden Waste Collection (Changed Collection)',
];
let jsonData = [];
describe('Directory test', function() {
it('Check to see if JSON folder exists', function() {
assert.isOk(jetpack.exists(jsonFolder));
});
});
// Get all JSON data to validate
fs.readdirSync(jsonFolder).forEach(file => {
const filepath = `${jsonFolder}/${file}`;
const size = jetpack.inspect(filepath).size / 1024;
describe('JSON filesize', function() {
it(`Check filesize of ${file} is below 14 KB`, function() {
assert.isAtMost(parseFloat(size.toFixed(2)), 14, 'JSON filesize exceeds 14 KB.');
});
});
let jsonFile = jetpack.read(filepath, 'json');
jsonData.push(jsonFile);
});
// Total calendars should be 30. 20 refuse and 10 garden calendars
describe('JSON files output check', function() {
it('Check correct amount of JSON files have been written.', function() {
assert.strictEqual(jsonData.length, 30);
});
});
// Validate the data present in each calendar
describe('JSON calendar validation', function() {
jsonData.forEach((calendar) => {
let calendarType = calendar.collectionType;
let isGardenCollectionType = calendarType === 'Garden';
// Total collection dates should always be 21 for garden bin but can be 52 or 53 for refuse
let expectedCollectionsMin = isGardenCollectionType ? 21 : 52;
// Maximum collection dates is always 21, but could be 53 for some collection calendars
let expectedCollectionsMax = isGardenCollectionType ? expectedCollectionsMin : expectedCollectionsMin + 1;
// Minimum collection dates in a month is 1 for garden bin but 4 for refuse
let expectedCollectionsInMonthMin = isGardenCollectionType ? 1 : 4;
// Maximum collection dates in a month is 3 for garden bin but 5 for refuse
let expectedCollectionsInMonthMax = isGardenCollectionType ? expectedCollectionsInMonthMin + 2 : expectedCollectionsInMonthMin + 1;
let collectionDates = calendar.collectionDates;
let totalCalendarCollections = calendar.totalCollections;
// Check for calendar name value
it(`${calendar.filename} name is not null`, function() {
assert.isNotNull(calendar.name, 'Calendar is missing name value.');
});
// Check for calendar description value
it(`${calendar.name} description is not null`, function() {
assert.isNotNull(calendar.description, 'Calendar is missing description value.');
});
// Check the total amount of collection dates does not exceed the expected minimum
it(`${calendar.name} total ${calendarType.toLowerCase()} collection dates matches minimum expected count`, function() {
assert.isAtLeast(totalCalendarCollections, expectedCollectionsMin, 'Total collection dates is lower than the expected minimum.');
});
// Check the total amount of collections dates does not exceed the expected maximum
it(`${calendar.name} total ${calendarType.toLowerCase()} collection dates does not exceed maximum`, function() {
assert.isAtMost(totalCalendarCollections, expectedCollectionsMax, 'Total collection dates exceeds expected count.');
});
// Validate collection dates
describe(`${calendar.name} collection dates validation`, function() {
let collectionDatesByMonth = _.groupBy(collectionDates, (item) => item.collectionDate.substring(0, 7));
for (const month in collectionDatesByMonth) {
const datesInMonth = collectionDatesByMonth[month];
it(`${calendar.name} check ${month} contains minimum amount of dates expected`, function() {
assert.isAtLeast(datesInMonth.length, expectedCollectionsInMonthMin, `${month} did not contain minimum amount of collections expected.`);
});
it(`${calendar.name} check ${month} contains maximum amount of dates expected`, function() {
assert.isAtMost(datesInMonth.length, expectedCollectionsInMonthMax, `${month} contains more than the maximum amount of collections expected.`);
});
}
collectionDates.forEach((item) => {
// Check summary value to ensure consistency
it(`${calendar.name} check collection date: ${item.collectionDate} summary value is valid`, function() {
assert.include(validSummaryTitles, item.name, 'An invalid summary value has been used.');
});
// Check any changed collection is a single occurrence, not part of a repeating schedule
it(`${calendar.name} check collection date: ${item.collectionDate} for occurrence mismatch`, function() {
assert.isNotTrue(item.isOccurrence && item.isChangedCollection, 'A single occurrence is part of a recurring rule.');
});
// Ensure the weekday only differs when the collection is a changed collection date
it(`${calendar.name} check collection date: ${item.collectionDate} for weekday conflict`, function() {
assert.isNotTrue(calendar.weekday === item.weekday && item.isChangedCollection, 'The same weekday is set for an event which is a changed collection.');
});
// Ensure type key is not "Unknown"
it(`${calendar.name} check collection date: ${item.collectionDate} type is not unknown`, function() {
assert.notStrictEqual(item.type, 'Unknown', 'Bin type collection key is "Unknown".');
});
// Ensure any single event is a changed collection
it(`${calendar.name} check collection date: ${item.collectionDate} has changed collection identifier in summary value if not an occurrence.`, function() {
assert.isNotTrue(item.isChangedCollection && !item.name.includes('Changed Collection'), 'A single event does not identify as a changed collection in the summary value.');
});
});
});
});
});