|
| 1 | +var Datasource = require('../../lib/datasources/Datasource'), |
| 2 | + HdtDatasource = require('../../lib/datasources/HdtDatasource'), |
| 3 | + TurtleDatasource = require('../../lib/datasources/TurtleDatasource'), |
| 4 | + CompositeDatasource = require('../../lib/datasources/CompositeDatasource'), |
| 5 | + path = require('path'); |
| 6 | + |
| 7 | +var exampleHdtFile = path.join(__dirname, '../assets/test.hdt'); |
| 8 | +var exampleHdtFileWithBlanks = path.join(__dirname, '../assets/test-blank.hdt'); |
| 9 | +var exampleTurtleUrl = 'file://' + path.join(__dirname, '../assets/test.ttl'); |
| 10 | + |
| 11 | +describe('CompositeDatasource', function () { |
| 12 | + var datasources = { |
| 13 | + "data_0": { datasource: new HdtDatasource({ file: exampleHdtFile }), size: 132 }, |
| 14 | + "data_1": { datasource: new HdtDatasource({ file: exampleHdtFileWithBlanks }), size: 6 }, |
| 15 | + "data_2": { datasource: new TurtleDatasource({ url: exampleTurtleUrl }), size: 132 } |
| 16 | + }; |
| 17 | + var references = Object.keys(datasources); |
| 18 | + var totalSize = Object.keys(datasources).reduce(function(acc, key) { |
| 19 | + return acc + datasources[key].size; |
| 20 | + }, 0); |
| 21 | + |
| 22 | + describe('The CompositeDatasource module', function () { |
| 23 | + it('should be a function', function () { |
| 24 | + CompositeDatasource.should.be.a('function'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should be an CompositeDatasource constructor', function (done) { |
| 28 | + var instance = new CompositeDatasource({ datasources: datasources, references: references }); |
| 29 | + instance.should.be.an.instanceof(CompositeDatasource); |
| 30 | + instance.close(done); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should create CompositeDatasource objects', function (done) { |
| 34 | + var instance = new CompositeDatasource({ datasources: datasources, references: references }); |
| 35 | + instance.should.be.an.instanceof(CompositeDatasource); |
| 36 | + instance.close(done); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should create Datasource objects', function (done) { |
| 40 | + var instance = new CompositeDatasource({ datasources: datasources, references: references }); |
| 41 | + instance.should.be.an.instanceof(Datasource); |
| 42 | + instance.close(done); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('A CompositeDatasource instance for two HdtDatasources', function () { |
| 47 | + var datasource, getDatasource = function () { return datasource; }; |
| 48 | + before(function (done) { |
| 49 | + datasource = new CompositeDatasource({ datasources: datasources, references: references }); |
| 50 | + datasource.on('initialized', done); |
| 51 | + }); |
| 52 | + after(function (done) { |
| 53 | + datasource.close(done); |
| 54 | + }); |
| 55 | + |
| 56 | + itShouldExecute(getDatasource, |
| 57 | + 'the empty query', |
| 58 | + { features: { triplePattern: true } }, |
| 59 | + totalSize, totalSize); |
| 60 | + |
| 61 | + itShouldExecute(getDatasource, |
| 62 | + 'the empty query with a limit', |
| 63 | + { limit: 10, features: { triplePattern: true, limit: true } }, |
| 64 | + 10, totalSize); |
| 65 | + |
| 66 | + itShouldExecute(getDatasource, |
| 67 | + 'the empty query with an offset of 10', |
| 68 | + { offset: 10, features: { triplePattern: true, offset: true } }, |
| 69 | + totalSize - 10, totalSize); |
| 70 | + |
| 71 | + itShouldExecute(getDatasource, |
| 72 | + 'the empty query with an offset of 100', |
| 73 | + { offset: 100, features: { triplePattern: true, offset: true } }, |
| 74 | + totalSize - 100, totalSize); |
| 75 | + |
| 76 | + itShouldExecute(getDatasource, |
| 77 | + 'the empty query with an offset of 200', |
| 78 | + { offset: 200, features: { triplePattern: true, offset: true } }, |
| 79 | + totalSize - 200, totalSize); |
| 80 | + |
| 81 | + itShouldExecute(getDatasource, |
| 82 | + 'a query for an existing subject', |
| 83 | + { subject: 'http://example.org/s1', limit: 10, features: { triplePattern: true, limit: true } }, |
| 84 | + 10, 200); |
| 85 | + |
| 86 | + itShouldExecute(getDatasource, |
| 87 | + 'a query for a non-existing subject', |
| 88 | + { subject: 'http://example.org/p1', limit: 10, features: { triplePattern: true, limit: true } }, |
| 89 | + 0, 0); |
| 90 | + |
| 91 | + itShouldExecute(getDatasource, |
| 92 | + 'a query for an existing predicate', |
| 93 | + { predicate: 'http://example.org/p1', limit: 10, features: { triplePattern: true, limit: true } }, |
| 94 | + 10, 220 + 110); // 220 is an inexact count from the HDT files, the 110 from the ttl file is exact. |
| 95 | + |
| 96 | + itShouldExecute(getDatasource, |
| 97 | + 'a query for a non-existing predicate', |
| 98 | + { predicate: 'http://example.org/s1', limit: 10, features: { triplePattern: true, limit: true } }, |
| 99 | + 0, 0); |
| 100 | + |
| 101 | + itShouldExecute(getDatasource, |
| 102 | + 'a query for an existing object', |
| 103 | + { object: 'http://example.org/o001', limit: 10, features: { triplePattern: true, limit: true } }, |
| 104 | + 6, 6); |
| 105 | + |
| 106 | + itShouldExecute(getDatasource, |
| 107 | + 'a query for a non-existing object', |
| 108 | + { object: 'http://example.org/s1', limit: 10, features: { triplePattern: true, limit: true } }, |
| 109 | + 0, 0); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +function itShouldExecute(getDatasource, name, query, |
| 114 | + expectedResultsCount, expectedTotalCount, expectedTriples) { |
| 115 | + describe('executing ' + name, function () { |
| 116 | + var resultsCount = 0, totalCount, triples = []; |
| 117 | + before(function (done) { |
| 118 | + var result = getDatasource().select(query); |
| 119 | + result.on('metadata', function (metadata) { totalCount = metadata.totalCount; }); |
| 120 | + result.on('data', function (triple) { resultsCount++; expectedTriples && triples.push(triple); }); |
| 121 | + result.on('end', done); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return the expected number of triples', function () { |
| 125 | + expect(resultsCount).to.equal(expectedResultsCount); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should emit the expected total number of triples', function () { |
| 129 | + expect(totalCount).to.equal(expectedTotalCount); |
| 130 | + }); |
| 131 | + |
| 132 | + if (expectedTriples) { |
| 133 | + it('should emit the expected triples', function () { |
| 134 | + expect(triples.length).to.equal(expectedTriples.length); |
| 135 | + for (var i = 0; i < expectedTriples.length; i++) |
| 136 | + triples[i].should.deep.equal(expectedTriples[i]); |
| 137 | + }); |
| 138 | + } |
| 139 | + }); |
| 140 | +} |
0 commit comments