From dbc0586fc2d7a918b8b647730ac85d054e7fe6f6 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Tue, 28 May 2019 16:15:32 +0200 Subject: [PATCH] test: add test for validator --- package-lock.json | 10 ++++++ package.json | 1 + test/Validate.spec.ts | 83 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 test/Validate.spec.ts diff --git a/package-lock.json b/package-lock.json index 464493e2..51baf899 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 012eb0e2..25f73a2b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/Validate.spec.ts b/test/Validate.spec.ts new file mode 100644 index 00000000..40834e9b --- /dev/null +++ b/test/Validate.spec.ts @@ -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 . + */ +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; + } +}