From 869ad98232ecfb51a0ca52ae31a2facd96c33a18 Mon Sep 17 00:00:00 2001 From: Wout Schellaert Date: Mon, 20 May 2019 17:48:18 +0200 Subject: [PATCH] Remove old utility directory --- test/misc/Errors.test.ts | 16 ------- test/misc/Exists.test.ts | 2 +- test/misc/nonlexicals.test.ts | 7 --- test/util/TruthTable.ts | 4 +- test/util/utils.ts | 24 ++++++++-- util/Util.ts | 89 ----------------------------------- 6 files changed, 24 insertions(+), 118 deletions(-) delete mode 100644 test/misc/Errors.test.ts delete mode 100644 test/misc/nonlexicals.test.ts delete mode 100644 util/Util.ts diff --git a/test/misc/Errors.test.ts b/test/misc/Errors.test.ts deleted file mode 100644 index 62f3d592..00000000 --- a/test/misc/Errors.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { ExpressionError } from '../../lib/util/Errors'; -import { Example } from '../../util/Util'; - -describe('the evaluation of', () => { - describe('faulty expressions', () => { - it.skip('should throw ExpressionErrors', async () => { - const expr = new Example('str("testString") > 10'); - expect(expr.evaluate()).rejects.toThrow(ExpressionError); - }); - }); - describe('expressions with unimplemented features', () => { - it('should error', () => { - expect(() => new Example('avg(!?a)) > 10').evaluate()).toThrow(); - }); - }); -}); diff --git a/test/misc/Exists.test.ts b/test/misc/Exists.test.ts index 459402cc..5903bf89 100644 --- a/test/misc/Exists.test.ts +++ b/test/misc/Exists.test.ts @@ -1,5 +1,5 @@ import * as RDFDM from '@rdfjs/data-model'; -import { evaluate } from '../../util/Util'; +import { evaluate } from '../util/utils'; describe.skip('exists', () => { it('runs with mock existence hooks', () => { diff --git a/test/misc/nonlexicals.test.ts b/test/misc/nonlexicals.test.ts deleted file mode 100644 index 784b13df..00000000 --- a/test/misc/nonlexicals.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -describe.skip('non-lexical', () => { - // TODO Most of the operators (and other functions) should fail when - // non lexical literals are given - it('thing', () => { - expect(1 + 1).toBe(2); - }); -}); diff --git a/test/util/TruthTable.ts b/test/util/TruthTable.ts index 77857923..32fa421c 100644 --- a/test/util/TruthTable.ts +++ b/test/util/TruthTable.ts @@ -1,7 +1,7 @@ import * as RDF from 'rdf-js'; -import { stringToTerm, termToString } from 'rdf-string'; -import { evaluate } from '../../util/Util'; +import { termToString } from 'rdf-string'; +import { evaluate } from '../util/utils'; /* * Maps short strings to longer RDF term-literals for easy use in making diff --git a/test/util/utils.ts b/test/util/utils.ts index 5c92fa70..a6c8cf50 100644 --- a/test/util/utils.ts +++ b/test/util/utils.ts @@ -1,11 +1,12 @@ -import { literal } from '@rdfjs/data-model'; import * as RDF from 'rdf-js'; + +import { literal } from '@rdfjs/data-model'; import { stringToTerm, termToString } from 'rdf-string'; +import { translate } from 'sparqlalgebrajs'; import { AsyncEvaluator, AsyncEvaluatorConfig } from '../../lib/evaluators/AsyncEvaluator'; import { Bindings } from '../../lib/Types'; import { ExpressionError } from '../../lib/util/Errors'; -import { parse } from '../../util/Util'; export function testAll(exprs: string[], config?: AsyncEvaluatorConfig) { exprs.forEach((_expr) => { @@ -38,8 +39,25 @@ export function testAllErrors(exprs: string[], config?: AsyncEvaluatorConfig) { }); } +function template(expr: string) { + return ` +PREFIX xsd: +PREFIX fn: +PREFIX err: +PREFIX rdf: + +SELECT * WHERE { ?s ?p ?o FILTER (${expr})} +`; +} + +function parse(query: string) { + const sparqlQuery = translate(query); + // Extract filter expression from complete query + return sparqlQuery.input.expression; +} + export function evaluate(expr: string, config?: AsyncEvaluatorConfig): Promise { - const evaluator = new AsyncEvaluator(parse(expr), config); + const evaluator = new AsyncEvaluator(parse(template(expr)), config); return evaluator.evaluate(Bindings({})); } diff --git a/util/Util.ts b/util/Util.ts deleted file mode 100644 index eca650db..00000000 --- a/util/Util.ts +++ /dev/null @@ -1,89 +0,0 @@ -import * as RDFDM from '@rdfjs/data-model'; -import * as RDF from 'rdf-js'; -import { Algebra as Alg, translate } from 'sparqlalgebrajs'; - -import * as C from '../lib/util/Consts'; - -import { AsyncEvaluator } from '../lib/evaluators/AsyncEvaluator'; -import { Bindings } from '../lib/Types'; -import { TypeURL as DT } from '../lib/util/Consts'; - -export class Example { - expression: Alg.Expression; - mapping: () => Bindings; - - constructor(expr: string, mapping?: () => Bindings) { - this.expression = parse(expr); - this.mapping = mapping || (() => Bindings({})); - } - - async evaluate(mapping?: Bindings): Promise { - const evaluator = new AsyncEvaluator(this.expression); - return mapping - ? evaluator.evaluate(mapping) - : evaluator.evaluate(this.mapping()); - } -} - -export const example1 = (() => { - const str = '((?age + ?otherAge) = "50"^^xsd:integer) && (?joinYear > "2005-01-01T00:00:00Z"^^xsd:dateTime)'; - const mapping = () => { - const randAge = Math.floor((Math.random() * 100)); - const beSame = Math.random() > 0.7; - const randOtherAge = (beSame) - ? 50 - randAge - : Math.floor((Math.random() * 100)); - const randYear = 1980 + Math.floor(Math.random() * 40); - - return Bindings({ - age: RDFDM.literal(randAge.toString(), RDFDM.namedNode(DT.XSD_INTEGER)), - joinYear: RDFDM.literal(`${randYear}-03-03T00:00:00Z`, RDFDM.namedNode(DT.XSD_DATE_TIME)), - otherAge: RDFDM.literal(randOtherAge.toString(), RDFDM.namedNode(DT.XSD_INTEGER)), - }); - }; - return new Example(str, mapping); -})(); - -export function evaluate(expr: string, bindings = Bindings({})): Promise { - const evaluator = new AsyncEvaluator(parse(expr)); - return evaluator.evaluate(bindings); -} - -export function mockExistence(expression: Alg.ExistenceExpression, mapping: Bindings): Promise { - return Promise.resolve(true); -} - -export function mockAggregate(expression: Alg.AggregateExpression): Promise { - switch (expression.aggregator) { - case 'count': - case 'sum': - case 'min': - case 'max': - case 'avg': return Promise.resolve(RDFDM.literal('3.14', C.make(DT.XSD_FLOAT))); - case 'groupConcat': return Promise.resolve(RDFDM.literal('term term term')); - case 'sample': return Promise.resolve(RDFDM.literal('MockTerm')); - default: throw new Error('woops y daisy'); - } -} - -export function parse(expr: string): Alg.Expression { - // Build mock SPARQL query with expression in the filter - const prefixes = `PREFIX xsd: - PREFIX fn: - PREFIX err: - PREFIX rdf: `; - const queryString = `${prefixes} SELECT * WHERE { ?s ?p ?o FILTER (${expr})}`; - const sparqlQuery = translate(queryString); - // Extract filter expression from complete query - return sparqlQuery.input.expression; -} - -export function parseFull(expr: string): Alg.Operation { - const prefixes = `PREFIX xsd: - PREFIX fn: - PREFIX err: - PREFIX rdf: `; - const queryString = `${prefixes} ${expr}`; - const sparqlQuery = translate(queryString); - return sparqlQuery; -}