Skip to content

Commit

Permalink
Add mediator tests for expression evaluation buses
Browse files Browse the repository at this point in the history
  • Loading branch information
jitsedesmet authored Oct 9, 2024
1 parent b37d7e0 commit c2d2e45
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 9 deletions.
61 changes: 61 additions & 0 deletions engines/query-sparql/test/Mediators-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type {
IActionBindingsAggregatorFactory,
IActorBindingsAggregatorFactoryOutput,
} from '@comunica/bus-bindings-aggregator-factory';
import type { IActionContextPreprocess, IActorContextPreprocessOutput } from '@comunica/bus-context-preprocess';
import type { IActionDereference, IActorDereferenceOutput } from '@comunica/bus-dereference';
import type {
IActionDereferenceRdf,
IActorDereferenceRdfOutput,
} from '@comunica/bus-dereference-rdf';
import type {
IActionExpressionEvaluatorFactory,
IActorExpressionEvaluatorFactoryOutput,
} from '@comunica/bus-expression-evaluator-factory';
import type { IActionFunctionFactory, IActorFunctionFactoryOutput } from '@comunica/bus-function-factory';
import type { IActionHashBindings, IActorHashBindingsOutput } from '@comunica/bus-hash-bindings';
import type { IActionHashQuads, IActorHashQuadsOutput } from '@comunica/bus-hash-quads';
import type { IActionHttp, IActorHttpOutput } from '@comunica/bus-http';
Expand Down Expand Up @@ -48,16 +57,23 @@ import type {
} from '@comunica/bus-rdf-serialize';
import type { IActionRdfUpdateHypermedia, IActorRdfUpdateHypermediaOutput } from '@comunica/bus-rdf-update-hypermedia';
import type { IActionRdfUpdateQuads, IActorRdfUpdateQuadsOutput } from '@comunica/bus-rdf-update-quads';
import type {
IActionTermComparatorFactory,
IActorTermComparatorFactoryOutput,
} from '@comunica/bus-term-comparator-factory';
import { ActionContext, failTest } from '@comunica/core';
import type { Mediate, IAction, IActorOutput, IActorTest, Bus } from '@comunica/core';
import type { IMediatorTypeAccuracy } from '@comunica/mediatortype-accuracy';
import type { IMediatorTypeJoinCoefficients } from '@comunica/mediatortype-join-coefficients';
import type { Runner } from '@comunica/runner';
import { instantiateComponent } from '@comunica/runner';
import type { IActionContext, IQueryOperationResultBindings } from '@comunica/types';
import { DataFactory } from 'rdf-data-factory';
import { Algebra } from 'sparqlalgebrajs';
import { QueryEngineFactory } from '../lib';

const queryEngineFactory = new QueryEngineFactory();
const DF = new DataFactory();

describe('System test: mediators', () => {
let runner: Runner;
Expand All @@ -70,6 +86,23 @@ describe('System test: mediators', () => {
});
const context: IActionContext = new ActionContext();

addTest<IActionBindingsAggregatorFactory, IActorBindingsAggregatorFactoryOutput>(
'bindings-aggregator-factory',
':query-operation/actors#group',
'mediatorBindingsAggregatorFactory',
{ expr: {
type: Algebra.types.EXPRESSION,
expressionType: Algebra.expressionTypes.AGGREGATE,
aggregator: 'avg',
distinct: false,
expression: {
type: Algebra.types.EXPRESSION,
expressionType: Algebra.expressionTypes.TERM,
term: DF.variable('x'),
},
}},
`Creation of Aggregator failed: none of the configured actors were able to handle avg`,
);
addTest<IActionContextPreprocess, IActorContextPreprocessOutput>(
'context-preprocess',
':context-preprocess/actors#query-source-identify',
Expand All @@ -91,6 +124,27 @@ describe('System test: mediators', () => {
<any> { handle: <any> { data: <any> undefined, context, mediaType: 'text/css', url: 'http://example.org/' }},
`RDF dereferencing failed: none of the configured parsers were able to handle the media type text/css for http://example.org/`,
);
addTest<IActionExpressionEvaluatorFactory, IActorExpressionEvaluatorFactoryOutput>(
'expression-evaluator-factory',
':query-operation/actors#extend',
'mediatorExpressionEvaluatorFactory',
{ algExpr: <any> {
type: Algebra.types.EXPRESSION,
expressionType: Algebra.expressionTypes.TERM,
term: DF.variable('x'),
}},
`Creation of Expression Evaluator failed`,
);
addTest<IActionFunctionFactory, IActorFunctionFactoryOutput>(
'function-factory',
':expression-evaluator-factory/actors#default',
'mediatorFunctionFactory',
{
functionName: 'http://example.org/',
requireTermExpression: false,
},
`Creation of function evaluator failed: no configured actor was able to evaluate function http://example.org/`,
);
addTest<IActionHashBindings, IActorHashBindingsOutput>(
'hash-bindings',
':query-operation/actors#distinct',
Expand Down Expand Up @@ -249,6 +303,13 @@ IMediatorTypeJoinCoefficients
{},
`RDF updating failed: none of the configured actors were able to handle an update`,
);
addTest<IActionTermComparatorFactory, IActorTermComparatorFactoryOutput>(
'term-comparator-factory',
':query-operation/actors#orderby',
'mediatorTermComparatorFactory',
{},
`Creation of term comparator failed`,
);

/**
* Add a test for a given bus mediator.
Expand Down
4 changes: 2 additions & 2 deletions engines/query-sparql/test/QuerySparql-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,15 @@ describe('System test: QuerySparql', () => {
const context = <any> { sources: [ store ]};
context.extensionFunctions = baseFunctions;
await expect(engine.query(baseQuery('nonExist'), context)).rejects.toThrow(
`ActorFunctionFactory_default_bus failed to handle an action`,
`Creation of function evaluator failed: no configured actor was able to evaluate function http://example.org/functions#nonExist`,
);
});

it('rejects when creator returns null', async() => {
const context = <any> { sources: [ store ]};
context.extensionFunctionCreator = () => null;
await expect(engine.query(baseQuery('nonExist'), context)).rejects.toThrow(
`ActorFunctionFactory_default_bus failed to handle an action`,
`Creation of function evaluator failed: no configured actor was able to evaluate function http://example.org/functions#nonExist`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ExpressionEvaluator implements IExpressionEvaluator {
return result.coerceEBV();
}

public evaluateAsEvaluatorExpression(mapping: RDF.Bindings): Promise<Expression> {
public evaluateAsEvaluatorExpression(mapping: RDF.Bindings): Promise<TermExpression> {
return this.evaluatorExpressionEvaluation(this.expr, mapping);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ IActorBindingsAggregatorFactoryOutput,
TS
> {
protected readonly mediatorExpressionEvaluatorFactory: MediatorExpressionEvaluatorFactory;

/* eslint-disable max-len */
/**
* @param args - @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* @param args -
* \ @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* \ @defaultNested {Creation of Aggregator failed: none of the configured actors were able to handle ${action.expr.aggregator}} busFailMessage
*/
/* eslint-enable max-len */
protected constructor(args: IActorBindingsAggregatorFactoryArgs<TS>) {
super(args);
this.mediatorExpressionEvaluatorFactory = args.mediatorExpressionEvaluatorFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export abstract class ActorExpressionEvaluatorFactory<TS = undefined> extends
protected mediatorMergeBindingsContext: MediatorMergeBindingsContext;

/**
* @param args - @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* @param args -
* \ @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* \ @defaultNested {Creation of Expression Evaluator failed} busFailMessage
*/
public constructor(args: IActorExpressionEvaluatorFactoryArgs<TS>) {
super(args);
Expand Down
6 changes: 5 additions & 1 deletion packages/bus-function-factory/lib/ActorFunctionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import type { Algebra as Alg } from 'sparqlalgebrajs';
*/
export abstract class ActorFunctionFactory<TS = undefined> extends
Actor<IActionFunctionFactory, IActorTest, IActorFunctionFactoryOutput, TS> {
/* eslint-disable max-len */
/**
* @param args - @defaultNested {<default_bus> a <cbff:components/BusFunctionFactory.jsonld#BusFunctionFactory>} bus
* @param args -
* \ @defaultNested {<default_bus> a <cbff:components/BusFunctionFactory.jsonld#BusFunctionFactory>} bus
* \ @defaultNested {Creation of function evaluator failed: no configured actor was able to evaluate function ${action.functionName}} busFailMessage
*/
/* eslint-enable max-len */
public constructor(args: IActorFunctionFactoryArgs<TS>) {
super(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export abstract class ActorTermComparatorFactory<TS = undefined> extends
protected readonly mediatorMergeBindingsContext: MediatorMergeBindingsContext;

/**
* @param args - @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* @param args -
* \ @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
* \ @defaultNested {Creation of term comparator failed} busFailMessage
*/
public constructor(args: IActorTermComparatorFactoryArgs<TS>) {
super(args);
Expand Down
2 changes: 1 addition & 1 deletion packages/types/lib/ExpressionEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface IExpressionEvaluator extends IInternalEvaluator {
*/
evaluateAsEBV: (mapping: RDF.Bindings) => Promise<boolean>;

evaluateAsEvaluatorExpression: (mapping: RDF.Bindings) => Promise<Expression>;
evaluateAsEvaluatorExpression: (mapping: RDF.Bindings) => Promise<TermExpression>;
}

export interface IInternalEvaluator {
Expand Down

0 comments on commit c2d2e45

Please sign in to comment.