Compare commits

...

8 Commits

Author SHA1 Message Date
Jovan Krunić
9ab5280b2e 0.50.0 2021-09-01 13:56:44 +02:00
Wieland Schöbl
596788f3a1 test: migrate schema.spec.ts from TypeDoc to Easy AST 2021-09-01 11:40:32 +00:00
Rainer Killinger
70271a4849 docs: update changelog 2021-08-27 17:27:23 +02:00
Rainer Killinger
98ab64403f 0.49.5 2021-08-27 17:27:21 +02:00
Rainer Killinger
3d1bb6ef13 docs: update changelog 2021-08-27 17:25:06 +02:00
Rainer Killinger
d5f39517e8 0.49.4 2021-08-27 17:20:40 +02:00
Rainer Killinger
dfe35d71a3 fix: test resources from hds2 2021-08-27 17:19:25 +02:00
wulkanat@gmail.com
cdaa83122f docs: update changelog 2021-08-18 09:57:29 +02:00
14 changed files with 798 additions and 504 deletions

View File

@@ -1,3 +1,16 @@
## [0.49.5](https://gitlab.com/openstapps/core/compare/v0.49.3...v0.49.5) (2021-08-27)
### Bug Fixes
* test resources from hds2 ([dfe35d7](https://gitlab.com/openstapps/core/commit/dfe35d71a38c35064726365f99714abff3b30ba6))
## [0.49.3](https://gitlab.com/openstapps/core/compare/v0.49.2...v0.49.3) (2021-08-18)
## [0.49.2](https://gitlab.com/openstapps/core/compare/v0.49.1...v0.49.2) (2021-08-17)

805
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "@openstapps/core",
"version": "0.49.3",
"version": "0.50.0",
"description": "StAppsCore - Generalized model of data",
"keywords": [
"Model",
@@ -45,7 +45,7 @@
"Wieland Schöbl"
],
"dependencies": {
"@openstapps/core-tools": "0.23.2",
"@openstapps/core-tools": "0.25.0",
"@types/geojson": "1.0.6",
"@types/json-patch": "0.0.30",
"@types/json-schema": "7.0.9",
@@ -62,11 +62,14 @@
"@openstapps/es-mapping-generator": "0.0.3",
"@openstapps/logger": "0.7.0",
"@testdeck/mocha": "0.1.2",
"@types/lodash": "4.14.172",
"@types/chai": "4.2.21",
"@types/rimraf": "3.0.1",
"@types/mocha": "8.2.3",
"chai": "4.3.4",
"conditional-type-checks": "1.0.5",
"conventional-changelog-cli": "2.1.1",
"lodash": "4.17.21",
"mocha": "8.4.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
@@ -74,7 +77,7 @@
"surge": "0.23.0",
"ts-node": "9.1.1",
"tslint": "6.1.3",
"typedoc": "0.21.5",
"typedoc": "0.21.6",
"typescript": "4.3.5"
},
"nyc": {

View File

@@ -12,7 +12,7 @@
* 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 {ValidationError} from '@openstapps/core-tools/lib/common';
import {ValidationError} from '@openstapps/core-tools/lib/types/validator';
import {BAD_REQUEST} from 'http-status-codes';
import {SCError} from '../error';

140
test/features.spec.ts Normal file
View File

@@ -0,0 +1,140 @@
/*
* 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 {isLightweightClass, isLightweightEnum, isUnionType} from '@openstapps/core-tools/lib/easy-ast/ast-util';
import {LightweightAliasDefinition} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-alias-definition';
import {LightweightProjectWithIndex} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-project';
import {LightweightType} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-type';
import {LightweightClassDefinition} from '@openstapps/core-tools/src/easy-ast/types/lightweight-class-definition';
import {LightweightDefinition} from '@openstapps/core-tools/src/easy-ast/types/lightweight-definition';
import {LightweightProperty} from '@openstapps/core-tools/src/easy-ast/types/lightweight-property';
import {expect} from 'chai';
import {assign, chain, clone, flatMap, isNil, reduce, reject, some} from 'lodash';
process.on('unhandledRejection', (err) => {
throw err;
});
describe('Features', () => {
let project: LightweightProjectWithIndex;
let thingNames: string[];
let things: LightweightClassDefinition[];
let thingsWithoutReferences: LightweightClassDefinition[];
before(function () {
this.timeout(15000);
this.slow(10000);
project = new LightweightProjectWithIndex('src');
const thingsReflection = project.definitions['SCIndexableThings'] as LightweightAliasDefinition;
expect(isLightweightEnum(thingsReflection)).to.be.true;
expect(isUnionType(thingsReflection.type!)).to.be.true;
thingsReflection.type!.specificationTypes!.push({
flags: 524_288,
referenceName: 'SCDiff',
});
// tslint:disable-next-line:no-unused-expression
expect(thingsReflection.type?.specificationTypes?.every(it => typeof it.referenceName !== 'undefined')).to.be.true;
thingNames = thingsReflection.type?.specificationTypes?.map(type => type.referenceName!) ?? [];
things = thingNames
.map(it => project.definitions[it])
.filter(isLightweightClass);
thingsWithoutReferences = thingNames
.map(it => project.definitions[`${it}WithoutReferences`])
.filter(isLightweightClass);
});
const inheritedProperties = function (classLike: LightweightClassDefinition):
Record<string, LightweightProperty> | undefined {
return reduce(
[...(classLike.implementedDefinitions ?? []), ...(classLike.extendedDefinitions ?? [])],
(obj, extension) => {
const object = project.definitions[extension.referenceName ?? ''];
return assign(obj, isLightweightClass(object)
? inheritedProperties(object)
: obj);
},
clone(classLike.properties)
);
};
it('should have an origin', () => {
for (const thing of things) {
expect(inheritedProperties(thing)?.['origin']).not.to.be.undefined;
}
});
it('should not have duplicate names', () => {
reduce(project.files, (fileResult, file) =>
reduce(file, (definitionResult, definition: LightweightDefinition) => {
expect(definitionResult[definition.name]).to.be.undefined;
definitionResult[definition.name] = true; // something that's not undefined
return definitionResult;
}, fileResult), {} as Record<string, true>);
});
it('should not have properties referencing SCThing', () => {
const allPropertyReferenceNames: (property: LightweightProperty) => string[] = property => reject([
property.type.referenceName!,
...flatMap(property.properties, allPropertyReferenceNames),
], isNil);
const typeHasSCThingReferences: (type?: LightweightType) => boolean = type => type?.referenceName
? hasSCThingReferences(project.definitions[type.referenceName])
: some(type?.specificationTypes, typeHasSCThingReferences);
const hasSCThingReferences: (definition?: LightweightDefinition) => boolean = definition =>
isLightweightClass(definition)
? chain(inheritedProperties(definition))
.flatMap(it => flatMap(it.properties, allPropertyReferenceNames))
.map(it => project.definitions[it] as LightweightDefinition)
.some(it => it.name === 'SCThing' || hasSCThingReferences(it))
.value()
: definition ? typeHasSCThingReferences(definition.type) : false;
for (const thing of things) {
expect(hasSCThingReferences(thing)).to.be.false;
}
});
function extendsSCThing(definition?: LightweightDefinition): boolean {
return isLightweightClass(definition)
? chain([
...(definition as LightweightClassDefinition).extendedDefinitions ?? [],
...(definition as LightweightClassDefinition).implementedDefinitions ?? [],
])
.map(it => it.referenceName)
.reject(isNil)
.some(it => it === 'SCThing' || extendsSCThing(project.definitions[it!]))
.value()
: false;
}
it('should extend SCThing if it is an SCThing', () => {
for (const thing of things) {
expect(extendsSCThing(thing)).to.be.true;
}
});
it('should not extend SCThing if it is an SCThingWithoutReferences', () => {
for (const thingWithoutReferences of thingsWithoutReferences) {
expect(extendsSCThing(thingWithoutReferences)).to.be.false;
}
});
});

View File

@@ -22,7 +22,7 @@
"name": "HeBIS HDS",
"originalId": "HEB198305427",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
},
"isPartOf": {
"uid": "bc5e5399-a24c-5c01-9c1b-0c8b83272087",

View File

@@ -22,7 +22,7 @@
"name": "HeBIS HDS",
"originalId": "HEB107025590",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
},
"isPartOf": {
"uid": "f84c1851-042e-542f-ba7a-158b32dfb82f",

View File

@@ -33,7 +33,7 @@
"name": "HeBIS HDS",
"originalId": "HEB022992618",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
}
},
"schema": "SCBook"

View File

@@ -18,7 +18,7 @@
"firstPublished": "2001",
"publications": [
{
"uid": "603a6574-8910-588a-9e83-cd26e6988c74",
"uid": "6333427c-0725-5398-9a04-11604680dae3",
"type": "publication event",
"locations": [
"Paris"
@@ -36,7 +36,7 @@
"name": "HeBIS HDS",
"originalId": "HEB102248788",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
}
},
"schema": "SCBook"

View File

@@ -1,6 +1,6 @@
{
"errorNames": [
"enum"
"const"
],
"instance": {
"type": "invalid-value-in-schema",

View File

@@ -28,7 +28,7 @@
"name": "HeBIS HDS",
"originalId": "HEB046847146",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
}
},
"schema": "SCPeriodical"

View File

@@ -25,7 +25,7 @@
"name": "HeBIS HDS",
"originalId": "HEB048624853",
"type": "remote",
"url": "https://hds2test.hebis.de/ubffm/"
"url": "https://hds2.hebis.de/ubffm/"
}
},
"schema": "SCPeriodical"

View File

@@ -1,321 +1,11 @@
/*
* 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 {getTsconfigPath} from '@openstapps/core-tools/lib/common';
import {validateFiles, writeReport} from '@openstapps/core-tools/lib/validate';
import {Logger} from '@openstapps/logger';
import {fail} from 'assert';
import {expect} from 'chai';
import {mkdirSync, PathLike} from 'fs';
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {expect} from 'chai';
import {mkdirSync} from 'fs';
import {join, resolve} from 'path';
import {Application, DeclarationReflection, ProjectReflection, TSConfigReader} from 'typedoc';
import {ArrayType, IntrinsicType, ReferenceType, LiteralType, Type, UnionType} from 'typedoc/dist/lib/models';
process.on('unhandledRejection', (err) => {
throw err;
});
/**
* Get a project reflection from a path
*
* @param srcPath Path to get reflection from
* @param excludeExternals Exclude external dependencies
*/
export function getProjectReflection(srcPath: PathLike, excludeExternals = true): ProjectReflection {
Logger.info(`Generating project reflection for ${srcPath.toString()}.`);
const tsconfigPath = getTsconfigPath(srcPath.toString());
let inputFilePath = srcPath;
if (inputFilePath === tsconfigPath) {
inputFilePath = join(tsconfigPath, 'src');
}
// initialize new Typedoc application
const app = new Application();
app.options.addReader(new TSConfigReader());
app.bootstrap({
entryPoints: [inputFilePath.toString()],
excludeExternals: excludeExternals,
tsconfig: join(tsconfigPath, 'tsconfig.json'),
});
// get project reflection from input files
const result = app.convert();
if (typeof result === 'undefined') {
throw new Error('Project reflection could not be generated.');
}
return result;
}
/**
* Check if type is a union type
*
* @param type Type to check
*/
function isUnionType(type: Type): type is UnionType {
return type.type === 'union';
}
/**
* Check if a type is reference type
*
* @param type Type to check
*/
function isReferenceType(type: Type): type is ReferenceType {
return type.type === 'reference';
}
/**
* Check if a type is an array type
*
* @param type Type to check
*/
function isArrayType(type: Type): type is ArrayType {
return type.type === 'array';
}
/**
* Check if a type is an intrinsic type
*
* @param type Type to check
*/
function isIntrinsicType(type: Type): type is IntrinsicType {
return type.type === 'intrinsic';
}
/**
* Check if a type is a string literal type
*
* @param type Type to check
*/
function isLiteralType(type: Type): type is LiteralType {
return type.type === 'literal';
}
/**
* Get extended types of a declaration reflection
* @param thingReflection Reflection of the thing
* @param objects Map of reflections by name
*/
function getExtendedTypes(thingReflection: DeclarationReflection,
objects: { [name: string]: DeclarationReflection }): string[] {
const extendedTypes: string[] = [];
if (Array.isArray(thingReflection.extendedTypes)) {
const typesToCheck = thingReflection.extendedTypes.slice();
while (typesToCheck.length > 0) {
const extendedType = typesToCheck.splice(0, 1)[0];
extendedTypes.push((extendedType as unknown as ReferenceType).name);
const extendedObject = objects[(extendedType as unknown as ReferenceType).name];
if (typeof extendedObject !== 'undefined') {
if (Array.isArray(extendedObject.extendedTypes)) {
typesToCheck.push.apply(typesToCheck, extendedObject.extendedTypes);
}
}
}
}
return extendedTypes;
}
@suite(timeout(15000), slow(10000))
export class SchemaSpec {
static objects: { [name: string]: DeclarationReflection } = {};
static reflection: ProjectReflection;
static thingNames: string[];
static before() {
SchemaSpec.reflection = getProjectReflection(resolve(__dirname, '..', 'src'));
if (Array.isArray(SchemaSpec.reflection.children)) {
for (const module of SchemaSpec.reflection.children) {
if (Array.isArray(module.children)) {
for (const object of module.children) {
SchemaSpec.objects[object.name] = object;
}
}
}
}
const thingsReflection = SchemaSpec.objects.SCIndexableThings;
// tslint:disable-next-line:no-unused-expression
expect(thingsReflection).not.to.be.undefined;
// tslint:disable-next-line:no-unused-expression
expect(isUnionType(thingsReflection.type!)).to.be.true;
(thingsReflection.type! as UnionType).types.push({
'id': 0,
'name': 'SCDiff',
'type': 'reference',
} as unknown as ReferenceType);
// tslint:disable-next-line:no-unused-expression
expect((thingsReflection.type! as UnionType).types.every(isReferenceType)).to.be.true;
SchemaSpec.thingNames = (thingsReflection.type! as UnionType).types.map((type) => {
return (type as ReferenceType).name;
});
}
@test
'all things have an origin'() {
for (const thingName of SchemaSpec.thingNames) {
const thingReflection = SchemaSpec.objects[`${thingName}`];
let originFound = false;
if (Array.isArray(thingReflection.children)) {
for (const property of thingReflection.children) {
if (property.name === 'origin') {
originFound = true;
break;
}
}
}
// tslint:disable-next-line:no-unused-expression
expect(originFound).to.be.equal(true, `'${thingName}' must have property 'origin'.`);
}
}
@test
'does not have duplicate names'() {
const names: string[] = [];
if (Array.isArray(SchemaSpec.reflection.children)) {
for (const module of SchemaSpec.reflection.children) {
if (Array.isArray(module.children)) {
for (const object of module.children) {
expect(names).not.to.contain(object.name);
names.push(object.name);
}
}
}
}
}
@test
'no property is an SCThing'() {
const handleUnionType = (type: UnionType, thingName: string, property: DeclarationReflection) => {
for (const nestedType of type.types) {
if (isIntrinsicType(nestedType) || isLiteralType(nestedType)) {
continue;
} else if (isReferenceType(nestedType)) {
expect(SchemaSpec.thingNames).not.to.contain(
nestedType.name,
`Union property '${property.name}' on type '${thingName}' contains type '${nestedType.name}'.`,
);
} else {
// tslint:disable-next-line:max-line-length
fail(`'${thingName}'#'${property.name}' union type '${nestedType.type}' is not handled by this test!`);
}
}
};
for (const thingName of SchemaSpec.thingNames) {
const thingReflection = SchemaSpec.objects[`${thingName}`];
if (Array.isArray(thingReflection.children)) {
for (const property of thingReflection.children) {
if (typeof property.type === 'undefined') {
Logger.error(thingName, property.name);
continue;
}
let type = property.type!;
if (isIntrinsicType(type)) {
continue;
} else if (isArrayType(type)) {
const elementType = type.elementType;
if (isIntrinsicType(elementType) || isLiteralType(elementType)) {
continue;
} else if (isUnionType(elementType)) {
handleUnionType(elementType, thingName, property);
} else if (isReferenceType(elementType)) {
expect(SchemaSpec.thingNames).not.to.contain(
elementType.name,
`Array property '${property.name}' on type '${thingName}' has element type '${elementType.name}'.`,
);
} else {
// tslint:disable-next-line:max-line-length
fail(`'${thingName}'#'${property.name}' element type '${elementType.type}' is not handled by this test!`);
}
} else if (isReferenceType(type)) {
do {
expect(SchemaSpec.thingNames).not.to.contain(
type.name,
`Property '${property.name}' on type '${thingName}' has element type '${type.name}'.`,
);
const referencedObject = SchemaSpec.objects[type.name];
if (typeof referencedObject !== 'undefined') {
const referencedType = referencedObject.type;
if (typeof referencedType !== 'undefined') {
type = referencedType;
} else {
break;
}
} else {
break;
}
} while (isReferenceType(type));
} else if (isUnionType(type)) {
handleUnionType(type, thingName, property);
} else {
// tslint:disable-next-line:max-line-length
fail(`'${thingName}'#'${property.name}' with type '${type.type}' is not handled by this test!`);
}
}
}
}
}
@test
'things extend SCThing'() {
for (const thingName of SchemaSpec.thingNames) {
const thingReflection = SchemaSpec.objects[`${thingName}`];
expect(getExtendedTypes(thingReflection, SchemaSpec.objects)).to.contain(
'SCThing',
`'${thingName}' neither extends 'SCThing' transitively nor directly.`,
);
}
}
@test
'things without references do not extend SCThing'() {
for (const thingName of SchemaSpec.thingNames) {
const thingWithoutReferencesReflection = SchemaSpec.objects[`${thingName}WithoutReferences`];
expect(getExtendedTypes(thingWithoutReferencesReflection, SchemaSpec.objects)).not.to.contain(
'SCThing',
`'${thingName}WithoutReferences' extends 'SCThing' either transitively or directly.`,
);
}
}
@test
async 'validate against test files'() {
const errorsPerFile = {

View File

@@ -1,6 +1,7 @@
{
"extends": "./node_modules/@openstapps/configuration/tslint.json",
"rules": {
"no-empty-interface": false
"no-empty-interface": false,
"no-redundant-jsdoc": false
}
}