tests: Add tests and some other changes
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
This commit is contained in:
261
tests/test.js
Normal file
261
tests/test.js
Normal file
@@ -0,0 +1,261 @@
|
||||
import fs from "fs"
|
||||
import parseAbsence from "../scrape/utils/parseAbsence.js";
|
||||
|
||||
const teachermap = JSON.parse(fs.readFileSync("./teachermap.json"))
|
||||
|
||||
|
||||
test("Me", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
}
|
||||
]);
|
||||
|
||||
test("ad", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
}
|
||||
]);
|
||||
|
||||
test("me ad", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
}
|
||||
]);
|
||||
|
||||
test("me 3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "single",
|
||||
hours: 3
|
||||
}
|
||||
]);
|
||||
|
||||
test("ad 1", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "single",
|
||||
hours: 1
|
||||
}
|
||||
]);
|
||||
|
||||
test("me 2-4", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 2, to: 4 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("ad 5,6", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 5, to: 6 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("me 7+", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 7, to: 10 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("me,ad 3-5", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 3, to: 5 }
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 3, to: 5 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("me;ad 4", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "single",
|
||||
hours: 4
|
||||
}
|
||||
]);
|
||||
|
||||
test("me;ad;bo 2-3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Ing. Anna Bodnárová",
|
||||
teacherCode: "bo",
|
||||
type: "range",
|
||||
hours: { from: 2, to: 3 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("me 2 ad 4-5", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "single",
|
||||
hours: 2
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 4, to: 5 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("3", [
|
||||
{
|
||||
type: "invalid",
|
||||
teacher: null,
|
||||
teacherCode: null,
|
||||
hours: null,
|
||||
original: "3"
|
||||
}
|
||||
]);
|
||||
|
||||
test("2-4", [
|
||||
{
|
||||
type: "invalid",
|
||||
teacher: null,
|
||||
teacherCode: null,
|
||||
hours: null,
|
||||
original: "2-4"
|
||||
}
|
||||
]);
|
||||
|
||||
test("me Xx 3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{ teacher: null, teacherCode: 'xx', type: 'single', hours: 3 },
|
||||
]);
|
||||
|
||||
test("me,ad;bo 1-2 ad 5+", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null
|
||||
},
|
||||
{
|
||||
teacher: "Ing. Anna Bodnárová",
|
||||
teacherCode: "bo",
|
||||
type: "range",
|
||||
hours: { from: 1, to: 2 }
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 5, to: 10 }
|
||||
}
|
||||
]);
|
||||
|
||||
test("Me-exk", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "exkurze",
|
||||
hours: null
|
||||
}
|
||||
])
|
||||
|
||||
function test(input, expectedOutput) {
|
||||
const res = parseAbsence(input, teachermap);
|
||||
|
||||
if (!deepEqual(res, expectedOutput)) {
|
||||
console.error("ERROR for input: " + input);
|
||||
console.log(res);
|
||||
console.log(expectedOutput);
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
function deepEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
|
||||
if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle arrays (ignore order)
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
const used = new Array(b.length).fill(false);
|
||||
|
||||
return a.every(itemA =>
|
||||
b.some((itemB, i) => {
|
||||
if (used[i]) return false; // don't reuse elements
|
||||
if (deepEqual(itemA, itemB)) {
|
||||
used[i] = true; // mark element as used
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
||||
|
||||
const keysA = Object.keys(a);
|
||||
const keysB = Object.keys(b);
|
||||
|
||||
if (keysA.length !== keysB.length) return false;
|
||||
|
||||
return keysA.every(key => keysB.includes(key) && deepEqual(a[key], b[key]));
|
||||
}
|
||||
Reference in New Issue
Block a user