Skip to content

Commit

Permalink
Add unit tests for packages/actor-sparql-serialize-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jul 27, 2018
1 parent 416d7e0 commit 4a6199c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ export class ActorSparqlSerializeTree extends ActorSparqlSerializeFixedMediaType
}

const resultStream: NodeJS.EventEmitter = (<IActorQueryOperationOutputBindings> action).bindingsStream;
resultStream.on('error', (e) => data.emit('error', e));
resultStream.on('data', (bindings) => {
const rawBindings = bindings.toJS();
const reKeyedBindings: {[key: string]: RDF.Term} = {};
// Removes the '?' prefix
for (const key in rawBindings) {
reKeyedBindings[key.substr(1)] = rawBindings[key];
const bindingValue = rawBindings[key];
if (bindingValue) {
reKeyedBindings[key.substr(1)] = bindingValue;
}
}
bindingsArray.push(reKeyedBindings);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import {Bindings, BindingsStream} from "@comunica/bus-query-operation";
import {ActorSparqlSerialize} from "@comunica/bus-sparql-serialize";
import {Bus} from "@comunica/core";
import {ArrayIterator} from "asynciterator";
import {namedNode} from "rdf-data-model";
import {Readable} from "stream";
import {ActorSparqlSerializeTree} from "../lib/ActorSparqlSerializeTree";

const quad = require('rdf-quad');
const stringifyStream = require('stream-to-string');

describe('ActorSparqlSerializeTree', () => {
let bus;

Expand All @@ -15,8 +22,8 @@ describe('ActorSparqlSerializeTree', () => {
});

it('should be a ActorSparqlSerializeTree constructor', () => {
expect(new (<any> ActorSparqlSerializeTree)({ name: 'actor', bus })).toBeInstanceOf(ActorSparqlSerializeTree);
expect(new (<any> ActorSparqlSerializeTree)({ name: 'actor', bus })).toBeInstanceOf(ActorSparqlSerialize);
expect(new (<any> ActorSparqlSerializeTree)({ name: 'actor', bus, mediaTypes: { tree: 1.0 } }))
.toBeInstanceOf(ActorSparqlSerializeTree);
});

it('should not be able to create new ActorSparqlSerializeTree objects without \'new\'', () => {
Expand All @@ -26,17 +33,101 @@ describe('ActorSparqlSerializeTree', () => {

describe('An ActorSparqlSerializeTree instance', () => {
let actor: ActorSparqlSerializeTree;
let bindingsStream: BindingsStream;
let quadStream;
let streamError;
let variables;

beforeEach(() => {
actor = new ActorSparqlSerializeTree({ name: 'actor', bus });
actor = new ActorSparqlSerializeTree({ bus, name: 'actor', mediaTypes: { tree: 1.0 } });
bindingsStream = new ArrayIterator([
Bindings({ '?k1': namedNode('v1'), '?k2': null }),
Bindings({ '?k1': null, '?k2': namedNode('v2') }),
]);
quadStream = new ArrayIterator([
quad('http://example.org/a', 'http://example.org/b', 'http://example.org/c'),
quad('http://example.org/a', 'http://example.org/d', 'http://example.org/e'),
]);
streamError = new Readable();
streamError._read = () => streamError.emit('error', new Error());
variables = [ 'k1', 'k2' ];
});

it('should test', () => {
return expect(actor.test({ todo: true })).resolves.toEqual({ todo: true }); // TODO
describe('for getting media types', () => {
it('should test', () => {
return expect(actor.test({ mediaTypes: true })).resolves.toBeTruthy();
});

it('should run', () => {
return expect(actor.run({ mediaTypes: true })).resolves.toEqual({ mediaTypes: { tree: 1.0 }});
});
});

it('should run', () => {
return expect(actor.run({ todo: true })).resolves.toMatchObject({ todo: true }); // TODO
describe('for serializing', () => {
it('should test on tree', () => {
return expect(actor.test({ handle: <any> { type: 'bindings', bindingsStream },
handleMediaType: 'tree' })).resolves.toBeTruthy();
});

it('should not test on tree with a quad stream', () => {
return expect(actor.test({ handle: <any> { type: 'quads', quadStream },
handleMediaType: 'tree' }))
.rejects.toBeTruthy();
});

it('should not test on N-Triples', () => {
return expect(actor.test({ handle: <any> { type: 'bindings', bindingsStream },
handleMediaType: 'application/n-triples' }))
.rejects.toBeTruthy();
});

it('should not test on unknown types', () => {
return expect(actor.test(
{ handle: <any> { type: 'unknown' }, handleMediaType: 'tree' }))
.rejects.toBeTruthy();
});

// tslint:disable:no-trailing-whitespace
it('should run on a bindings stream', async () => {
return expect((await stringifyStream((await actor.run(
{handle: <any> { type: 'bindings', bindingsStream, variables },
handleMediaType: 'tree'})).handle.data))).toEqual(
`[
{
"k1": [
"v1"
]
},
{
"k2": [
"v2"
]
}
]`);
});

it('should run on a bindings stream with a context', async () => {
const context = { "@context": { k1: { "@singular": true }, k2: { "@singular": false } } };
return expect((await stringifyStream((await actor.run(
{handle: <any> { type: 'bindings', bindingsStream, variables, context },
handleMediaType: 'tree'})).handle.data))).toEqual(
`[
{
"k1": "v1"
},
{
"k2": [
"v2"
]
}
]`);
});

it('should emit an error when a bindings stream emits an error', async () => {
return expect(stringifyStream((await actor.run(
{handle: <any> { type: 'bindings', bindingsStream: streamError, variables },
handleMediaType: 'tree'})).handle.data)).rejects.toBeTruthy();
});
});
});
});

0 comments on commit 4a6199c

Please sign in to comment.