Skip to content
Permalink
Browse files

Check edge-cases for empty bindings and results

  • Loading branch information...
rubensworks committed Aug 24, 2018
1 parent 3531f7f commit 031c30c93e6e2e3a56b1dd2cb47013a30344998e
Showing with 53 additions and 19 deletions.
  1. +23 −19 lib/SparqlXmlParser.ts
  2. +30 −0 test/SparqlXmlParser-test.ts
@@ -56,27 +56,31 @@ export class SparqlXmlParser {
*/
public parseXmlBindings(rawBindings: any): IBindings {
const bindings: IBindings = {};
const bindingsArray = Array.isArray(rawBindings.children.binding)
? rawBindings.children.binding : [ rawBindings.children.binding ];
for (const binding of bindingsArray) {
const key = binding.attribs.name;
let value: RDF.Term = null;
if (binding.children.bnode) {
value = this.dataFactory.blankNode(binding.children.bnode.value);
} else if (binding.children.literal) {
if (binding.children.literal.attribs && binding.children.literal.attribs['xml:lang']) {
value = this.dataFactory.literal(binding.children.literal.value,
binding.children.literal.attribs['xml:lang']);
} else if (binding.children.literal.attribs && binding.children.literal.attribs.datatype) {
value = this.dataFactory.literal(binding.children.literal.value,
this.dataFactory.namedNode(binding.children.literal.attribs.datatype));
} else {
value = this.dataFactory.literal(binding.children.literal.value);
if (rawBindings.children) {
const bindingsArray = Array.isArray(rawBindings.children.binding)
? rawBindings.children.binding : [rawBindings.children.binding];
for (const binding of bindingsArray) {
if (binding.attribs) {
const key = binding.attribs.name;
let value: RDF.Term = null;
if (binding.children.bnode) {
value = this.dataFactory.blankNode(binding.children.bnode.value);
} else if (binding.children.literal) {
if (binding.children.literal.attribs && binding.children.literal.attribs['xml:lang']) {
value = this.dataFactory.literal(binding.children.literal.value,
binding.children.literal.attribs['xml:lang']);
} else if (binding.children.literal.attribs && binding.children.literal.attribs.datatype) {
value = this.dataFactory.literal(binding.children.literal.value,
this.dataFactory.namedNode(binding.children.literal.attribs.datatype));
} else {
value = this.dataFactory.literal(binding.children.literal.value);
}
} else {
value = this.dataFactory.namedNode(binding.children.uri.value);
}
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = value;
}
} else {
value = this.dataFactory.namedNode(binding.children.uri.value);
}
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = value;
}
return bindings;
}
@@ -191,6 +191,36 @@ describe('SparqlXmlParser', () => {
]);
});

it('should convert a SPARQL XML response with an empty result', async () => {
return expect(await arrayifyStream(parser.parseXmlResultsStream(streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
</head>
<results>
<result />
</results>
</sparql>
`))))
.toEqual([{}]);
});

it('should convert a SPARQL XML response with an empty binding', async () => {
return expect(await arrayifyStream(parser.parseXmlResultsStream(streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
</head>
<results>
<result>
<binding />
</result>
</results>
</sparql>
`))))
.toEqual([{}]);
});

it('should convert a SPARQL XML response and emit the variables', async () => {
const stream = parser.parseXmlResultsStream(streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">

0 comments on commit 031c30c

Please sign in to comment.
You can’t perform that action at this time.