|
| 1 | +/*! @license MIT ©2014-2016 Ruben Verborgh, Ghent University - imec */ |
| 2 | +let RdfaDatasource = require('../../').datasources.RdfaDatasource; |
| 3 | + |
| 4 | +let Datasource = require('@ldf/core').datasources.Datasource, |
| 5 | + path = require('path'), |
| 6 | + dataFactory = require('n3').DataFactory; |
| 7 | + |
| 8 | +let exampleRdfaUrl = 'file://' + path.join(__dirname, '../../../../test/assets/test.html'); |
| 9 | + |
| 10 | +describe('RdfaDatasource', () => { |
| 11 | + describe('The RdfaDatasource module', () => { |
| 12 | + it('should be a function', () => { |
| 13 | + RdfaDatasource.should.be.a('function'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should be a RdfaDatasource constructor', (done) => { |
| 17 | + let instance = new RdfaDatasource({ dataFactory, url: exampleRdfaUrl }); |
| 18 | + instance.should.be.an.instanceof(RdfaDatasource); |
| 19 | + instance.close(done); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should create Datasource objects', (done) => { |
| 23 | + let instance = new RdfaDatasource({ dataFactory, url: exampleRdfaUrl }); |
| 24 | + instance.should.be.an.instanceof(Datasource); |
| 25 | + instance.close(done); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('A RdfaDatasource instance for an example RDFa HTML file', () => { |
| 30 | + let datasource = new RdfaDatasource({ dataFactory, url: exampleRdfaUrl }); |
| 31 | + datasource.initialize(); |
| 32 | + after((done) => { datasource.close(done); }); |
| 33 | + |
| 34 | + itShouldExecute(datasource, |
| 35 | + 'the empty query', |
| 36 | + { features: { triplePattern: true } }, |
| 37 | + 129, 129); |
| 38 | + |
| 39 | + itShouldExecute(datasource, |
| 40 | + 'the empty query with a limit', |
| 41 | + { limit: 10, features: { triplePattern: true, limit: true } }, |
| 42 | + 10, 129); |
| 43 | + |
| 44 | + itShouldExecute(datasource, |
| 45 | + 'the empty query with an offset', |
| 46 | + { offset: 10, features: { triplePattern: true, offset: true } }, |
| 47 | + 119, 129); |
| 48 | + |
| 49 | + itShouldExecute(datasource, |
| 50 | + 'a query for an existing subject', |
| 51 | + { subject: dataFactory.namedNode('http://example.org/s1'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 52 | + 10, 100); |
| 53 | + |
| 54 | + itShouldExecute(datasource, |
| 55 | + 'a query for a non-existing subject', |
| 56 | + { subject: dataFactory.namedNode('http://example.org/p1'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 57 | + 0, 0); |
| 58 | + |
| 59 | + itShouldExecute(datasource, |
| 60 | + 'a query for an existing predicate', |
| 61 | + { predicate: dataFactory.namedNode('http://example.org/p1'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 62 | + 10, 110); |
| 63 | + |
| 64 | + itShouldExecute(datasource, |
| 65 | + 'a query for a non-existing predicate', |
| 66 | + { predicate: dataFactory.namedNode('http://example.org/s1'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 67 | + 0, 0); |
| 68 | + |
| 69 | + itShouldExecute(datasource, |
| 70 | + 'a query for an existing object', |
| 71 | + { object: dataFactory.namedNode('http://example.org/o001'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 72 | + 3, 3); |
| 73 | + |
| 74 | + itShouldExecute(datasource, |
| 75 | + 'a query for a non-existing object', |
| 76 | + { object: dataFactory.namedNode('http://example.org/s1'), limit: 10, features: { triplePattern: true, limit: true } }, |
| 77 | + 0, 0); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +function itShouldExecute(datasource, name, query, expectedResultsCount, expectedTotalCount) { |
| 82 | + describe('executing ' + name, () => { |
| 83 | + let resultsCount = 0, totalCount; |
| 84 | + before((done) => { |
| 85 | + let result = datasource.select(query); |
| 86 | + result.getProperty('metadata', (metadata) => { totalCount = metadata.totalCount; }); |
| 87 | + result.on('data', (triple) => { resultsCount++; }); |
| 88 | + result.on('end', done); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return the expected number of triples', () => { |
| 92 | + expect(resultsCount).to.equal(expectedResultsCount); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should emit the expected total number of triples', () => { |
| 96 | + expect(totalCount).to.equal(expectedTotalCount); |
| 97 | + }); |
| 98 | + }); |
| 99 | +} |
0 commit comments