Skip to content

Commit

Permalink
Update to Comunica 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jun 5, 2023
1 parent 0fb7cfd commit 935eebd
Show file tree
Hide file tree
Showing 13 changed files with 4,746 additions and 5,855 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
Expand All @@ -9,6 +11,8 @@ module.exports = {
'@rubensworks'
],
rules: {
'no-implicit-coercion': 'off'
'no-implicit-coercion': 'off',
'import/no-nodejs-modules': 'off',
'unicorn/filename-case': 'off',
}
};
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
key: ${{ runner.os }}-lint-modules-${{ hashFiles('**/yarn.lock') }}
- uses: actions/setup-node@v2
with:
node-version: 14.x
node-version: 18.x
- run: yarn install
- run: yarn run lint

Expand All @@ -22,9 +22,10 @@ jobs:
matrix:
os: [ubuntu-latest]
node-version:
- 12.x
- 14.x
- 16.x
- 18.x
- 20.x
steps:
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
Expand Down
5 changes: 3 additions & 2 deletions bin/Runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
/* eslint-disable no-sync,unicorn/filename-case */
import * as fs from 'fs';
import * as Path from 'path';
import minimist = require('minimist');
Expand Down Expand Up @@ -45,7 +44,9 @@ const writeConfig: IWriteConfig = {
};

// Check if directory exists
// eslint-disable-next-line no-sync
if (!fs.existsSync(writeConfig.directory)) {
// eslint-disable-next-line no-sync
fs.mkdirSync(writeConfig.directory);
}

Expand All @@ -54,7 +55,7 @@ const query: string = args._.pop();

// Fetch the data sources given
const dataSources: string[] = [];
while (args._.length) {
while (args._.length > 0) {
dataSources.push(args._.pop());
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Colors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/group-exports */
export const RESET = '\u001B[0m';
export const RED = '\u001B[31m';
export const GREEN = '\u001B[32m';
Expand All @@ -14,3 +15,5 @@ export const CYAN = '\u001B[36m';
export function inColor(element: string, color: string): string {
return `${color}${element}${RESET}`;
}

/* eslint-enable import/group-exports */
2 changes: 2 additions & 0 deletions lib/HttpInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as fs from 'fs';
import * as Path from 'path';
import type { Readable } from 'stream';
import type { IInterceptOptions, IMockedFile, IWriteConfig } from './IRecorder';

// eslint-disable-next-line import/no-commonjs
const stringifyStream = require('stream-to-string');

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/IRecorder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Bindings } from '@comunica/bus-query-operation';
import type { Quad } from 'rdf-js';
/* eslint-disable import/group-exports */
import type * as RDF from 'rdf-js';
import type { QueryType } from './QueryExecutor';

/**
Expand Down Expand Up @@ -42,6 +42,8 @@ export interface IQuerySource {
*/
export interface IQueryResult {
type: QueryType;
value: Bindings[] | boolean | Quad[];
value: RDF.Bindings[] | boolean | RDF.Quad[];
variables?: string[];
}

/* eslint-enable import/group-exports */
42 changes: 23 additions & 19 deletions lib/QueryExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* eslint-disable no-case-declarations */
import { newEngine } from '@comunica/actor-init-sparql';
import type { Bindings } from '@comunica/bus-query-operation';
import type { Quad } from 'rdf-js';
import { QueryEngine } from '@comunica/query-sparql';
import type { IQueryBindingsEnhanced } from '@comunica/types';
import type * as RDF from 'rdf-js';
import type { IQueryResult, IQuerySource } from './IRecorder';

/**
* A class which executes SPARQL-queries on a TPF endpoint that can be recorded
*/
// eslint-disable-next-line import/group-exports
export class QueryExecutor {
public readonly myEngine: any;
public readonly myEngine: QueryEngine;

public constructor(engine?: any) {
// Use comunica engine by default.
this.myEngine = engine || newEngine();
this.myEngine = engine || new QueryEngine();
}

/**
Expand All @@ -29,28 +30,30 @@ export class QueryExecutor {
return new Promise(async(resolve, reject) => {
switch (queryType) {
case QueryType.SELECT:
const rss: Bindings[] = [];
const rs = await this.myEngine.query(queryString, context);
await rs.bindingsStream.on('data', (data: Bindings) => {
const rss: RDF.Bindings[] = [];
const rs = <IQueryBindingsEnhanced> await this.myEngine.query(queryString, <any>context);
const metadata = await rs.metadata();
const bindingsStream = await rs.execute();
bindingsStream.on('data', (data: RDF.Bindings) => {
rss.push(data);
});
rs.bindingsStream.on('error', reject);
await rs.bindingsStream.on('end', async() => {
resolve({ type: QueryType.SELECT, value: rss, variables: rs.variables });
bindingsStream.on('error', reject);
bindingsStream.on('end', () => {
resolve({ type: QueryType.SELECT, value: rss, variables: metadata.variables.map(vr => vr.value) });
});
break;
case QueryType.ASK:
const ra = await this.myEngine.query(queryString, context);
resolve({ type: QueryType.ASK, value: await ra.booleanResult });
const booleanResult = await this.myEngine.queryBoolean(queryString, <any>context);
resolve({ type: QueryType.ASK, value: booleanResult });
break;
case QueryType.CONSTRUCT:
const rsc: Quad[] = [];
const rc = await this.myEngine.query(queryString, context);
await rc.quadStream.on('data', (data: Quad) => {
const rsc: RDF.Quad[] = [];
const quadStream = await this.myEngine.queryQuads(queryString, <any>context);
quadStream.on('data', (data: RDF.Quad) => {
rsc.push(data);
});
rc.quadStream.on('error', reject);
await rc.quadStream.on('end', async() => {
quadStream.on('error', reject);
quadStream.on('end', async() => {
resolve({ type: QueryType.CONSTRUCT, value: rsc });
});
break;
Expand Down Expand Up @@ -113,9 +116,10 @@ export class QueryExecutor {
/**
* The different QueryTypes the comunica engine and the recorder support
*/
// eslint-disable-next-line no-shadow
// eslint-disable-next-line import/group-exports
export enum QueryType {
ASK,
SELECT,
CONSTRUCT,
}
/* eslint-enable no-case-declarations */
14 changes: 6 additions & 8 deletions lib/ResultWriter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs';
import * as Path from 'path';
import { ActorSparqlSerializeSparqlJson } from '@comunica/actor-sparql-serialize-sparql-json';
import type { Bindings } from '@comunica/bus-query-operation';
import { ActorQueryResultSerializeSparqlJson } from '@comunica/actor-query-result-serialize-sparql-json';
import type { Quad } from 'n3';
import { Writer } from 'n3';
import type * as RDF from 'rdf-js';
Expand Down Expand Up @@ -55,7 +54,7 @@ export class ResultWriter {
private getResultString(results: IQueryResult): string {
switch (results.type) {
case QueryType.SELECT:
return this.bindingsToSparqlJsonResult(<Bindings[]> results.value, results.variables);
return this.bindingsToSparqlJsonResult(<RDF.Bindings[]> results.value, results.variables);
case QueryType.ASK:
return this.booleanToSparqlJsonResult(<boolean> results.value);
case QueryType.CONSTRUCT:
Expand Down Expand Up @@ -83,17 +82,16 @@ export class ResultWriter {
* Transform the bindings to the SPARQLJsonResult format used for testing
* @param bindings The bindings returned from the query-engine
*/
private bindingsToSparqlJsonResult(bindings: Bindings[], variables: string[]): string {
private bindingsToSparqlJsonResult(bindings: RDF.Bindings[], variables: string[]): string {
const head: any = {};
head.vars = variables.map(key => key.slice(1));
head.vars = variables.map(key => key);

const results: any = {};
results.bindings = [];
for (const binding of bindings) {
const bres: any = {};
binding.keySeq().forEach((key: string) => {
const value: RDF.Term = binding.get(key);
bres[key.slice(1)] = ActorSparqlSerializeSparqlJson.bindingToJsonBindings(value);
binding.forEach((value: RDF.Term, key: RDF.Variable) => {
bres[key.value] = ActorQueryResultSerializeSparqlJson.bindingToJsonBindings(value);
});
results.bindings.push(bres);
}
Expand Down
37 changes: 11 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@
"test"
],
"jest": {
"globals": {
"ts-jest": {
"tsConfig": "test/tsconfig.json"
}
},
"transform": {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": ["ts-jest", {}]
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
Expand All @@ -54,46 +49,36 @@
"scripts": {
"test": "jest ${1}",
"test-watch": "jest ${1} --watch",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"lint": "eslint . --ext .ts --cache",
"build": "tsc",
"validate": "npm ls",
"prepare": "npm run build",
"version": "manual-git-changelog onversion"
},
"dependencies": {
"@comunica/actor-init-sparql": "^1.22.3",
"@comunica/query-sparql": "^2.7.1",
"@comunica/actor-query-result-serialize-sparql-json": "^2.7.0",
"@types/minimist": "^1.2.0",
"@types/n3": "^1.10.4",
"@types/nock": "^10.0.3",
"@types/node": "^17.0.9",
"@types/node": "^20.2.5",
"minimist": "^1.2.0",
"nock": "^10.0.6",
"node-fetch": "^2.6.7",
"stream-to-string": "^1.2.0"
},
"devDependencies": {
"@comunica/actor-sparql-serialize-sparql-json": "^1.22.0",
"@rubensworks/eslint-config": "^1.0.1",
"@comunica/bindings-factory": "^2.7.0",
"@rubensworks/eslint-config": "^2.0.0",
"@types/jest": "^24.0.17",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"coveralls": "^3.0.6",
"fs-extra": "^8.1.0",
"eslint": "^7.9.0",
"eslint-config-es": "3.23.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^25.0.0",
"eslint-plugin-tsdoc": "^0.2.7",
"eslint-plugin-unused-imports": "^2.0.0",
"jest": "^24.8.0",
"jest-rdf": "^1.3.0",
"jest": "^29.5.0",
"jest-rdf": "^1.8.0",
"manual-git-changelog": "^1.0.1",
"pre-commit": "^1.2.2",
"rdf-quad": "^1.4.0",
"rdf-test-suite": "^1.9.3",
"ts-jest": "^24.0.2",
"rdf-quad": "^1.5.0",
"rdf-test-suite": "^1.24.0",
"ts-jest": "^29.1.0",
"typescript": "^4.3.5"
}
}
12 changes: 2 additions & 10 deletions test/HttpInterceptor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as nock from 'nock';
import fetch from 'node-fetch';
import { HttpInterceptor } from '../lib/HttpInterceptor';

global.fetch = fetch;
globalThis.fetch = fetch;

describe('HttpInterceptor', () => {
beforeEach(() => {
Expand Down Expand Up @@ -34,15 +34,7 @@ describe('HttpInterceptor', () => {
await interceptor.interceptResponse({
input: 'http://ex.org/path/',
});
await new Promise((resolve, reject) => {
fs.readdir(jestTestFolder, (error, files) => {
if (error) {
reject(error);
} else {
resolve(files);
}
});
});
await new Promise(resolve => setTimeout(resolve, 100));
const filename: string = crypto.createHash('sha1')
.update(fn)
.digest('hex');
Expand Down
6 changes: 2 additions & 4 deletions test/QueryExecutor-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { QueryExecutor } from '../lib/QueryExecutor';

jest.useFakeTimers();

describe('QueryExecutor', () => {
const queryExecutor: QueryExecutor = new QueryExecutor();

Expand All @@ -14,8 +12,8 @@ describe('QueryExecutor', () => {
describe('#runQuery', () => {
it('should resolve SELECT', () => {
return expect(queryExecutor.runQuery(
'SELECT * WHERE { ?s ?p <http://dbpedia.org/resource/Belgium>. ?s ?p ?o } LIMIT 5',
[ 'TPF@http://fragments.dbpedia.org/2015/en' ],
'SELECT * WHERE { ?s ?p ?o } LIMIT 5',
[ 'TPF@https://fragments.dbpedia.org/2015/en' ],
fetch,
)).resolves.toBeTruthy();
});
Expand Down
23 changes: 12 additions & 11 deletions test/ResultWriter-test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import * as fs from 'fs';
import * as Path from 'path';
import type { Bindings } from '@comunica/bus-query-operation';
import { BindingsFactory } from '@comunica/bindings-factory';
import type * as RDF from '@rdfjs/types';
import * as fse from 'fs-extra';
import { Map } from 'immutable';
import type { Quad } from 'n3';
import { DataFactory } from 'n3';
import type { IWriteConfig } from '../lib/IRecorder';
import { QueryType } from '../lib/QueryExecutor';
import { ResultWriter } from '../lib/ResultWriter';

const BF = new BindingsFactory();

const { namedNode, literal, defaultGraph, quad } = DataFactory;

const binding: Bindings = Map(
{
'?o': namedNode('http://ex.org/oa'),
'?s': namedNode('http://ex.org/os'),
'?p': namedNode('http://ex.org/op'),
},
);
const bindings: Bindings[] = [ binding ];
const binding: RDF.Bindings = BF.fromRecord({
o: namedNode('http://ex.org/oa'),
s: namedNode('http://ex.org/os'),
p: namedNode('http://ex.org/op'),
});
const bindings: RDF.Bindings[] = [ binding ];

const bool = false;

Expand Down Expand Up @@ -54,7 +55,7 @@ describe('ResultWriter', () => {
await resultWriter.writeResultsToFile({
type: QueryType.SELECT,
value: bindings,
variables: [ '?o', '?s', '?p' ],
variables: [ 'o', 's', 'p' ],
});

const filename: string = Path.join(writeConfig.directory, 'result.srj');
Expand Down
Loading

0 comments on commit 935eebd

Please sign in to comment.