From 04c5c08ca50b14f0cc49aa482b30e33a92b2385c Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 5 Apr 2023 19:15:26 +0200 Subject: [PATCH 1/2] fix: migration from single to multi value select works automatically --- .../schema-datatypes/datatype-array.spec.ts | 70 +++++++++++++++++++ .../entity/schema-datatypes/datatype-array.ts | 12 ++-- 2 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/app/core/entity/schema-datatypes/datatype-array.spec.ts diff --git a/src/app/core/entity/schema-datatypes/datatype-array.spec.ts b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts new file mode 100644 index 0000000000..0d78cfb8ae --- /dev/null +++ b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts @@ -0,0 +1,70 @@ +import { EntitySchemaField } from "../schema/entity-schema-field"; +import { EntitySchemaService } from "../schema/entity-schema.service"; +import { ConfigurableEnumDatatype } from "../../configurable-enum/configurable-enum-datatype/configurable-enum-datatype"; +import { defaultInteractionTypes } from "../../config/default-config/default-interaction-types"; +import { arrayEntitySchemaDatatype } from "./datatype-array"; + +describe("Schema data type: array", () => { + let schemaService: EntitySchemaService; + const schema: EntitySchemaField = { + dataType: "array", + innerDataType: "configurable-enum", + additional: "test", + }; + beforeEach(() => { + schemaService = new EntitySchemaService(); + schemaService.registerSchemaDatatype( + new ConfigurableEnumDatatype({ + getEnumValues: () => defaultInteractionTypes, + } as any) + ); + }); + + it("should transform enums inside arrays", () => { + const value = defaultInteractionTypes.map(({ id }) => id); + + const obj = arrayEntitySchemaDatatype.transformToObjectFormat( + value, + schema, + schemaService + ); + + expect(obj).toEqual(defaultInteractionTypes); + + const db = arrayEntitySchemaDatatype.transformToDatabaseFormat( + obj, + schema, + schemaService + ); + + expect(db).toEqual(value); + }); + + it("should automatically transform values that are not entities", () => { + const value = defaultInteractionTypes[1].id; + + const obj = arrayEntitySchemaDatatype.transformToObjectFormat( + value, + schema, + schemaService + ); + + expect(obj).toEqual([defaultInteractionTypes[1]]); + }); + + it("should transform empty values as an empty array", () => { + let obj = arrayEntitySchemaDatatype.transformToObjectFormat( + undefined, + schema, + schemaService + ); + expect(obj).toEqual([]); + + obj = arrayEntitySchemaDatatype.transformToObjectFormat( + "", + schema, + schemaService + ); + expect(obj).toEqual([]); + }); +}); diff --git a/src/app/core/entity/schema-datatypes/datatype-array.ts b/src/app/core/entity/schema-datatypes/datatype-array.ts index 6fc37f9d6d..812eca2f21 100644 --- a/src/app/core/entity/schema-datatypes/datatype-array.ts +++ b/src/app/core/entity/schema-datatypes/datatype-array.ts @@ -49,9 +49,8 @@ export const arrayEntitySchemaDatatype: EntitySchemaDatatype = { return value; } - const arrayElementDatatype: EntitySchemaDatatype = schemaService.getDatatypeOrDefault( - schemaField.innerDataType - ); + const arrayElementDatatype: EntitySchemaDatatype = + schemaService.getDatatypeOrDefault(schemaField.innerDataType); return value.map((el) => arrayElementDatatype.transformToDatabaseFormat( el, @@ -74,12 +73,11 @@ export const arrayEntitySchemaDatatype: EntitySchemaDatatype = { value, parent ); - return value; + value = value ? [value] : []; } - const arrayElementDatatype: EntitySchemaDatatype = schemaService.getDatatypeOrDefault( - schemaField.innerDataType - ); + const arrayElementDatatype: EntitySchemaDatatype = + schemaService.getDatatypeOrDefault(schemaField.innerDataType); return value.map((el) => arrayElementDatatype.transformToObjectFormat( From 1b146ee27e8fcaa5a1393145ce18537be95b46e8 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 11 Apr 2023 12:05:54 +0200 Subject: [PATCH 2/2] Update src/app/core/entity/schema-datatypes/datatype-array.spec.ts Co-authored-by: Sebastian --- src/app/core/entity/schema-datatypes/datatype-array.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/core/entity/schema-datatypes/datatype-array.spec.ts b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts index 0d78cfb8ae..919ca2a888 100644 --- a/src/app/core/entity/schema-datatypes/datatype-array.spec.ts +++ b/src/app/core/entity/schema-datatypes/datatype-array.spec.ts @@ -40,7 +40,7 @@ describe("Schema data type: array", () => { expect(db).toEqual(value); }); - it("should automatically transform values that are not entities", () => { + it("should automatically wrap value into array (and transform to inner type) if not an array yet", () => { const value = defaultInteractionTypes[1].id; const obj = arrayEntitySchemaDatatype.transformToObjectFormat(