Files
openstapps/test/Validate.spec.ts
2019-06-18 14:16:17 +02:00

84 lines
2.9 KiB
TypeScript

/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Logger} from '@openstapps/logger';
import {expect} from 'chai';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {Schema} from 'jsonschema';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {join} from 'path';
import * as rimraf from 'rimraf';
import {Foo} from '../src/resources/foo';
import {Converter} from '../src/schema';
import {Validator} from '../src/validate';
process.on('unhandledRejection', (err) => {
Logger.error('UNHANDLED REJECTION', err.stack);
process.exit(1);
});
const tmpdir = join(__dirname, 'tmp');
const fooInstance: Foo = {
lorem: 'ipsum',
type: 'Foo',
};
@suite(timeout(15000), slow(5000))
export class SchemaSpec {
static converter: Converter;
static schema: Schema;
static before() {
this.converter = new Converter(join(__dirname, '..', 'src', 'resources'));
this.schema = this.converter.getSchema('Foo', '0.0.1');
if (!existsSync(tmpdir)) {
mkdirSync(tmpdir);
}
writeFileSync(join(tmpdir, 'SCFoo.json'), JSON.stringify(this.schema, null, 2));
}
static after() {
rimraf(tmpdir, (error: Error) => {
// tslint:disable-next-line: no-unused-expression
expect(error, `Unable to remove temporary directory for tests at: ${tmpdir}`).to.be.null;
});
}
@test
async validateBySchemaIdentifingString() {
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, null, 2)).to.be.empty;
}
@test
async validateBySchemaInstance() {
const validator = new Validator();
const validationResult = validator.validate(fooInstance, SchemaSpec.schema);
// tslint:disable-next-line: no-unused-expression
expect(validationResult.errors, JSON.stringify(validationResult.errors, null, 2)).to.be.empty;
}
@test
async validateIntrinsic() {
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, null, 2)).to.be.empty;
}
}