-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest-json-schema.js
47 lines (38 loc) · 1.28 KB
/
test-json-schema.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
const fs = require("fs");
const path = require("path");
const assert = require("assert").strict;
const Ajv = require("ajv");
const ajv = new Ajv();
const schema = JSON.parse(fs.readFileSync("./schema/league.json"));
const validate = ajv.compile(schema);
const testFileIsComplete = (data, filePath) => {
const teamsCount = filePath.includes("germany") ? 18 : 20;
const expectedRounds = 2 * (teamsCount - 1);
const expectedGames = teamsCount / 2;
data.forEach(season => {
assert.equal(
season.rounds.length,
expectedRounds,
`Wrong rounds count in ${filePath}, season ${season.season}`
);
season.rounds.forEach(round => {
assert.equal(
round.games.length,
expectedGames,
`Wrong games count in ${filePath}, season ${season.season}, round ${round.round}`
);
});
});
};
const testDataFile = filePath => {
const json = fs.readFileSync(filePath);
const data = JSON.parse(json);
const valid = validate(data);
assert(valid, JSON.stringify(validate.errors));
testFileIsComplete(data, filePath);
console.log(`${filePath} -> Passed`);
};
const dirPath = "./data/json";
const fileNames = fs.readdirSync(dirPath);
assert.notEqual(fileNames.length, 0, `No files found in directory ${dirPath}.`);
fileNames.map(fileName => path.join(dirPath, fileName)).forEach(testDataFile);