inquery
Version:
A portable indexing and searching library.
178 lines (102 loc) • 5.26 kB
JavaScript
const Inquery = require( '../index' );
const Globals = require( '../globals' );
const inquery = new Inquery();
const testableDocuments = [{"contributors":[{"name":"Eric Tilton","uid":"bVL0Mi1uFRaR63fcxytnkthl2VN2"},{"name":"Eric Tilton","uid":"uKlaEzaBhDY1iw2wvxst6RDzqxe2"}],"date":"10.1.2017","department":"-Kp1wp4k2xF-ZqM_7FXu","desc":"Updates from Marty Bitter, Director","forOffice":"","published":true,"subDepartment":"-Kp2A_1jdLiCW-gW-S99","title":"Athletics Updates","unpublishedChanges":false,"writtenOnBehalfOfOffice":false,"key":"-KvPqmfHa2AxJ4BldS2l"},{"contributors":[{"name":"Eric Tilton","uid":"bVL0Mi1uFRaR63fcxytnkthl2VN2"},{"name":"Eric Tilton","uid":"uKlaEzaBhDY1iw2wvxst6RDzqxe2"}],"date":"10.1.2017","department":"-Kp1wp4k2xF-ZqM_7FXu","desc":"Additional Athletics updates from Marty Bitter","forOffice":"","published":true,"subDepartment":"-Kp2A_1jdLiCW-gW-S99","title":"More Athletics Updates","unpublishedChanges":false,"writtenOnBehalfOfOffice":false,"key":"-KvPtvnWF74Zpt6d2mNB"},{"contributors":[{"name":"Marty Bitter","uid":"ZRhiPjxGnwMfS4NtvGOcvuLeR7i1"}],"date":"9.11.2017","department":"-Kp1wp4k2xF-ZqM_7FXu","desc":"ATHLETICS EVENTS CALENDAR ","forOffice":"","published":true,"subDepartment":"-Kp2A_1jdLiCW-gW-S99","title":"EVENTS CALENDAR","unpublishedChanges":false,"writtenOnBehalfOfOffice":false,"key":"-KtmqaxbWeMDKGEHGCvA"},{"contributors":[{"name":"Marty Bitter","uid":"ZRhiPjxGnwMfS4NtvGOcvuLeR7i1"},{"name":"Tim Farrow","uid":"7KifA9mYmihhYhzpR1fLuIJmt7D3"}],"date":"9.11.2017","department":"-Kp1wp4k2xF-ZqM_7FXu","desc":"ATHLETICS EVENTS CALENDAR ","forOffice":"","published":true,"subDepartment":"-Kp2A_1jdLiCW-gW-S99","title":"EVENTS CALENDAR","unpublishedChanges":false,"writtenOnBehalfOfOffice":false,"key":"-KtmtCLs1-XOs7CwkQsh"},{"contributors":[{"name":"Eric Tilton","uid":"bVL0Mi1uFRaR63fcxytnkthl2VN2"}],"date":"7.19.2017","department":"-Kp1wp4k2xF-ZqM_7FXu","desc":"News and information about Athletics at Madera Unified.","forOffice":"","published":true,"subDepartment":"-Kp2A_1jdLiCW-gW-S99","title":"Athletics","unpublishedChanges":false,"writtenOnBehalfOfOffice":false,"key":"-Kj5GV9oD_W1bsIisR1I"}]; // eslint-disable-line
testableDocuments.forEach( ( document ) => {
inquery.addDocument( document, document.key );
} );
/* * * * * *
* Helpers *
* * * * * */
const HasProperty = function ( a, b ) {
return Object.prototype.hasOwnProperty.call( a, b );
};
/* * * * *
* Tests *
* * * * */
describe( 'Inquery', () => {
const index = inquery.index;
it( 'should index terms', () => {
const terms = Object.keys( index.terms );
expect( terms.length ).to.be.greaterThan( 0 );
} );
it( 'should cache all documents', () => {
let allDocumentsCached = true;
const documents = index.documents;
testableDocuments.forEach( ( document ) => {
if ( !HasProperty( documents, document.key ) ) {
allDocumentsCached = false;
}
} );
expect( allDocumentsCached ).to.be.true; // eslint-disable-line
} );
} );
describe( 'findMatch', () => {
it( 'matches two strings with one set of flipped characters', () => {
const match = Globals.findMatch( 'word', 'wrod' );
expect( match ).to.be.greaterThan( -1 );
} );
it( 'matches two strings with a spelling error', () => {
const match = Globals.findMatch( 'Eric', 'Erik' );
expect( match ).to.be.greaterThan( -1 );
} );
it( 'matches a substring of another string', () => {
const match = Globals.findMatch( 'nation', 'nationally' );
expect( match ).to.be.greaterThan( -1 );
} );
it( 'doesn\'t match strings with more than two spelling errors', () => {
const match = Globals.findMatch( 'timothy', 'tomithe' );
expect( match ).to.equal( -1 );
} );
} );
describe( 'search', () => {
const results = inquery.search( 'Updates' );
it( 'should return good results', () => {
expect( results.length ).to.be.greaterThan( 0 );
} );
it( 'results are ordered by relevance', () => {
let orderedRelevantly = true;
let lastResult = null;
results.forEach( ( result ) => {
if ( lastResult === null ) {
lastResult = result;
return;
}
if ( lastResult.relevance < result.relevance ) {
orderedRelevantly = false;
}
} );
expect( orderedRelevantly ).to.be.true; // eslint-disable-line
} );
it( 'returns the cached version of the document', () => {
let allHaveDocuments = true;
results.forEach( ( result ) => {
if ( !HasProperty( result, 'document' ) ) {
allHaveDocuments = false;
}
else if ( typeof result.document !== 'object' ) {
allHaveDocuments = false;
}
} );
expect( allHaveDocuments ).to.be.true; // eslint-disable-line
} );
it( 'all results contain the id of the found document', () => {
let allHaveId = true;
results.forEach( ( result ) => {
if ( !HasProperty( result, 'id' ) ) {
allHaveId = false;
}
else if ( typeof result.id !== 'string' || !result.id ) {
allHaveId = false;
}
} );
expect( allHaveId ).to.be.true; // eslint-disable-line
} );
} );
describe( 'getIndex', () => {
const index = inquery.getIndex();
const terms = Object.keys( index );
it( 'should return the term list', () => {
expect( terms.length ).to.be.greaterThan( 0 );
} );
} );