UNPKG

2.88 kBJavaScriptView Raw
1// ext libs
2var should = require('should');
3var fs = require('fs');
4
5
6var SearchEngine = require('../../');
7
8var hitsAsJson = 'test/specs/hits.json';
9var testDB = 'test_DB'; // TODO: process.env.testDB
10
11describe('search', function () {
12
13 it('count hits of keyword', function (done) {
14
15 var se = new SearchEngine({'indexPath': testDB, logLevel: 'warn'});
16
17 se.search(["epub"], "Accessible EPUB 3", function (hits) {
18
19 se.close(function () {
20 (hits.length).should.be.exactly(15);
21 done();
22 })
23 });
24 });
25
26 it('should find no hits if keyword is not included', function (done) {
27
28 var se = new SearchEngine({'indexPath': testDB, logLevel: 'warn'});
29
30 se.search(["Accessi"], "Accessible EPUB 3", function (hits) {
31
32 se.close(function () {
33 (hits.length).should.be.exactly(0);
34
35 done();
36 });
37 });
38 });
39
40 it('should return the right hits', function (done) {
41
42 var se = new SearchEngine({'indexPath': testDB, logLevel: 'warn'});
43
44 se.search(["epub"], "Accessible EPUB 3", function (hits) {
45
46 se.close(function () {
47 var data = JSON.parse(fs.readFileSync(hitsAsJson));
48 (hits).should.eql(data);
49
50 done();
51 });
52 });
53 });
54
55 it('should return always the same hits', function (done) {
56
57 var se = new SearchEngine({'indexPath': testDB, logLevel: 'warn'});
58
59 se.search(["epub"], "Accessible EPUB 3", function (hits) {
60
61 var first = hits;
62
63 se.search(["epub"], "Accessible EPUB 3", function (hits) {
64
65 se.close(function () {
66
67 (first).should.eql(hits);
68 done();
69 });
70 });
71 });
72 });
73
74 it('check hit properties are set', function (done) {
75
76 var se = new SearchEngine({'indexPath': testDB, logLevel: 'warn'});
77
78 se.search(["epub"], "Accessible EPUB 3", function (hits) {
79
80 se.close(function () {
81
82 for (i in hits) {
83
84 Object.keys(hits[i]).should.have.length(5);
85 hits[i].should.have.property('baseCfi');
86 hits[i].should.have.property('cfis');
87 hits[i].should.have.property('epubTitle');
88 hits[i].should.have.property('id');
89 hits[i].should.have.property('href');
90
91 hits[i].should.not.have.enumerable('cfis', 0);
92 hits[i].href.should.not.be.empty;
93 hits[i].baseCfi.should.not.be.empty;
94 hits[i].epubTitle.should.not.be.empty;
95 hits[i].id.should.not.be.empty;
96 }
97
98 done();
99 });
100 });
101 });
102});
103