feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -15,9 +15,9 @@
*/
import {Logger} from '@openstapps/logger';
import {expect} from 'chai';
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {cwd} from 'process';
import {getTsconfigPath} from '../src/common.js';
import path from 'path';
import {fileURLToPath} from 'url';
process.on('unhandledRejection', (reason: unknown): void => {
if (reason instanceof Error) {
@@ -26,10 +26,10 @@ process.on('unhandledRejection', (reason: unknown): void => {
process.exit(1);
});
@suite(timeout(20_000), slow(10_000))
export class CommonSpec {
@test
async getTsconfigPath() {
expect(getTsconfigPath(__dirname)).to.be.equal(cwd());
}
}
describe('common', function () {
describe('getTsconfigPath', function () {
it('should get tsconfig path', function () {
expect(getTsconfigPath(path.dirname(fileURLToPath(import.meta.url)))).to.be.equal(process.cwd());
});
});
});

View File

@@ -14,36 +14,32 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {existsSync, unlinkSync} from 'fs';
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {createDiagram, createDiagramFromString} from '../src/uml/create-diagram.js';
import {UMLConfig} from '../src/uml/uml-config.js';
import {LightweightDefinition, lightweightDefinitionsFromPath} from '@openstapps/easy-ast';
import {unlink} from 'fs/promises';
import {createDiagram, createDiagramFromString} from '../src/index.js';
import {UMLConfig} from '../src/index.js';
import {lightweightDefinitionsFromPath} from '@openstapps/easy-ast';
import nock = require('nock');
import path from 'path';
import {existsSync} from 'fs';
import {fileURLToPath} from 'url';
@suite(timeout(15_000), slow(5000))
export class CreateDiagramSpec {
plantUmlConfig: UMLConfig;
describe('CreateDiagram', function () {
this.timeout(15_000);
this.slow(5000);
definitions: LightweightDefinition[];
const plantUmlConfig: UMLConfig = {
definitions: [],
showAssociations: true,
showEnumValues: true,
showInheritance: true,
showInheritedProperties: true,
showOptionalProperties: true,
showProperties: true,
};
constructor() {
this.plantUmlConfig = {
definitions: [],
showAssociations: true,
showEnumValues: true,
showInheritance: true,
showInheritedProperties: true,
showOptionalProperties: true,
showProperties: true,
};
const definitions = lightweightDefinitionsFromPath('./test/model');
this.definitions = lightweightDefinitionsFromPath('./test/model');
}
@test
async shouldRefuseRequest() {
it('should refuse request', async function () {
const testPlantUmlCode = 'class Test{\n}';
try {
await createDiagramFromString(testPlantUmlCode, 'http://plantuml:8080');
@@ -54,7 +50,7 @@ export class CreateDiagramSpec {
new Error('getaddrinfo ENOTFOUND plantuml').message,
]).to.include((error as NodeJS.ErrnoException).message);
}
}
});
/**
* This test will only test the functionality of the method
@@ -63,26 +59,25 @@ export class CreateDiagramSpec {
* - Writing the response to a file
* This test will not check the file content
*/
@test
async shouldCreateDiagrams() {
it('should create diagrams', async function () {
nock('http://plantuml:8080')
.persist()
.get(() => true)
.reply(200, 'This will be the file content');
let fileName = await createDiagram(this.definitions, this.plantUmlConfig, 'http://plantuml:8080');
let filePath = path.resolve(__dirname, '..', fileName);
expect(await existsSync(filePath)).to.equal(true);
let fileName = await createDiagram(definitions, plantUmlConfig, 'http://plantuml:8080');
let filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', fileName);
expect(existsSync(filePath)).to.be.true;
await unlinkSync(fileName);
this.plantUmlConfig.showAssociations = false;
await unlink(fileName);
plantUmlConfig.showAssociations = false;
this.plantUmlConfig.showInheritance = false;
fileName = await createDiagram(this.definitions, this.plantUmlConfig, 'http://plantuml:8080');
filePath = path.resolve(__dirname, '..', fileName);
expect(await existsSync(filePath)).to.equal(true);
await unlinkSync(fileName);
plantUmlConfig.showInheritance = false;
fileName = await createDiagram(definitions, plantUmlConfig, 'http://plantuml:8080');
filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', fileName);
expect(existsSync(filePath)).to.be.true;
await unlink(fileName);
nock.cleanAll();
}
}
});
});

View File

@@ -15,9 +15,9 @@
*/
import {Logger} from '@openstapps/logger';
import {expect} from 'chai';
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {Converter} from '../src/schema.js';
import path from 'path';
import {fileURLToPath} from 'url';
process.on('unhandledRejection', (error: unknown) => {
if (error instanceof Error) {
@@ -26,11 +26,14 @@ process.on('unhandledRejection', (error: unknown) => {
process.exit(1);
});
@suite(timeout(40_000), slow(10_000))
export class SchemaSpec {
@test
async getSchema() {
const converter = new Converter(path.join(__dirname, '..', 'src', 'resources'));
describe('Schema', function () {
this.timeout(40_000);
this.slow(10_000);
it('should create schema', function () {
const converter = new Converter(
path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'resources'),
);
const schema = converter.getSchema('Foo', '0.0.1');
expect(schema).to.be.deep.equal({
@@ -77,5 +80,5 @@ export class SchemaSpec {
required: ['lorem', 'type'],
type: 'object',
});
}
}
});
});

View File

@@ -15,14 +15,14 @@
*/
import {Logger} from '@openstapps/logger';
import {expect} from 'chai';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {existsSync} from 'fs';
import {JSONSchema7 as Schema} from 'json-schema';
import {slow, suite, test, timeout} from '@testdeck/mocha';
import rimraf from 'rimraf';
import {Foo} from '../src/resources/foo.js';
import {Converter} from '../src/schema.js';
import {Validator} from '../src/validate.js';
import path from 'path';
import {fileURLToPath} from 'url';
import {rm, mkdir, writeFile} from 'fs/promises';
import {Converter} from '../src/index.js';
process.on('unhandledRejection', (error: unknown) => {
if (error instanceof Error) {
@@ -31,57 +31,55 @@ process.on('unhandledRejection', (error: unknown) => {
process.exit(1);
});
const tmpdir = path.join(__dirname, 'tmp');
const tmpdir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'tmp');
const fooInstance: Foo = {
lorem: 'ipsum',
type: 'Foo',
};
@suite(timeout(40_000), slow(5000))
export class ValidateSpec {
static converter: Converter;
describe('Validator', function () {
this.timeout(40_000);
this.slow(5000);
static schema: Schema;
let schema: Schema;
let converter: Converter;
static before() {
this.converter = new Converter(path.join(__dirname, '..', 'src', 'resources'));
this.schema = this.converter.getSchema('Foo', '0.0.1');
beforeEach(async function () {
converter = new Converter(
path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'resources'),
);
schema = converter.getSchema('Foo', '0.0.1');
if (!existsSync(tmpdir)) {
mkdirSync(tmpdir);
await mkdir(tmpdir);
}
writeFileSync(path.join(tmpdir, 'SCFoo.json'), JSON.stringify(this.schema, undefined, 2));
}
await writeFile(path.join(tmpdir, 'SCFoo.json'), JSON.stringify(schema, undefined, 2));
});
static after() {
rimraf(tmpdir, error => {
// tslint:disable-next-line: no-unused-expression
afterEach(async function () {
try {
await rm(tmpdir, {recursive: true});
} catch (error) {
expect(error, `Unable to remove temporary directory for tests at: ${tmpdir}`).to.be.null;
});
}
}
});
@test
async validateBySchemaIdentifyingString() {
it('should validate by schema identifying string', async function () {
const validator = new Validator();
await validator.addSchemas(tmpdir);
const validationResult = validator.validate(fooInstance, 'SCFoo');
// tslint:disable-next-line: no-unused-expression
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
}
});
@test
async validateBySchemaInstance() {
it('should validate by schema instance', async function () {
const validator = new Validator();
const validationResult = validator.validate(fooInstance, ValidateSpec.schema);
// tslint:disable-next-line: no-unused-expression
const validationResult = validator.validate(fooInstance, schema);
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
}
});
@test
async validateIntrinsic() {
it('should validate intrinsic', async function () {
const validator = new Validator();
await validator.addSchemas(tmpdir);
const validationResult = validator.validate(fooInstance);
// tslint:disable-next-line: no-unused-expression
expect(validationResult.errors, JSON.stringify(validationResult.errors, undefined, 2)).to.be.empty;
}
}
});
});