mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 19:52:53 +00:00
test: add tests for schema generation
This commit is contained in:
115
test/Schema.spec.ts
Normal file
115
test/Schema.spec.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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 {slow, suite, test, timeout} from 'mocha-typescript';
|
||||
import {join} from 'path';
|
||||
import {ProjectReflection} from 'typedoc';
|
||||
import {Converter, getValidatableTypesFromReflection} from '../src/schema';
|
||||
|
||||
const logger = new Logger();
|
||||
|
||||
type RecursivePartial<T> = {
|
||||
[P in keyof T]?:
|
||||
T[P] extends Array<infer U> ? Array<RecursivePartial<U>> :
|
||||
T[P] extends object ? RecursivePartial<T[P]> :
|
||||
T[P];
|
||||
};
|
||||
|
||||
process.on('unhandledRejection', (err) => {
|
||||
logger.error('UNHANDLED REJECTION', err.stack);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class SchemaSpec {
|
||||
@test
|
||||
async getSchema() {
|
||||
const converter = new Converter(join(__dirname, 'resources'));
|
||||
|
||||
const schema = converter.getSchema('Foo', '0.0.1');
|
||||
|
||||
expect(schema).to.be.deep.equal({
|
||||
$schema: 'http://json-schema.org/draft-06/schema#',
|
||||
additionalProperties: false,
|
||||
definitions: {
|
||||
SCFoo: {
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
lorem: {
|
||||
enum: [
|
||||
'ipsum',
|
||||
],
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'lorem',
|
||||
],
|
||||
type: 'object',
|
||||
},
|
||||
},
|
||||
id: 'https://core.stapps.tu-berlin.de/v0.0.1/lib/schema/Foo.json',
|
||||
properties: {
|
||||
lorem: {
|
||||
enum: [
|
||||
'ipsum',
|
||||
],
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'lorem',
|
||||
],
|
||||
type: 'object',
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async getValidatableTypesFromReflection() {
|
||||
const reflection: RecursivePartial<ProjectReflection> = {
|
||||
children: [
|
||||
{
|
||||
children: [
|
||||
{
|
||||
comment: {
|
||||
tags: [
|
||||
{
|
||||
tagName: 'validatable',
|
||||
},
|
||||
],
|
||||
},
|
||||
name: 'foo',
|
||||
},
|
||||
{
|
||||
name: 'bar',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
children: [
|
||||
{
|
||||
name: 'foobar',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const validatableTypes = getValidatableTypesFromReflection(reflection as any);
|
||||
|
||||
expect(validatableTypes).to.be.deep.equal(['foo']);
|
||||
}
|
||||
}
|
||||
6
test/resources/Foo.ts
Normal file
6
test/resources/Foo.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @validatable
|
||||
*/
|
||||
export interface Foo {
|
||||
lorem: 'ipsum';
|
||||
}
|
||||
Reference in New Issue
Block a user