mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-02-27 03:12:16 +00:00
feat: tests
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user