Files
openstapps/test/mapping-model/MapAggTest.ts
2020-09-29 19:02:13 +02:00

137 lines
4.2 KiB
TypeScript

// tslint:disable
/*
* 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 {generateTemplate} from '../../src/mapping';
import {resolve} from "path";
import {expect} from "chai";
import {ProjectReflection} from 'typedoc';
import {getProjectReflection} from '../../src/common';
import {AggregationSchema, ESNestedAggregation} from '../../src/mappings/aggregation-definitions';
import {MapAggTestOptions, MinimalMappingDescription} from './MapAggTestOptions';
import {ElasticsearchTemplateCollection} from '../../src/mappings/mapping-definitions';
import {settings} from '../../src/mappings/definitions/settings';
import {ElasticsearchDataType} from '../../src/mappings/definitions/typemap';
export class MapAggTest {
mapping_model_path!: string;
reflection!: ProjectReflection;
constructor(dir: string) {
this.mapping_model_path = resolve(__dirname, dir);
this.reflection = getProjectReflection(this.mapping_model_path);
}
testInterfaceAgainstPath(options: MapAggTestOptions) {
const template = generateTemplate(this.reflection, options.ignoredTags ?? [], false, [options.name]);
if (typeof options.err !== 'undefined') {
for (const error of template.errors) {
expect(options.err).to.include(error, "Unexpected Error!")
}
} else {
expect(template.errors).to.be.deep.equal([], 'Unexpected Error!');
}
if (typeof options.agg !== 'undefined') {
const expectedAggSchema = MapAggTest.buildAggregation(options.name, options.agg.fields, options.agg.globals)
expect(template.aggregations).to.be.deep.equal(expectedAggSchema, 'Aggregation schema not equal!');
}
if (typeof options.map !== 'undefined') {
const expectedMappingSchema = MapAggTest.buildMapping(options.name, options.map);
expect(template.mappings).to.be.deep.equal(expectedMappingSchema, 'Mapping schema not equal!');
}
}
static buildAggregation(name: string, fields?: string[], globals?: string[]): AggregationSchema {
const out: AggregationSchema = {
'@all': {
aggs: {},
filter: {
match_all: {}
}
},
};
for (const global of globals ?? []) {
(out['@all']! as ESNestedAggregation).aggs[global] = {
terms: {
field: `${global}.raw`,
size: 1000,
}
}
}
if (typeof fields === 'undefined' || fields.length === 0) {
return out;
}
out[name] = {
aggs: {},
filter: {
type: {
value: name,
}
}
}
for (const field of fields) {
(out[name]! as ESNestedAggregation).aggs[field] = {
terms: {
field: `${field}.raw`,
size: 1000,
}
}
}
return out;
}
static buildMapping(name: string, map: MinimalMappingDescription): ElasticsearchTemplateCollection {
let typeNameWithoutSpaces = name.toLowerCase();
while (typeNameWithoutSpaces.includes(' ')) {
typeNameWithoutSpaces = typeNameWithoutSpaces.replace(' ', '_');
}
const out: ElasticsearchTemplateCollection = {};
const templateName = `template_${typeNameWithoutSpaces}`;
out[templateName] = {
mappings: {},
settings: settings,
template: `stapps_${typeNameWithoutSpaces}*`,
}
const maps = map.maps ?? {};
maps.type = {
type: ElasticsearchDataType.text
}
maps.creation_date = {
type: ElasticsearchDataType.date
}
out[templateName].mappings[name] = {
_source: {
excludes: [
'creation_date'
]
},
date_detection: false,
dynamic: 'strict',
properties: maps,
dynamic_templates: map.dynamicTemplates ?? [],
}
return out;
}
}