refactoring to allow unit tests of the whole search subsystem

This commit is contained in:
zadam 2020-05-21 14:05:56 +02:00
parent a06662f4ce
commit cd48135394
9 changed files with 42 additions and 32 deletions

View File

@ -6,7 +6,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: ["hello", "hi"],
expressionTokens: [],
parsingContext: new ParsingContext(false)
parsingContext: new ParsingContext({includeNoteContent: false})
});
expect(rootExp.constructor.name).toEqual("NoteCacheFulltextExp");
@ -17,7 +17,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: ["hello", "hi"],
expressionTokens: [],
parsingContext: new ParsingContext(true)
parsingContext: new ParsingContext({includeNoteContent: true})
});
expect(rootExp.constructor.name).toEqual("OrExp");
@ -34,7 +34,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#mylabel", "=", "text"],
parsingContext: new ParsingContext(true)
parsingContext: new ParsingContext()
});
expect(rootExp.constructor.name).toEqual("FieldComparisonExp");
@ -64,7 +64,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "#second", "=", "text"],
parsingContext: new ParsingContext(true)
parsingContext: new ParsingContext()
});
expect(rootExp.constructor.name).toEqual("AndExp");
@ -81,7 +81,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "OR", "#second", "=", "text"],
parsingContext: new ParsingContext(true)
parsingContext: new ParsingContext()
});
expect(rootExp.constructor.name).toEqual("OrExp");
@ -98,7 +98,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: ["hello"],
expressionTokens: ["#mylabel", "=", "text"],
parsingContext: new ParsingContext(false)
parsingContext: new ParsingContext()
});
expect(rootExp.constructor.name).toEqual("AndExp");
@ -115,7 +115,7 @@ describe("Parser", () => {
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "OR", ["#second", "=", "text", "AND", "#third", "=", "text"]],
parsingContext: new ParsingContext(false)
parsingContext: new ParsingContext()
});
expect(rootExp.constructor.name).toEqual("OrExp");

7
spec/search.spec.js Normal file
View File

@ -0,0 +1,7 @@
const searchService = require('../src/services/search/search');
describe("Search", () => {
it("fulltext parser without content", () => {
// searchService.
});
});

View File

@ -12,6 +12,8 @@ const sessionSecret = require('./services/session_secret');
const cls = require('./services/cls');
require('./entities/entity_constructor');
require('./services/handlers');
require('./services/hoisted_note_loader');
require('./services/note_cache/note_cache_loader');
const app = express();
@ -120,4 +122,4 @@ require('./services/scheduler');
module.exports = {
app,
sessionParser
};
};

View File

@ -1,19 +1,6 @@
const optionService = require('./options');
const sqlInit = require('./sql_init');
const eventService = require('./events');
let hoistedNoteId = 'root';
eventService.subscribe(eventService.ENTITY_CHANGED, async ({entityName, entity}) => {
if (entityName === 'options' && entity.name === 'hoistedNoteId') {
hoistedNoteId = entity.value;
}
});
sqlInit.dbReady.then(async () => {
hoistedNoteId = await optionService.getOption('hoistedNoteId');
});
module.exports = {
getHoistedNoteId: () => hoistedNoteId
};
getHoistedNoteId: () => hoistedNoteId,
setHoistedNoteId(noteId) { hoistedNoteId = noteId; }
};

View File

@ -0,0 +1,14 @@
const optionService = require('./options');
const sqlInit = require('./sql_init');
const eventService = require('./events');
const hoistedNote = require('./hoisted_note');
eventService.subscribe(eventService.ENTITY_CHANGED, async ({entityName, entity}) => {
if (entityName === 'options' && entity.name === 'hoistedNoteId') {
hoistedNote.setHoistedNoteId(entity.value);
}
});
sqlInit.dbReady.then(async () => {
hoistedNote.setHoistedNoteId(await optionService.getOption('hoistedNoteId'));
});

View File

@ -166,4 +166,4 @@ eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes());
});
module.exports = load;
load();

View File

@ -4,8 +4,6 @@ const noteCache = require('./note_cache');
const hoistedNoteService = require('../hoisted_note');
const stringSimilarity = require('string-similarity');
require('./note_cache_loader')();
function isNotePathArchived(notePath) {
const noteId = notePath[notePath.length - 1];
const note = noteCache.notes[noteId];

View File

@ -1,10 +1,10 @@
"use strict";
class ParsingContext {
constructor(includeNoteContent) {
this.includeNoteContent = includeNoteContent;
constructor(params = {}) {
this.includeNoteContent = !!params.includeNoteContent;
this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch;
this.highlightedTokens = [];
this.fuzzyAttributeSearch = false;
this.error = null;
}

View File

@ -61,8 +61,10 @@ async function searchNotesForAutocomplete(query) {
return [];
}
const parsingContext = new ParsingContext(false);
parsingContext.fuzzyAttributeSearch = true;
const parsingContext = new ParsingContext({
includeNoteContent: false,
fuzzyAttributeSearch: true
});
const expression = parseQueryToExpression(query, parsingContext);