comparison.test.js (2854B)
1 const tape = require('tape'); 2 const comparison = require('./comparison'); 3 4 tape('filter.number.comparison -- structure', async t => { 5 t.plan(2); 6 t.ok('detect' in comparison, 'comparison filter has detect method'); 7 t.ok('compile' in comparison, 'comparison filter has compile method'); 8 }); 9 10 tape('filter.number.comparison -- detect', async t => { 11 t.plan(9); 12 t.notOk(comparison.detect(false), 'contains.detect doesn\'t trigger on false query'); 13 t.notOk(comparison.detect(null), 'contains.detect doesn\'t trigger on null query'); 14 t.notOk(comparison.detect(true), 'contains.detect doesn\'t trigger on true query'); 15 t.notOk(comparison.detect([]), 'contains.detect doesn\'t trigger on empty array query'); 16 t.notOk(comparison.detect({}), 'contains.detect doesn\'t trigger on empty object query'); 17 t.notOk(comparison.detect({ field: 'something' }), 'contains.detect doesn\'t trigger on termless query'); 18 t.notOk(comparison.detect({ term: 'something' }), 'contains.detect doesn\'t trigger on fieldless query'); 19 t.ok(comparison.detect({ field: 'hello', term: '>1' }), 'contains.detect triggers on properly structured query'); 20 t.notOk(comparison.detect({ field: 'hello', term: '1' }), 'contains.detect doesn\'t trigger on query with no comparator'); 21 }); 22 23 tape('filter.number.comparison, gt -- compile', async t => { 24 t.plan(3); 25 26 const extractor = comparison.compile({ field: 'age', term: '>21' }); 27 t.equal(typeof extractor, 'function', 'comparison.compile returns a function'); 28 29 t.ok(extractor({ age: 22 }), 'finds term in comparison'); 30 t.notOk(extractor({ age: 21 }), 'doesn\'t find term in comparison'); 31 }); 32 tape('filter.number.comparison, gte -- compile', async t => { 33 t.plan(4); 34 35 const extractor = comparison.compile({ field: 'age', term: '>=21' }); 36 t.equal(typeof extractor, 'function', 'comparison.compile returns a function'); 37 38 t.ok(extractor({ age: 22 }), 'finds term in comparison'); 39 t.ok(extractor({ age: 21 }), 'finds term in comparison'); 40 t.notOk(extractor({ age: 20 }), 'doesn\'t find term in comparison'); 41 }); 42 tape('filter.number.comparison, lt -- compile', async t => { 43 t.plan(3); 44 45 const extractor = comparison.compile({ field: 'age', term: '<21' }); 46 t.equal(typeof extractor, 'function', 'comparison.compile returns a function'); 47 48 t.ok(extractor({ age: 20 }), 'finds term in comparison'); 49 t.notOk(extractor({ age: 21 }), 'doesn\'t find term in comparison'); 50 }); 51 tape('filter.number.comparison, lte -- compile', async t => { 52 t.plan(4); 53 54 const extractor = comparison.compile({ field: 'age', term: '<=21' }); 55 t.equal(typeof extractor, 'function', 'comparison.compile returns a function'); 56 57 t.ok(extractor({ age: 20 }), 'finds term in comparison'); 58 t.ok(extractor({ age: 21 }), 'finds term in comparison'); 59 t.notOk(extractor({ age: 22 }), 'doesn\'t find term in comparison'); 60 });