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