test: add unit tests to pipeline

This commit is contained in:
2023-05-31 15:33:19 +02:00
parent 45444d9373
commit 495a63977c
29 changed files with 232 additions and 484 deletions

View File

@@ -13,47 +13,49 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThings, SCThingType} from '@openstapps/core';
import {Validator} from '@openstapps/core-tools/lib/validate';
import {Validator} from '@openstapps/core-tools';
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {join} from 'path';
import path from 'path';
import {MinimalConnector} from '../src/minimal-connector.js';
import * as url from 'url';
@suite
export class MinimalConnectorSpec {
private static connector: MinimalConnector;
private static validator: Validator;
/**
* Gets the `SCThingType`-key as string
*
* @param instance Contains `type` with the value of a SCThingType-key
*/
function getSchemaNameFromType<T extends SCThings>(instance: T): string {
const type = instance.type;
const index = Object.values(SCThingType).indexOf(type);
const key = Object.keys(SCThingType)[index];
return `SC${key}`;
}
static async before() {
this.validator = new Validator();
await this.validator.addSchemas(
join(__dirname, '..', 'node_modules', '@openstapps', 'core', 'lib', 'schema'),
describe('minimal-connector', function () {
let connector: MinimalConnector;
let validator: Validator;
beforeEach(async function () {
validator = new Validator();
await validator.addSchemas(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
'..',
'node_modules',
'@openstapps',
'core',
'lib',
'schema',
),
);
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
connector = new MinimalConnector('f-u', 'minimal-connector');
});
/**
* Gets the `SCThingType`-key as string
*
* @param instance Contains `type` with the value of a SCThingType-key
*/
static getSchemaNameFromType<T extends SCThings>(instance: T): string {
const type = instance.type;
const index = Object.values(SCThingType).indexOf(type);
const key = Object.keys(SCThingType)[index];
return `SC${key}`;
}
/**
* Checks, if the items are valid
*
* @param things Items fetched by the connector
*/
static validateThings<T extends SCThings>(things: T[]) {
things.forEach((thing: T) => {
const schemaName = this.getSchemaNameFromType<T>(thing);
it('should get sample things', async function () {
for (const thing of await connector.getItems()) {
const schemaName = getSchemaNameFromType(thing);
// validate thing
const validatorResult = MinimalConnectorSpec.validator.validate(thing, schemaName);
const validatorResult = validator.validate(thing, schemaName);
expect(validatorResult.errors).to.have.lengthOf(
0,
JSON.stringify({
@@ -61,15 +63,6 @@ export class MinimalConnectorSpec {
thing: thing,
}),
);
});
}
@test
getSampleThings() {
return MinimalConnectorSpec.connector.getItems().then(<T extends SCThings>(items: T[]) => {
if (items.length > 0) {
MinimalConnectorSpec.validateThings(items);
}
});
}
}
}
});
});