refactor(backend): Streamlining van de testdata voor leerpaden en leerobjecten + integratie in seed

Hierbij ook testdata functionaliteit toegevoegd om makkelijk nieuwe leerpaden aan te maken.
This commit is contained in:
Gerald Schmittinger 2025-04-15 23:43:30 +02:00
parent 4092f1f617
commit 202cf4e33c
32 changed files with 691 additions and 493 deletions

View file

@ -11,52 +11,45 @@ const IGNORE_PROPERTIES = ['parent'];
* Checks if the actual entity from the database conforms to the entity that was added previously.
* @param actual The actual entity retrieved from the database
* @param expected The (previously added) entity we would expect to retrieve
* @param propertyPrefix Prefix to append to property in error messages.
*/
export function expectToBeCorrectEntity<T extends object>(actual: { entity: T; name?: string }, expected: { entity: T; name?: string }): void {
if (!actual.name) {
actual.name = 'actual';
}
if (!expected.name) {
expected.name = 'expected';
}
for (const property in expected.entity) {
export function expectToBeCorrectEntity<T extends object>(
actual: T,
expected: T,
propertyPrefix: string = ""
): void {
for (const property in expected) {
const prefixedProperty = propertyPrefix + property;
if (
property in IGNORE_PROPERTIES &&
expected.entity[property] !== undefined && // If we don't expect a certain value for a property, we assume it can be filled in by the database however it wants.
typeof expected.entity[property] !== 'function' // Functions obviously are not persisted via the database
expected[property] !== undefined && // If we don't expect a certain value for a property, we assume it can be filled in by the database however it wants.
typeof expected[property] !== 'function' // Functions obviously are not persisted via the database
) {
if (!Object.prototype.hasOwnProperty.call(actual.entity, property)) {
if (!Object.prototype.hasOwnProperty.call(actual, property)) {
throw new AssertionError({
message: `${expected.name} has defined property ${property}, but ${actual.name} is missing it.`,
message: `Expected property ${prefixedProperty}, but it is missing.`,
});
}
if (typeof expected.entity[property] === 'boolean') {
if (typeof expected[property] === 'boolean') {
// Sometimes, booleans get represented by numbers 0 and 1 in the objects actual from the database.
if (Boolean(expected.entity[property]) !== Boolean(actual.entity[property])) {
if (Boolean(expected[property]) !== Boolean(actual[property])) {
throw new AssertionError({
message: `${property} was ${expected.entity[property]} in ${expected.name},
but ${actual.entity[property]} (${Boolean(expected.entity[property])}) in ${actual.name}`,
message: `Expected ${prefixedProperty} to be ${expected[property]},
but was ${actual[property]} (${Boolean(expected[property])}).`,
});
}
} else if (typeof expected.entity[property] !== typeof actual.entity[property]) {
} else if (typeof expected[property] !== typeof actual[property]) {
throw new AssertionError({
message: `${property} has type ${typeof expected.entity[property]} in ${expected.name}, but type ${typeof actual.entity[property]} in ${actual.name}.`,
message: `${prefixedProperty} was expected to have type ${typeof expected[property]},`
+ `but had type ${typeof actual[property]}.`,
});
} else if (typeof expected.entity[property] === 'object') {
expectToBeCorrectEntity(
{
name: actual.name + '.' + property,
entity: actual.entity[property] as object,
},
{
name: expected.name + '.' + property,
entity: expected.entity[property] as object,
}
);
} else if (typeof expected[property] === 'object') {
expectToBeCorrectEntity(actual[property] as object, expected[property] as object, property);
} else {
if (expected.entity[property] !== actual.entity[property]) {
if (expected[property] !== actual[property]) {
throw new AssertionError({
message: `${property} was ${expected.entity[property]} in ${expected.name}, but ${actual.entity[property]} in ${actual.name}`,
message: `${prefixedProperty} was expected to be ${expected[property]}, `
+ `but was ${actual[property]}.`,
});
}
}

View file

@ -6,5 +6,5 @@ import path from 'node:path';
* @param relPath Path of the asset relative to the test-assets folder.
*/
export function loadTestAsset(relPath: string): Buffer {
return fs.readFileSync(path.resolve(__dirname, `../test-assets/${relPath}`));
return fs.readFileSync(path.resolve(__dirname, `../test_assets/${relPath}`));
}