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

@@ -16,11 +16,11 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "tsup --dts",
"format": "prettier . --ignore-path ../../.gitignore",
"format": "prettier . -c --ignore-path ../../.gitignore",
"format:fix": "prettier --write . --ignore-path ../../.gitignore",
"lint": "eslint --ext .ts src/",
"lint:fix": "eslint --fix --ext .ts src/",
"test": "nyc mocha 'test/**/*.spec.ts'"
"test": "c8 mocha"
},
"dependencies": {
"@openstapps/api": "workspace:*",
@@ -31,20 +31,18 @@
"devDependencies": {
"@openstapps/core-tools": "workspace:*",
"@openstapps/eslint-config": "workspace:*",
"@openstapps/nyc-config": "workspace:*",
"@openstapps/prettier-config": "workspace:*",
"@openstapps/tsconfig": "workspace:*",
"@testdeck/mocha": "0.3.3",
"@types/chai": "4.3.5",
"@types/chai-as-promised": "7.1.5",
"@types/mocha": "10.0.1",
"@types/node": "18.15.3",
"c8": "7.14.0",
"chai": "4.3.7",
"chai-as-promised": "7.1.1",
"conventional-changelog-cli": "2.2.2",
"mocha": "10.2.0",
"nock": "13.3.1",
"nyc": "15.1.0",
"ts-node": "10.9.1",
"tsup": "6.7.0",
"typescript": "4.8.4"
@@ -63,8 +61,5 @@
"extends": [
"@openstapps"
]
},
"nyc": {
"extends": "@openstapps/nyc-config"
}
}

View File

@@ -13,42 +13,41 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThingType, SCBulkResponse, SCLicensePlate} from '@openstapps/core';
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import chai, {expect} from 'chai';
import {MinimalConnector} from '../src/minimal-connector.js';
import {createUUID, executeConnector, isValidSCNamespace} from '../src/common.js';
import nock = require('nock');
import chaiAsPromised from 'chai-as-promised';
@suite
export class CommonSpec {
private static connector: MinimalConnector;
chai.use(chaiAsPromised);
static async before() {
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
describe('common', function () {
let connector: MinimalConnector;
@test
public isValidSCNamespace() {
const existingAddedSCLIcensePlate: SCLicensePlate = 'f-u';
const notASCLicensePlate: string = 'NOT-A-LICENSE';
const existingButUnaddedLicensePlate: SCLicensePlate = 'a-fh';
expect(isValidSCNamespace(existingAddedSCLIcensePlate)).to.be.equal(true);
beforeEach(function () {
connector = new MinimalConnector('f-u', 'minimal-connector');
});
it('should be a valid SCNamespace', function () {
const existingAddedSCLicensePlate: SCLicensePlate = 'f-u';
const notASCLicensePlate = 'NOT-A-LICENSE';
const existingButNotYetAddedLicensePlate: SCLicensePlate = 'a-fh';
expect(isValidSCNamespace(existingAddedSCLicensePlate)).to.be.equal(true);
expect(isValidSCNamespace(notASCLicensePlate)).to.be.equal(false);
expect(isValidSCNamespace(existingButUnaddedLicensePlate)).to.be.equal(false);
}
expect(isValidSCNamespace(existingButNotYetAddedLicensePlate)).to.be.equal(false);
});
@test
testCreateUUID() {
it('should create a uuid', function () {
const item = {
type: 'Not even a thing',
};
expect(createUUID(item, 'f-u')).to.equal('3ac2b548-75d3-5326-920a-241e514fe445');
// ID was generated once before!
}
});
@test
async testExecuteConnector() {
const source = CommonSpec.connector.origin;
it('should exectute the connector', async function () {
const source = connector.origin;
const backendUrl = 'http://localhost:3000';
const bulkOpen: SCBulkResponse = {
source: source,
@@ -57,33 +56,18 @@ export class CommonSpec {
uid: '744321ca-cc95-4967-b8df-42c98b792db6',
};
nock('http://localhost:3000').post('/bulk').reply(200, bulkOpen);
nock(backendUrl).post('/bulk').reply(200, bulkOpen);
const nockBulkAdd = nock('http://localhost:3000')
.post('/bulk/744321ca-cc95-4967-b8df-42c98b792db6')
.reply(201, {})
.persist(); // otherwise consumed!
const nockBulkAdd = nock(backendUrl).post(`/bulk/${bulkOpen.uid}`).reply(201, {}).persist(); // otherwise consumed!
nock('http://localhost:3000').post('/bulk/744321ca-cc95-4967-b8df-42c98b792db6/done').reply(204, {});
nock(backendUrl).post(`/bulk/${bulkOpen.uid}/done`).reply(204, {});
// should succeed
expect(await CommonSpec.runExecuteConnector()).to.equal(true);
expect(executeConnector(backendUrl, connector)).not.to.be.rejected;
nockBulkAdd.persist(false);
// should fail due to nockBulkAdd being consumed
expect(await CommonSpec.runExecuteConnector()).to.equal(false);
}
/**
* Executes the connector
*/
static async runExecuteConnector(): Promise<boolean> {
try {
await executeConnector('http://localhost:3000', CommonSpec.connector);
return true;
} catch (err) {
return false;
}
}
}
expect(executeConnector(backendUrl, connector)).to.be.rejected;
});
});

View File

@@ -14,33 +14,29 @@
*/
import {SCThingOriginType} from '@openstapps/core';
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import {MinimalConnector} from '../src/minimal-connector.js';
@suite
export class ConnectorSpec {
private static connector: MinimalConnector;
describe('connector', function () {
let connector: MinimalConnector;
static async before() {
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
beforeEach(function () {
connector = new MinimalConnector('f-u', 'minimal-connector');
});
@test
testCreateRemoteOrigin() {
const remoteOrigin = ConnectorSpec.connector.createRemoteOrigin();
expect(remoteOrigin.name).to.equal(ConnectorSpec.connector.origin);
it('should create remote origin', function () {
const remoteOrigin = connector.createRemoteOrigin();
expect(remoteOrigin.name).to.equal(connector.origin);
expect(remoteOrigin.type).to.equal(SCThingOriginType.Remote);
expect(new Date().valueOf()).to.be.at.least(Date.parse(remoteOrigin.indexed).valueOf());
}
expect(Date.now()).to.be.at.least(Date.parse(remoteOrigin.indexed).valueOf());
});
@test
async testAutomaticMissingUIDGeneration() {
it('should automatically generate missing UIDs', async function () {
const uuidRegExp = new RegExp(
'^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$',
);
const messages = await ConnectorSpec.connector.getItems();
const messages = await connector.getItems();
for (const message of messages) {
expect(message.uid).to.match(uuidRegExp);
}
}
}
});
});

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);
}
});
}
}
}
});
});