From af2ed681f78cc0a43db865e2e25a8752b4198736 Mon Sep 17 00:00:00 2001 From: Thomas Minier Date: Sat, 1 Sep 2018 10:06:52 +0200 Subject: [PATCH] add tests for LIMIT, OFFSET & DISTINCT modifiers --- tests/modifiers/limit-offset-test.js | 110 +++++++++++++++++++++++++++ tests/modifiers/select-test.js | 29 ++++++- 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/modifiers/limit-offset-test.js diff --git a/tests/modifiers/limit-offset-test.js b/tests/modifiers/limit-offset-test.js new file mode 100644 index 00000000..b3256114 --- /dev/null +++ b/tests/modifiers/limit-offset-test.js @@ -0,0 +1,110 @@ +/* file : limit-offset-test.js +MIT License + +Copyright (c) 2018 Thomas Minier + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +'use strict' + +const expect = require('chai').expect +const { getGraph, TestEngine } = require('../utils.js') + +describe('SPARQL queries with LIMIT/OFFSET', () => { + let engine = null + before(() => { + const g = getGraph('./tests/data/dblp.nt') + engine = new TestEngine(g) + }) + + const data = [ + { + text: 'should evaluate SPARQL queries with OFFSET', + query: ` + PREFIX dblp-pers: + PREFIX dblp-rdf: + PREFIX rdf: + SELECT ?name ?article WHERE { + ?s rdf:type dblp-rdf:Person . + ?s dblp-rdf:primaryFullPersonName ?name . + ?s dblp-rdf:authorOf ?article . + } + OFFSET 2`, + results: [ + 'https://dblp.org/rec/conf/esws/MinierSMV18', + 'https://dblp.org/rec/conf/esws/MinierSMV18a', + 'https://dblp.org/rec/journals/corr/abs-1806-00227' + ] + }, + { + text: 'should evaluate SPARQL queries with LIMIT', + query: ` + PREFIX dblp-pers: + PREFIX dblp-rdf: + PREFIX rdf: + SELECT ?name ?article WHERE { + ?s rdf:type dblp-rdf:Person . + ?s dblp-rdf:primaryFullPersonName ?name . + ?s dblp-rdf:authorOf ?article . + } + LIMIT 2`, + results: [ + 'https://dblp.org/rec/conf/esws/MinierMSM17', + 'https://dblp.org/rec/conf/esws/MinierMSM17a' + ] + }, + { + text: 'should evaluate SPARQL queries with LIMIT & OFFSET', + query: ` + PREFIX dblp-pers: + PREFIX dblp-rdf: + PREFIX rdf: + SELECT ?name ?article WHERE { + ?s rdf:type dblp-rdf:Person . + ?s dblp-rdf:primaryFullPersonName ?name . + ?s dblp-rdf:authorOf ?article . + } + OFFSET 3 + LIMIT 2`, + results: [ + 'https://dblp.org/rec/conf/esws/MinierSMV18', + 'https://dblp.org/rec/conf/esws/MinierSMV18a' + ] + } + ] + + data.forEach(d => { + it(d.text, done => { + const expectedCardinality = d.results.length + let nbResults = 0 + const iterator = engine.execute(d.query) + iterator.on('error', done) + iterator.on('data', b => { + expect(b['?article']).to.be.oneOf(d.results) + d.results.splice(d.results.indexOf(b['?article']), 1) + nbResults++ + }) + iterator.on('end', () => { + expect(nbResults).to.equal(expectedCardinality) + done() + }) + }) + }) +}) diff --git a/tests/modifiers/select-test.js b/tests/modifiers/select-test.js index 498b63d0..5721f0b4 100644 --- a/tests/modifiers/select-test.js +++ b/tests/modifiers/select-test.js @@ -57,7 +57,7 @@ describe('SELECT SPARQL queries', () => { }) }) - it('should evaluate SELECT * SPARQL queries', done => { + it('should evaluate SELECT * queries', done => { const query = ` PREFIX dblp-pers: PREFIX dblp-rdf: @@ -79,4 +79,31 @@ describe('SELECT SPARQL queries', () => { done() }) }) + + it('should evaluate SELECT DISTINCT queries', done => { + const query = ` + PREFIX dblp-pers: + PREFIX dblp-rdf: + PREFIX rdf: + SELECT DISTINCT ?name WHERE { + { + ?s rdf:type dblp-rdf:Person . + ?s dblp-rdf:primaryFullPersonName ?name . + } UNION { + ?s rdf:type dblp-rdf:Person . + ?s dblp-rdf:primaryFullPersonName ?name . + } + }` + const results = [] + const iterator = engine.execute(query) + iterator.on('error', done) + iterator.on('data', b => { + expect(b).to.have.keys('?name') + results.push(b) + }) + iterator.on('end', () => { + expect(results.length).to.equal(1) + done() + }) + }) })