Skip to content

Commit

Permalink
moved some tests and fixed abs01-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jitsedesmet committed Apr 4, 2024
1 parent 1dec258 commit 2717d80
Show file tree
Hide file tree
Showing 92 changed files with 91 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { prepareEvaluatorActionContext } from '@comunica/actor-expression-evaluator-factory-base';
import { AlgebraTransformer } from '@comunica/actor-expression-evaluator-factory-base/lib/AlgebraTransformer';
import { NamedExtension } from '@comunica/actor-functions-wrapper-all/lib/implementation/NamedExtension';
import { namedFunctions } from '@comunica/actor-functions-wrapper-all/lib/implementation/NamedFunctions';
import { regularFunctions } from '@comunica/actor-functions-wrapper-all/lib/implementation/RegularFunctions';
import { specialFunctions } from '@comunica/actor-functions-wrapper-all/lib/implementation/SpecialFunctions';
import type { IExpressionFunction, MediatorFunctions } from '@comunica/bus-functions';
import { namedFunctions, regularFunctions, specialFunctions } from '@comunica/bus-functions/lib/implementation';
import { ActionContext } from '@comunica/core';
import * as E from '@comunica/expression-evaluator/lib/expressions';
import type * as C from '@comunica/expression-evaluator/lib/util/Consts';
import * as Err from '@comunica/expression-evaluator/lib/util/Errors';
import { getMockEEFactory } from '@comunica/jest';
import { NamedExtension } from 'packages/actor-functions-wrapper-all/lib/implementation/NamedExtension';
import { DataFactory } from 'rdf-data-factory';
import { expressionTypes, types } from 'sparqlalgebrajs/lib/algebra';
import { Wildcard } from 'sparqljs';
import * as E from '../../../lib/expressions';
import type * as C from '../../../lib/util/Consts';
import * as Err from '../../../lib/util/Errors';

const DF = new DataFactory();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dateTyped, dateTimeTyped, decimal, int } from '../util/Aliases';
import { dateTyped, dateTimeTyped, decimal, int } from '@comunica/expression-evaluator/test/util/Aliases';

// Data ------------------------------------------------------------------------
// @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { bool } from '../util/Aliases';
import { Notation } from '../util/TestTable';
import { runTestTable } from '../util/utils';
import { bool } from '@comunica/expression-evaluator/test/util/Aliases';
import { Notation } from '@comunica/expression-evaluator/test/util/TestTable';
import { runTestTable } from '@comunica/expression-evaluator/test/util/utils';
import { getMockEEFactory } from '@comunica/jest';
import { createFuncMediator } from '../util';
import * as Data from './_data';

/**
Expand Down Expand Up @@ -34,6 +36,9 @@ describe('We should respect the abs01 spec', () => {
notation: Notation.Infix,
operation: '>=',
aliases: bool,
exprEvalFactory: getMockEEFactory({
mediatorFunctions: createFuncMediator(),
}),
testTable: `
'abs(${n1})' 2 = false
'abs(${n2})' 2 = false
Expand Down
17 changes: 17 additions & 0 deletions packages/actor-functions-wrapper-all/test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { MediatorFunctions } from '@comunica/bus-functions';
import { Bus } from '@comunica/core';
import { ActorFunctionsWrapperAll } from '../lib';

export function createFuncActor() {
const bus: any = new Bus({ name: 'bus' });
const actor = new ActorFunctionsWrapperAll({ name: 'actor', bus });
return { actor, bus };
}

export function createFuncMediator(): MediatorFunctions {
return <MediatorFunctions> {
async mediate(action) {
return createFuncActor().actor.run(action);
},
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bus } from '@comunica/core';
import {
ActorTermComparatorFactoryInequalityFunctionsBased,
} from '../lib/ActorTermComparatorFactoryInequalityFunctionsBased';
} from '../lib';

describe('ActorTermComparatorFactoryInequalityFunctionsBased', () => {
let bus: any;
Expand All @@ -14,7 +14,11 @@ describe('ActorTermComparatorFactoryInequalityFunctionsBased', () => {
let actor: ActorTermComparatorFactoryInequalityFunctionsBased;

beforeEach(() => {
actor = new ActorTermComparatorFactoryInequalityFunctionsBased({ name: 'actor', bus });
actor = new ActorTermComparatorFactoryInequalityFunctionsBased({
name: 'actor',
bus,
mediatorFunctions:
});
});

it('should test', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { prepareEvaluatorActionContext } from '@comunica/actor-expression-evaluator-factory-base';
import { InternalEvaluator } from '@comunica/actor-expression-evaluator-factory-base/lib/InternalEvaluator';
import {
InequalityFunctionBasedComparator,
} from '@comunica/actor-term-comparator-factory-inequality-functions-based/lib/InequalityFunctionBasedComparator';
import { regularFunctions } from '@comunica/actor-functions-wrapper-all/lib/implementation/RegularFunctions';
import type { TermSparqlFunction } from '@comunica/bus-functions/lib/implementation';
import { regularFunctions } from '@comunica/bus-functions/lib/implementation';
import type {
ITermComparator,
} from '@comunica/bus-term-comparator-factory';
import type { ITermComparator } from '@comunica/bus-term-comparator-factory';
import { KeysExpressionEvaluator } from '@comunica/context-entries';
import { getMockEEFactory } from '@comunica/jest';
import type * as C from '@comunica/expression-evaluator/lib/util/Consts';
import { TypeURL, TypeURL as DT } from '@comunica/expression-evaluator/lib/util/Consts';
import { getMockEEActionContext, getMockInternalEvaluator } from '@comunica/jest';
import type { SuperTypeCallback } from '@comunica/types';
import type * as RDF from '@rdfjs/types';
import { LRUCache } from 'lru-cache';
import { DataFactory } from 'rdf-data-factory';

import { TypeURL, TypeURL as DT } from '../../../lib/util/Consts';
import type * as C from '../../../lib/util/Consts';
import { getMockEEActionContext } from '../../util/utils';
import { InequalityFunctionBasedComparator } from '../lib/InequalityFunctionBasedComparator';

const DF = new DataFactory();

Expand Down Expand Up @@ -52,13 +44,8 @@ function orderByFactory(typeDiscoveryCallback?: SuperTypeCallback): ITermCompara
cache: new LRUCache<string, any>({ max: 1_000 }),
}) :
getMockEEActionContext();
const factory = getMockEEFactory();
return new InequalityFunctionBasedComparator(
new InternalEvaluator(
prepareEvaluatorActionContext(context),
factory.mediatorFunctions,
factory.mediatorQueryOperation,
),
getMockInternalEvaluator(undefined, context),
<TermSparqlFunction<C.RegularOperator>> regularFunctions['='],
<TermSparqlFunction<C.RegularOperator>> regularFunctions['<'],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { InternalEvaluator } from '@comunica/actor-expression-evaluator-factory-base/lib/InternalEvaluator';
import { BindingsFactory } from '@comunica/bindings-factory';
import { getMockInternalEvaluator } from '@comunica/jest';
import { DataFactory } from 'rdf-data-factory';
import { expressionTypes, types } from 'sparqlalgebrajs/lib/algebra';
import { Wildcard } from 'sparqljs';
import * as E from '../../../lib/expressions';
import * as Err from '../../../lib/util/Errors';
import { getMockEvaluatorContext } from '../../util/utils';

const BF = new BindingsFactory();
const DF = new DataFactory();

describe('recursive evaluators', () => {
describe('AsyncRecursiveEvaluator', () => {
const evaluator = new InternalEvaluator(getMockEvaluatorContext());
const evaluator = getMockInternalEvaluator();

it('is able to evaluate operator', async() => {
expect(await evaluator.internalEvaluation(new E.IntegerLiteral(1), BF.bindings()))
Expand Down
12 changes: 7 additions & 5 deletions packages/expression-evaluator/test/util/TestTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { stringToTermPrefix, template } from './Aliases';
import { generalErrorEvaluation, generalEvaluate } from './generalEvaluation';
import type { TestTableConfig } from './utils';
import {ActorExpressionEvaluatorFactory} from "@comunica/bus-expression-evaluator-factory";

export enum Notation {
Infix,
Expand Down Expand Up @@ -36,13 +37,14 @@ abstract class Table<RowType extends Row> {
public abstract test(): void;

protected async testExpression(expr: string, result: string) {
const { config, additionalPrefixes } = this.def;
const { config, additionalPrefixes, exprEvalFactory } = this.def;
const aliases = this.def.aliases || {};
result = aliases[result] || result;
const evaluated = await generalEvaluate({
expression: template(expr, additionalPrefixes),
expectEquality: true,
generalEvaluationConfig: config,
exprEvalFactory
});
expect(evaluated.asyncResult).toEqual(stringToTermPrefix(result, additionalPrefixes));
}
Expand Down Expand Up @@ -156,25 +158,25 @@ export class BinaryTable extends Table<[string, string, string]> {
}

public test(): void {
this.parser.table.forEach(row => {
for (const row of this.parser.table) {
const result = row[2];
const { operation } = this.def;
const aliases = this.def.aliases || {};
it(`${this.format(operation, row)} should return ${result}`, async() => {
const expr = this.format(operation, <[string, string, string]> row.map(el => aliases[el] || el));
await this.testExpression(expr, result);
});
});
}

this.parser.errorTable.forEach(row => {
for (const row of this.parser.errorTable) {
const error = row[2];
const { operation } = this.def;
const aliases = this.def.aliases || {};
it(`${this.format(operation, row)} should error`, async() => {
const expr = this.format(operation, <[string, string, string]> row.map(el => aliases[el] || el));
await this.testErrorExpression(expr, error);
});
});
}
}

protected format(operation: string, row: [string, string, string]): string {
Expand Down
11 changes: 9 additions & 2 deletions packages/expression-evaluator/test/util/generalEvaluation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { InternalEvaluator } from '@comunica/actor-expression-evaluator-factory-base/lib/InternalEvaluator';
import { BindingsFactory } from '@comunica/bindings-factory';
import type { ActorExpressionEvaluatorFactory } from '@comunica/bus-expression-evaluator-factory';
import { getMockEEFactory } from '@comunica/jest';
import type { IActionContext } from '@comunica/types';
import type * as RDF from '@rdfjs/types';
Expand All @@ -17,6 +18,10 @@ export interface IGeneralEvaluationArg {
* Default: Check / true
*/
expectEquality?: boolean;
/**
* The factory that will create the evaluator used for this evaluation.
*/
exprEvalFactory?: ActorExpressionEvaluatorFactory;

// TODO: remove legacyContext in *final* update (probably when preparing the EE for function bussification)
legacyContext?: Partial<InternalEvaluator>;
Expand All @@ -29,6 +34,7 @@ Promise<{ asyncResult: RDF.Term; syncResult?: RDF.Term }> {
arg.expression,
bindings,
arg.generalEvaluationConfig || getMockEEActionContext(),
arg.exprEvalFactory,
);
return { asyncResult };
}
Expand All @@ -54,8 +60,9 @@ function parse(query: string) {
return sparqlQuery.input.expression;
}

async function evaluateAsync(expr: string, bindings: RDF.Bindings, actionContext: IActionContext): Promise<RDF.Term> {
const evaluator = (await getMockEEFactory()
async function evaluateAsync(expr: string, bindings: RDF.Bindings, actionContext: IActionContext,
exprEvalFactory?: ActorExpressionEvaluatorFactory): Promise<RDF.Term> {
const evaluator = (await (exprEvalFactory ?? getMockEEFactory())
.run({ algExpr: parse(expr), context: actionContext })).expressionEvaluator;
return evaluator.evaluate(bindings);
}
9 changes: 6 additions & 3 deletions packages/expression-evaluator/test/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { prepareEvaluatorActionContext } from '@comunica/actor-expression-evaluator-factory-base';
import type { ActorExpressionEvaluatorFactory } from '@comunica/bus-expression-evaluator-factory';
import { ActionContext } from '@comunica/core';
import { getMockEEFactory } from '@comunica/jest';
import type { GeneralSuperTypeDict, IActionContext, ISuperTypeProvider } from '@comunica/types';
Expand All @@ -20,9 +21,7 @@ export function getMockExpression(expr = '1+1'): Alg.Expression {
export function getMockEvaluatorContext(): IActionContext {
const factory = getMockEEFactory();

return prepareEvaluatorActionContext(getMockEEActionContext(),
factory.mediatorQueryOperation,
factory.mediatorFunctions);
return prepareEvaluatorActionContext(getMockEEActionContext());
}

export function getMockSuperTypeProvider(): ISuperTypeProvider {
Expand Down Expand Up @@ -68,6 +67,10 @@ export type TestTableConfig = ITestTableConfigBase & {
* Result can be '' if the message doesn't need to be checked.
*/
errorTable?: string;
/**
* The factory that will create the evaluator used for this evaluation.
*/
exprEvalFactory?: ActorExpressionEvaluatorFactory;
/**
* Test array that will check if a given error is thrown.
* Result can be '' if the message doesn't need to be checked.
Expand Down
23 changes: 21 additions & 2 deletions packages/jest/lib/expressions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ActorExpressionEvaluatorFactoryBase } from '@comunica/actor-expression-evaluator-factory-base';
import {
ActorExpressionEvaluatorFactoryBase,
prepareEvaluatorActionContext,
} from '@comunica/actor-expression-evaluator-factory-base';
import { InternalEvaluator } from '@comunica/actor-expression-evaluator-factory-base/lib/InternalEvaluator';
import { BindingsFactory } from '@comunica/bindings-factory';
import type { MediatorBindingsAggregatorFactory } from '@comunica/bus-bindings-aggeregator-factory';
import type { ActorExpressionEvaluatorFactory } from '@comunica/bus-expression-evaluator-factory';
import type { MediatorFunctions } from '@comunica/bus-functions';
import type { MediatorQueryOperation } from '@comunica/bus-query-operation';
import type { MediatorTermComparatorFactory } from '@comunica/bus-term-comparator-factory';
import { Bus } from '@comunica/core';
import { ActionContext, Bus } from '@comunica/core';
import type { IActionContext } from '@comunica/types';
import type * as RDF from '@rdfjs/types';
import type { Quad } from 'rdf-data-factory';
import { DataFactory } from 'rdf-data-factory';
Expand Down Expand Up @@ -67,6 +72,20 @@ export function nonLiteral(): RDF.Term {
return DF.namedNode('http://example.org/');
}

export function getMockEEActionContext(): IActionContext {
return new ActionContext({});
}

export function getMockInternalEvaluator(factory?: ActorExpressionEvaluatorFactory,
context?: IActionContext): InternalEvaluator {
const defFactory = factory ?? getMockEEFactory();
return new InternalEvaluator(
prepareEvaluatorActionContext(context ?? getMockEEActionContext()),
defFactory.mediatorFunctions,
defFactory.mediatorQueryOperation,
);
}

export function getMockEEFactory({ mediatorQueryOperation,
mediatorBindingsAggregatorFactory,
mediatorFunctions,
Expand Down

0 comments on commit 2717d80

Please sign in to comment.