mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
test: add test for validator
This commit is contained in:
10
package-lock.json
generated
10
package-lock.json
generated
@@ -156,6 +156,16 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/rimraf": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/rimraf/-/rimraf-2.0.2.tgz",
|
||||
"integrity": "sha512-Hm/bnWq0TCy7jmjeN5bKYij9vw5GrDFWME4IuxV08278NtU/VdGbzsBohcCUJ7+QMqmUq5hpRKB39HeQWJjztQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/glob": "*",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/semver": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-6.0.0.tgz",
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
"@openstapps/configuration": "0.19.0",
|
||||
"@types/chai": "4.1.7",
|
||||
"@types/mocha": "5.2.7",
|
||||
"@types/rimraf": "2.0.2",
|
||||
"conventional-changelog-cli": "2.0.21",
|
||||
"mocha": "6.1.4",
|
||||
"mocha-typescript": "1.1.17",
|
||||
|
||||
83
test/Validate.spec.ts
Normal file
83
test/Validate.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user