more search tests + numeric label comparison

This commit is contained in:
zadam 2020-05-22 19:08:06 +02:00
parent ee053b9fdf
commit 714881ad99
3 changed files with 97 additions and 6 deletions

View File

@ -61,6 +61,85 @@ describe("Search", () => {
expect(searchResults.length).toEqual(1); expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy(); expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
}); });
it("numeric label comparison", async () => {
rootNote.child(
note("Europe")
.label('country', '', true)
.child(
note("Austria")
.label('population', '8859000')
)
.child(
note("Czech Republic")
.label('population', '10650000')
)
);
const parsingContext = new ParsingContext();
const searchResults = await searchService.findNotesWithQuery('#country #population >= 10000000', parsingContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("logical or", async () => {
rootNote.child(
note("Europe")
.label('country', '', true)
.child(
note("Austria")
.label('languageFamily', 'germanic')
)
.child(
note("Czech Republic")
.label('languageFamily', 'slavic')
)
.child(
note("Hungary")
.label('languageFamily', 'finnougric')
)
);
const parsingContext = new ParsingContext();
const searchResults = await searchService.findNotesWithQuery('#languageFamily = slavic OR #languageFamily = germanic', parsingContext);
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
it("fuzzy attribute search", async () => {
rootNote.child(
note("Europe")
.label('country', '', true)
.child(
note("Austria")
.label('languageFamily', 'germanic')
)
.child(
note("Czech Republic")
.label('languageFamily', 'slavic')
)
);
let parsingContext = new ParsingContext({fuzzyAttributeSearch: false});
let searchResults = await searchService.findNotesWithQuery('#language', parsingContext);
expect(searchResults.length).toEqual(0);
searchResults = await searchService.findNotesWithQuery('#languageFamily=ger', parsingContext);
expect(searchResults.length).toEqual(0);
parsingContext = new ParsingContext({fuzzyAttributeSearch: true});
searchResults = await searchService.findNotesWithQuery('#language', parsingContext);
expect(searchResults.length).toEqual(2);
searchResults = await searchService.findNotesWithQuery('#languageFamily=ger', parsingContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
}); });
/** @return {Note} */ /** @return {Note} */
@ -75,11 +154,12 @@ class NoteBuilder {
this.note = note; this.note = note;
} }
label(name, value) { label(name, value, isInheritable = false) {
new Attribute(noteCache, { new Attribute(noteCache, {
attributeId: id(), attributeId: id(),
noteId: this.note.noteId, noteId: this.note.noteId,
type: 'label', type: 'label',
isInheritable,
name, name,
value value
}); });

View File

@ -1,6 +1,6 @@
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const comparators = { const stringComparators = {
"=": comparedValue => (val => val === comparedValue), "=": comparedValue => (val => val === comparedValue),
"!=": comparedValue => (val => val !== comparedValue), "!=": comparedValue => (val => val !== comparedValue),
">": comparedValue => (val => val > comparedValue), ">": comparedValue => (val => val > comparedValue),
@ -10,7 +10,14 @@ const comparators = {
"*=": comparedValue => (val => val.endsWith(comparedValue)), "*=": comparedValue => (val => val.endsWith(comparedValue)),
"=*": comparedValue => (val => val.startsWith(comparedValue)), "=*": comparedValue => (val => val.startsWith(comparedValue)),
"*=*": comparedValue => (val => val.includes(comparedValue)), "*=*": comparedValue => (val => val.includes(comparedValue)),
} };
const numericComparators = {
">": comparedValue => (val => parseFloat(val) > comparedValue),
">=": comparedValue => (val => parseFloat(val) >= comparedValue),
"<": comparedValue => (val => parseFloat(val) < comparedValue),
"<=": comparedValue => (val => parseFloat(val) <= comparedValue)
};
const smartValueRegex = /^(NOW|TODAY|WEEK|MONTH|YEAR) *([+\-] *\d+)?$/i; const smartValueRegex = /^(NOW|TODAY|WEEK|MONTH|YEAR) *([+\-] *\d+)?$/i;
@ -58,8 +65,12 @@ function buildComparator(operator, comparedValue) {
comparedValue = calculateSmartValue(comparedValue); comparedValue = calculateSmartValue(comparedValue);
if (operator in comparators) { if (operator in numericComparators && !isNaN(comparedValue)) {
return comparators[operator](comparedValue); return numericComparators[operator](parseFloat(comparedValue));
}
if (operator in stringComparators) {
return stringComparators[operator](comparedValue);
} }
} }

View File

@ -19,7 +19,7 @@ class NoteSet {
} }
mergeIn(anotherNoteSet) { mergeIn(anotherNoteSet) {
this.notes = this.notes.concat(anotherNoteSet.arr); this.notes = this.notes.concat(anotherNoteSet.notes);
} }
minus(anotherNoteSet) { minus(anotherNoteSet) {