Skip to content

Commit

Permalink
add extension mechanism for Testable (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
gayathrir11 authored Oct 11, 2022
1 parent 04e914b commit 971a3cf
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 111 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-buttons-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': patch
---

Add extension mechanism for `Testable`
3 changes: 3 additions & 0 deletions .changeset/kind-months-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
'@finos/legend-extension-dsl-persistence': patch
---
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
type PackageableElement,
type PureGrammarElementLabeler,
PureGraphManagerPlugin,
type Testable_PureGraphManagerPlugin_Extension,
} from '@finos/legend-graph';
import { PersistenceTest } from '../graph/metamodel/pure/model/packageableElements/persistence/DSL_Persistence_PersistenceTest.js';

Expand All @@ -38,7 +39,10 @@ export const PURE_GRAMMAR_PERSISTENCE_ELEMENT_TYPE_LABEL = 'Persistence';
export const PURE_GRAMMAR_PERSISTENCE_CONTEXT_ELEMENT_TYPE_LABEL =
'PersistenceContext';

export class DSL_Persistence_PureGraphManagerPlugin extends PureGraphManagerPlugin {
export class DSL_Persistence_PureGraphManagerPlugin
extends PureGraphManagerPlugin
implements Testable_PureGraphManagerPlugin_Extension
{
constructor() {
super(packageJson.extensions.pureGraphManagerPlugin, packageJson.version);
}
Expand Down Expand Up @@ -83,7 +87,7 @@ export class DSL_Persistence_PureGraphManagerPlugin extends PureGraphManagerPlug
];
}

override getExtraAtomicTestObservers(): AtomicTestObserver[] {
getExtraAtomicTestObservers(): AtomicTestObserver[] {
return [
(
element: AtomicTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
V1_PERSISTENCE_ELEMENT_PROTOCOL_TYPE,
V1_persistenceModelSchema,
V1_TriggerType,
V1_persistenceTestModelSchema,
V1_AtomicTestType,
} from './v1/transformation/pureProtocol/V1_DSL_Persistence_ProtocolHelper.js';
import {
V1_PERSISTENCE_CONTEXT_ELEMENT_PROTOCOL_TYPE,
Expand Down Expand Up @@ -76,6 +78,9 @@ import {
type V1_GraphTransformerContext,
type V1_PackageableElement,
type V1_TestableAssertion,
type Testable_PureProtocolProcessorPlugin_Extension,
type V1_AtomicTestProtocolSerializer,
type V1_AtomicTestProtocolDeserializer,
} from '@finos/legend-graph';
import { assertType, type PlainObject } from '@finos/legend-shared';
import { deserialize, serialize } from 'serializr';
Expand All @@ -91,7 +96,9 @@ export const PERSISTENCE_CONTEXT_ELEMENT_CLASSIFIER_PATH =

export class DSL_Persistence_PureProtocolProcessorPlugin
extends PureProtocolProcessorPlugin
implements DSL_Persistence_PureProtocolProcessorPlugin_Extension
implements
DSL_Persistence_PureProtocolProcessorPlugin_Extension,
Testable_PureProtocolProcessorPlugin_Extension
{
constructor() {
super(
Expand Down Expand Up @@ -271,7 +278,7 @@ export class DSL_Persistence_PureProtocolProcessorPlugin
];
}

override V1_getExtraAtomicTestBuilders?(): V1_AtomicTestBuilder[] {
V1_getExtraAtomicTestBuilders?(): V1_AtomicTestBuilder[] {
return [
(
protocol: V1_AtomicTest,
Expand All @@ -294,7 +301,7 @@ export class DSL_Persistence_PureProtocolProcessorPlugin
];
}

override V1_getExtraAtomicTestTransformers?(): V1_AtomicTestTransformer[] {
V1_getExtraAtomicTestTransformers?(): V1_AtomicTestTransformer[] {
return [
(
metamodel: AtomicTest,
Expand All @@ -316,6 +323,37 @@ export class DSL_Persistence_PureProtocolProcessorPlugin
];
}

V1_getExtraAtomicTestProtocolSerializers(): V1_AtomicTestProtocolSerializer[] {
return [
(
atomicTestProtocol: V1_AtomicTest,
plugins: PureProtocolProcessorPlugin[],
): PlainObject<V1_AtomicTest> | undefined => {
if (atomicTestProtocol instanceof V1_PersistenceTest) {
return serialize(
V1_persistenceTestModelSchema(plugins),
atomicTestProtocol,
);
}
return undefined;
},
];
}

V1_getExtraAtomicTestProtocolDeserializers(): V1_AtomicTestProtocolDeserializer[] {
return [
(
json: PlainObject<V1_AtomicTest>,
plugins: PureProtocolProcessorPlugin[],
): V1_AtomicTest | undefined => {
if (json._type === V1_AtomicTestType.PERSISTENCE_TEST) {
return deserialize(V1_persistenceTestModelSchema(plugins), json);
}
return undefined;
},
];
}

V1_getExtraTriggerProtocolSerializers?(): V1_TriggerProtocolSerializer[] {
return [
(protocol: V1_Trigger): PlainObject<V1_Trigger> | undefined => {
Expand Down Expand Up @@ -344,7 +382,7 @@ export class DSL_Persistence_PureProtocolProcessorPlugin
];
}

override V1_getExtraTestableAssertionBuilders(): V1_TestableAssertion[] {
V1_getExtraTestableAssertionBuilders(): V1_TestableAssertion[] {
return [
(
atomicTest: AtomicTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ import {
V1_externalFormatDataModelSchema,
type V1_TestAssertion,
type V1_TestBatch,
V1_serializeAtomicTest,
V1_deserializeAtomicTest,
} from '@finos/legend-graph';
import {
deserializeArray,
Expand Down Expand Up @@ -1050,7 +1052,7 @@ export const V1_deserializeTrigger = (
* test
**********/

enum V1_AtomicTestType {
export enum V1_AtomicTestType {
PERSISTENCE_TEST = 'test',
}

Expand Down Expand Up @@ -1132,12 +1134,6 @@ export const V1_persistenceTestModelSchema = (
),
});

export const V1_serializePersistenceTest = (
protocol: V1_AtomicTest,
plugins: PureProtocolProcessorPlugin[],
): PlainObject<V1_AtomicTest> =>
serialize(V1_persistenceTestModelSchema(plugins), protocol);

/**********
* persistence
**********/
Expand Down Expand Up @@ -1165,23 +1161,16 @@ export const V1_persistenceModelSchema = (
(values) =>
serializeArray(
values,
(value: V1_AtomicTest) => V1_serializePersistenceTest(value, plugins),
(value: V1_AtomicTest) => V1_serializeAtomicTest(value, plugins),
{
skipIfEmpty: true,
INTERNAL__forceReturnEmptyInTest: true,
},
),
(values) =>
deserializeArray(
values,
(v) =>
v._type === V1_AtomicTestType.PERSISTENCE_TEST
? deserialize(V1_persistenceTestModelSchema(plugins), v)
: undefined,
{
skipIfEmpty: true,
},
),
deserializeArray(values, (v) => V1_deserializeAtomicTest(v, plugins), {
skipIfEmpty: true,
}),
),
trigger: custom(
(val) => V1_serializeTrigger(val, plugins),
Expand Down
33 changes: 0 additions & 33 deletions packages/legend-graph/src/graphManager/PureGraphManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
*/

import { AbstractPlugin } from '@finos/legend-shared';
import type { PureModel } from '../graph/PureModel.js';
import type { GraphManagerPluginManager } from './GraphManagerPluginManager.js';
import type { PackageableElement } from '../graph/metamodel/pure/packageableElements/PackageableElement.js';
import type { Testable } from '../graph/metamodel/pure/test/Testable.js';
import type {
AbstractPureGraphManager,
AbstractPureGraphManagerExtension,
} from './AbstractPureGraphManager.js';
import type { ObserverContext } from './action/changeDetection/CoreObserverHelper.js';
import type { AtomicTest } from '../graph/metamodel/pure/test/Test.js';

export type PureGraphManagerExtensionBuilder = (
graphManager: AbstractPureGraphManager,
Expand All @@ -35,11 +32,6 @@ export type ElementObserver = (
context: ObserverContext,
) => PackageableElement | undefined;

export type AtomicTestObserver = (
metamodel: AtomicTest,
context: ObserverContext,
) => AtomicTest | undefined;

/**
* Unlike `PureGraphPlugin`, this is for plugins of graph manager, i.e. operations acting
* on the graph instead of within the graph. As such processes involving grammar, compilation,
Expand All @@ -49,16 +41,6 @@ export type PureGrammarElementLabeler = (
metamodel: PackageableElement,
) => string | undefined;

export type TestableIDBuilder = (
testable: Testable,
graph: PureModel,
) => string | undefined;

export type TestableFinder = (
id: string,
graph: PureModel,
) => Testable | undefined;

export abstract class PureGraphManagerPlugin extends AbstractPlugin {
/**
* This helps to better type-check for this empty abtract type
Expand Down Expand Up @@ -102,19 +84,4 @@ export abstract class PureGraphManagerPlugin extends AbstractPlugin {
* Get the list of Pure grammar element labelers.
*/
getExtraPureGrammarElementLabelers?(): PureGrammarElementLabeler[];

/**
* Get the list of testable ID builders.
*/
getExtraTestableIDBuilders?(): TestableIDBuilder[];

/**
* Get the list of testable finders.
*/
getExtraTestableFinders?(): TestableFinder[];

/**
* Get the list of atomic test observers.
*/
getExtraAtomicTestObservers?(): AtomicTestObserver[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { AtomicTest } from '../graph/metamodel/pure/test/Test.js';
import type { Testable } from '../graph/metamodel/pure/test/Testable.js';
import type { PureModel } from '../graph/PureModel.js';
import type { ObserverContext } from './action/changeDetection/CoreObserverHelper.js';
import type { PureGraphManagerPlugin } from './PureGraphManagerPlugin.js';

export type AtomicTestObserver = (
metamodel: AtomicTest,
context: ObserverContext,
) => AtomicTest | undefined;

export type TestableIDBuilder = (
testable: Testable,
graph: PureModel,
) => string | undefined;

export type TestableFinder = (
id: string,
graph: PureModel,
) => Testable | undefined;

export interface Testable_PureGraphManagerPlugin_Extension
extends PureGraphManagerPlugin {
/**
* Get the list of testable ID builders.
*/
getExtraTestableIDBuilders?(): TestableIDBuilder[];

/**
* Get the list of testable finders.
*/
getExtraTestableFinders?(): TestableFinder[];

/**
* Get the list of atomic test observers.
*/
getExtraAtomicTestObservers?(): AtomicTestObserver[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
AtomicTest,
TestSuite,
} from '../../../graph/metamodel/pure/test/Test.js';
import type { Testable_PureGraphManagerPlugin_Extension } from '../../Testable_PureGraphManagerPlugin_Extension.js';
import { type ObserverContext, skipObserved } from './CoreObserverHelper.js';
import { observe_ExternalFormatData } from './DSL_Data_ObserverHelper.js';
import {
Expand Down Expand Up @@ -76,7 +77,10 @@ export function observe_AtomicTest(
return observe_ServiceTest(metamodel);
}
const extraAtomicTestBuilder = context.plugins.flatMap(
(plugin) => plugin.getExtraAtomicTestObservers?.() ?? [],
(plugin) =>
(
plugin as Testable_PureGraphManagerPlugin_Extension
).getExtraAtomicTestObservers?.() ?? [],
);

for (const builder of extraAtomicTestBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { PureModel } from '../../graph/PureModel.js';
import type { PureGraphManagerPlugin } from '../PureGraphManagerPlugin.js';
import { PackageableElement } from '../../graph/metamodel/pure/packageableElements/PackageableElement.js';
import type { Testable } from '../../graph/metamodel/pure/test/Testable.js';
import type { Testable_PureGraphManagerPlugin_Extension } from '../Testable_PureGraphManagerPlugin_Extension.js';

export const DEFAULT_TEST_SUITE_PREFIX = 'testSuite';
export const DEFAULT_TEST_PREFIX = 'test';
Expand All @@ -33,7 +34,12 @@ export const getNullableTestable = (
(e) => e instanceof PackageableElement && e.path === id,
) ??
plugins
.flatMap((plugin) => plugin.getExtraTestableFinders?.() ?? [])
.flatMap(
(plugin) =>
(
plugin as Testable_PureGraphManagerPlugin_Extension
).getExtraTestableFinders?.() ?? [],
)
.map((getter) => getter(id, graph))
.filter(isNonNullable)[0];

Expand All @@ -46,7 +52,12 @@ export const getNullableIDFromTestable = (
return testable.path;
}
return plugins
.flatMap((plugin) => plugin.getExtraTestableIDBuilders?.() ?? [])
.flatMap(
(plugin) =>
(
plugin as Testable_PureGraphManagerPlugin_Extension
).getExtraTestableIDBuilders?.() ?? [],
)
.map((getter) => getter(testable, graph))
.filter(isNonNullable)[0];
};
Loading

0 comments on commit 971a3cf

Please sign in to comment.