lucene-filter.js

Data filter for lucene queries
git clone git://git.finwo.net/lib/lucene-filter.js
Log | Files | Refs | README | LICENSE

contains.test.js (2852B)


      1 const tape     = require('tape');
      2 const contains = require('./contains');
      3 
      4 tape('filter.string.contains -- structure', async t => {
      5   t.plan(2);
      6   t.ok('detect' in contains, 'contains filter has detect method');
      7   t.ok('compile' in contains, 'contains filter has compile method');
      8 });
      9 
     10 tape('filter.string.contains -- detect', async t => {
     11   t.plan(8);
     12   t.notOk(contains.detect(false), 'contains.detect doesn\'t trigger on false query');
     13   t.notOk(contains.detect(null), 'contains.detect doesn\'t trigger on null query');
     14   t.notOk(contains.detect(true), 'contains.detect doesn\'t trigger on true query');
     15   t.notOk(contains.detect([]), 'contains.detect doesn\'t trigger on empty array query');
     16   t.notOk(contains.detect({}), 'contains.detect doesn\'t trigger on empty object query');
     17   t.notOk(contains.detect({ field: 'something' }), 'contains.detect doesn\'t trigger on termless query');
     18   t.notOk(contains.detect({ term: 'something' }), 'contains.detect doesn\'t trigger on fieldless query');
     19   t.ok(contains.detect({ field: 'hello', term: 'world' }), 'contains.detect triggers on properly structured query');
     20 });
     21 
     22 tape('filter.string.contains -- compile', async t => {
     23   t.plan(14);
     24 
     25   // Look for a nice greeting message
     26   const extractor = contains.compile({ field: 'greeting', term: 'hello', boost: 1 });
     27   t.equal(typeof extractor, 'function', 'contains.compile returns a function');
     28 
     29   t.ok(extractor({ greeting: 'hello', subject: 'world' }), 'finds term in exact string field');
     30   t.notOk(extractor({ greeting: 'helo', subject: 'world' }), 'doesn\'t find term in malformed string field');
     31   t.notOk(extractor({ greeting: 'hel', subject: 'world' }), 'doesn\'t find term in partial string field');
     32   t.ok(extractor({ greeting: '!hello!', subject: 'world' }), 'finds term within enclosed string field');
     33   t.notOk(extractor({ greeting: [], subject: 'world' }), 'doesn\'t find term within empty array field');
     34   t.ok(extractor({ greeting: ['hello'], subject: 'world' }), 'finds term within [exact] array field');
     35   t.ok(extractor({ greeting: ['!hello!'], subject: 'world' }), 'finds term within [enclosed] array field');
     36   t.notOk(extractor({ greeting: ['helo'], subject: 'world' }), 'doesn\'t find term within [malformed] array field');
     37   t.notOk(extractor({ greeting: ['hel'], subject: 'world' }), 'doesn\'t find term within [partial] array field');
     38   t.ok(extractor({ greeting: ['hel', 'hello'], subject: 'world' }), 'finds term within [partial,exact] array field');
     39   t.ok(extractor({ greeting: ['helo', 'hello'], subject: 'world' }), 'finds term within [malformed,exact] array field');
     40   t.ok(extractor({ greeting: ['hel', '!hello!'], subject: 'world' }), 'finds term within [partial,enclosed] array field');
     41   t.ok(extractor({ greeting: ['helo', '!hello!'], subject: 'world' }), 'finds term within [malformed,enclosed] array field');
     42 });