trilium/spec/search/search.spec.js

664 lines
26 KiB
JavaScript
Raw Normal View History

2022-01-11 00:09:20 +08:00
const searchService = require('../../src/services/search/services/search');
const Note = require('../../src/becca/entities/note');
const Branch = require('../../src/becca/entities/branch');
const SearchContext = require('../../src/services/search/search_context');
const dateUtils = require('../../src/services/date_utils');
const becca = require('../../src/becca/becca');
const {NoteBuilder, findNoteByTitle, note} = require('./becca_mocking.js');
describe("Search", () => {
2020-05-22 15:38:30 +08:00
let rootNote;
beforeEach(() => {
2021-04-17 05:00:08 +08:00
becca.reset();
2020-05-22 15:38:30 +08:00
2021-05-03 03:34:57 +08:00
rootNote = new NoteBuilder(new Note({noteId: 'root', title: 'root', type: 'text'}));
2021-12-21 04:31:44 +08:00
new Branch({branchId: 'root', noteId: 'root', parentNoteId: 'none', notePosition: 10});
2020-05-22 15:38:30 +08:00
});
2020-08-20 05:00:51 +08:00
it("simple path match", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.child(note("Austria"))
2020-05-22 15:38:30 +08:00
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('europe austria', searchContext);
2020-05-22 15:38:30 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-31 05:42:24 +08:00
it("normal search looks also at attributes", () => {
const austria = note("Austria");
const vienna = note("Vienna");
rootNote
.child(austria
.relation('capital', vienna))
.child(vienna
.label('inhabitants', '1888776'));
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('capital', searchContext);
2020-08-31 05:42:24 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('inhabitants', searchContext);
2020-08-31 05:42:24 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Vienna")).toBeTruthy();
});
it("normal search looks also at type and mime", () => {
rootNote
.child(note("Effective Java", {type: 'book', mime:''}))
.child(note("Hello World.java", {type: 'code', mime: 'text/x-java'}));
2020-08-31 05:42:24 +08:00
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('book', searchContext);
2020-08-31 05:42:24 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Effective Java")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('text', searchContext); // should match mime
2020-08-31 05:42:24 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Hello World.java")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('java', searchContext);
2020-08-31 05:42:24 +08:00
expect(searchResults.length).toEqual(2);
});
2020-08-20 05:00:51 +08:00
it("only end leafs are results", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.child(note("Austria"))
2020-05-22 15:38:30 +08:00
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('europe', searchContext);
2020-05-22 15:38:30 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Europe")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("only end leafs are results", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.child(note("Austria")
.label('capital', 'Vienna'))
2020-05-22 15:38:30 +08:00
);
const searchContext = new SearchContext();
2020-05-22 15:38:30 +08:00
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('Vienna', searchContext);
2020-05-22 15:38:30 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("label comparison with short syntax", () => {
rootNote
.child(note("Europe")
.child(note("Austria")
.label('capital', 'Vienna'))
.child(note("Czech Republic")
.label('capital', 'Prague'))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('#capital=Vienna', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
2020-12-15 22:15:41 +08:00
// case sensitivity:
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#CAPITAL=VIENNA', searchContext);
2020-12-15 22:15:41 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#caPItal=vienNa', searchContext);
2020-12-15 22:15:41 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("label comparison with full syntax", () => {
rootNote
.child(note("Europe")
.child(note("Austria")
.label('capital', 'Vienna'))
.child(note("Czech Republic")
.label('capital', 'Prague'))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.labels.capital=Prague', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("numeric label comparison", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.label('country', '', true)
2020-05-23 16:36:49 +08:00
.child(note("Austria")
.label('population', '8859000'))
.child(note("Czech Republic")
.label('population', '10650000'))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('#country #population >= 10000000', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
it("inherited label comparison", () => {
rootNote
.child(note("Europe")
.label('country', '', true)
.child(note("Austria"))
.child(note("Czech Republic"))
);
const searchContext = new SearchContext();
const searchResults = searchService.findResultsWithQuery('austria #country', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("numeric label comparison fallback to string comparison", () => {
2020-05-24 04:18:06 +08:00
// dates should not be coerced into numbers which would then give wrong numbers
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.label('country', '', true)
2020-05-23 16:36:49 +08:00
.child(note("Austria")
.label('established', '1955-07-27'))
.child(note("Czech Republic")
.label('established', '1993-01-01'))
.child(note("Hungary")
2020-05-24 04:18:06 +08:00
.label('established', '1920-06-04'))
2021-12-21 04:31:44 +08:00
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('#established <= "1955-01-01"', searchContext);
expect(searchResults.length).toEqual(1);
2020-05-24 04:18:06 +08:00
expect(findNoteByTitle(searchResults, "Hungary")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#established > "1955-01-01"', searchContext);
2020-05-24 04:18:06 +08:00
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
2020-05-24 04:18:06 +08:00
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("smart date comparisons", () => {
2020-05-24 05:44:55 +08:00
// dates should not be coerced into numbers which would then give wrong numbers
rootNote
.child(note("My note", {dateCreated: dateUtils.localNowDateTime()})
2020-05-24 05:44:55 +08:00
.label('year', new Date().getFullYear().toString())
.label('month', dateUtils.localNowDate().substr(0, 7))
.label('date', dateUtils.localNowDate())
.label('dateTime', dateUtils.localNowDateTime())
);
const searchContext = new SearchContext();
2020-05-24 05:44:55 +08:00
2020-08-20 05:00:51 +08:00
function test(query, expectedResultCount) {
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery(query, searchContext);
2020-05-24 05:44:55 +08:00
expect(searchResults.length).toEqual(expectedResultCount);
if (expectedResultCount === 1) {
expect(findNoteByTitle(searchResults, "My note")).toBeTruthy();
}
}
2020-08-20 05:00:51 +08:00
test("#year = YEAR", 1);
test("#year = 'YEAR'", 0);
2020-08-20 05:00:51 +08:00
test("#year >= YEAR", 1);
test("#year <= YEAR", 1);
test("#year < YEAR+1", 1);
test("#year < YEAR + 1", 1);
test("#year < year + 1", 1);
2020-08-20 05:00:51 +08:00
test("#year > YEAR+1", 0);
2020-05-24 05:44:55 +08:00
2020-08-20 05:00:51 +08:00
test("#month = MONTH", 1);
test("#month = month", 1);
test("#month = 'MONTH'", 0);
2020-05-24 05:44:55 +08:00
2021-12-21 04:31:44 +08:00
test("note.dateCreated =* month", 2);
2020-08-20 05:00:51 +08:00
test("#date = TODAY", 1);
test("#date = today", 1);
test("#date = 'today'", 0);
2020-08-20 05:00:51 +08:00
test("#date > TODAY", 0);
test("#date > TODAY-1", 1);
test("#date > TODAY - 1", 1);
2020-08-20 05:00:51 +08:00
test("#date < TODAY+1", 1);
test("#date < TODAY + 1", 1);
2020-08-20 05:00:51 +08:00
test("#date < 'TODAY + 1'", 1);
2020-05-24 05:44:55 +08:00
2020-08-20 05:00:51 +08:00
test("#dateTime <= NOW+10", 1);
test("#dateTime <= NOW + 10", 1);
2020-08-20 05:00:51 +08:00
test("#dateTime < NOW-10", 0);
test("#dateTime >= NOW-10", 1);
test("#dateTime < NOW-10", 0);
2020-05-24 05:44:55 +08:00
});
2020-08-20 05:00:51 +08:00
it("logical or", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.label('country', '', true)
2020-05-23 16:36:49 +08:00
.child(note("Austria")
.label('languageFamily', 'germanic'))
.child(note("Czech Republic")
.label('languageFamily', 'slavic'))
.child(note("Hungary")
.label('languageFamily', 'finnougric'))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('#languageFamily = slavic OR #languageFamily = germanic', searchContext);
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("fuzzy attribute search", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.label('country', '', true)
2020-05-23 16:36:49 +08:00
.child(note("Austria")
.label('languageFamily', 'germanic'))
.child(note("Czech Republic")
.label('languageFamily', 'slavic')));
let searchContext = new SearchContext({fuzzyAttributeSearch: false});
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('#language', searchContext);
expect(searchResults.length).toEqual(0);
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#languageFamily=ger', searchContext);
expect(searchResults.length).toEqual(0);
searchContext = new SearchContext({fuzzyAttributeSearch: true});
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#language', searchContext);
expect(searchResults.length).toEqual(2);
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#languageFamily=ger', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("filter by note property", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.child(note("Austria"))
.child(note("Czech Republic")));
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('# note.title =* czech', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("filter by note's parent", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
.child(note("Austria"))
2020-05-24 00:42:32 +08:00
.child(note("Czech Republic")
.child(note("Prague")))
)
2020-05-23 16:36:49 +08:00
.child(note("Asia")
.child(note('Taiwan')));
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe', searchContext);
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Asia', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Taiwan")).toBeTruthy();
2020-05-24 00:42:32 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.parents.title = Europe', searchContext);
2020-05-24 00:42:32 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Prague")).toBeTruthy();
});
2020-05-23 16:36:49 +08:00
2020-08-20 05:00:51 +08:00
it("filter by note's ancestor", () => {
2020-05-24 05:44:55 +08:00
rootNote
.child(note("Europe")
.child(note("Austria"))
.child(note("Czech Republic")
.child(note("Prague").label('city')))
)
.child(note("Asia")
.child(note('Taiwan')
.child(note('Taipei').label('city')))
);
const searchContext = new SearchContext();
2020-05-24 05:44:55 +08:00
2021-12-21 04:31:44 +08:00
let searchResults = searchService.findResultsWithQuery('#city AND note.ancestors.title = Europe', searchContext);
2020-05-24 05:44:55 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Prague")).toBeTruthy();
2021-12-21 04:31:44 +08:00
searchResults = searchService.findResultsWithQuery('#city AND note.ancestors.title = Asia', searchContext);
2020-05-24 05:44:55 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Taipei")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("filter by note's child", () => {
2020-05-23 16:36:49 +08:00
rootNote
.child(note("Europe")
2020-05-24 00:42:32 +08:00
.child(note("Austria")
.child(note("Vienna")))
.child(note("Czech Republic")
.child(note("Prague"))))
2020-05-23 16:36:49 +08:00
.child(note("Oceania")
.child(note('Australia')));
const searchContext = new SearchContext();
2020-05-23 16:36:49 +08:00
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.children.title =* Aust', searchContext);
2020-05-23 16:36:49 +08:00
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Europe")).toBeTruthy();
expect(findNoteByTitle(searchResults, "Oceania")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.children.title =* Aust AND note.children.title *= republic', searchContext);
2020-05-23 16:36:49 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Europe")).toBeTruthy();
2020-05-24 00:42:32 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.children.children.title = Prague', searchContext);
2020-05-24 00:42:32 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Europe")).toBeTruthy();
2020-05-23 16:36:49 +08:00
});
2020-08-20 05:00:51 +08:00
it("filter by relation's note properties using short syntax", () => {
const austria = note("Austria");
const portugal = note("Portugal");
rootNote
.child(note("Europe")
.child(austria)
.child(note("Czech Republic")
.relation('neighbor', austria.note))
.child(portugal)
.child(note("Spain")
.relation('neighbor', portugal.note))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# ~neighbor.title = Austria', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# ~neighbor.title = Portugal', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Spain")).toBeTruthy();
});
2020-08-20 05:00:51 +08:00
it("filter by relation's note properties using long syntax", () => {
const austria = note("Austria");
const portugal = note("Portugal");
rootNote
.child(note("Europe")
.child(austria)
.child(note("Czech Republic")
.relation('neighbor', austria.note))
.child(portugal)
.child(note("Spain")
.relation('neighbor', portugal.note))
);
const searchContext = new SearchContext();
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery('# note.relations.neighbor.title = Austria', searchContext);
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
});
2020-05-24 00:42:32 +08:00
2020-08-20 05:00:51 +08:00
it("filter by multiple level relation", () => {
2020-05-24 00:42:32 +08:00
const austria = note("Austria");
const slovakia = note("Slovakia");
const italy = note("Italy");
const ukraine = note("Ukraine");
rootNote
.child(note("Europe")
.child(austria
.relation('neighbor', italy.note)
.relation('neighbor', slovakia.note))
.child(note("Czech Republic")
.relation('neighbor', austria.note)
.relation('neighbor', slovakia.note))
.child(slovakia
.relation('neighbor', ukraine.note))
.child(ukraine)
);
const searchContext = new SearchContext();
2020-05-24 00:42:32 +08:00
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.relations.neighbor.relations.neighbor.title = Italy', searchContext);
2020-05-24 00:42:32 +08:00
expect(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.relations.neighbor.relations.neighbor.title = Ukraine', searchContext);
2020-05-24 00:42:32 +08:00
expect(searchResults.length).toEqual(2);
expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
it("test note properties", () => {
2020-05-24 02:52:55 +08:00
const austria = note("Austria");
austria.relation('myself', austria.note);
austria.label('capital', 'Vienna');
austria.label('population', '8859000');
rootNote
.child(note("Asia"))
.child(note("Europe")
.child(austria
.child(note("Vienna"))
.child(note("Sebastian Kurz"))
)
)
.child(note("Mozart")
.child(austria));
austria.note.isProtected = false;
austria.note.dateCreated = '2020-05-14 12:11:42.001+0200';
austria.note.dateModified = '2020-05-14 13:11:42.001+0200';
austria.note.utcDateCreated = '2020-05-14 10:11:42.001Z';
austria.note.utcDateModified = '2020-05-14 11:11:42.001Z';
austria.note.contentLength = 1001;
const searchContext = new SearchContext();
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
function test(propertyName, value, expectedResultCount) {
2021-04-18 02:52:46 +08:00
const searchResults = searchService.findResultsWithQuery(`# note.${propertyName} = ${value}`, searchContext);
2020-05-24 02:52:55 +08:00
expect(searchResults.length).toEqual(expectedResultCount);
}
2020-12-15 22:09:00 +08:00
test("type", "text", 7);
2020-12-15 22:15:41 +08:00
test("TYPE", "TEXT", 7);
2020-08-20 05:00:51 +08:00
test("type", "code", 0);
2020-05-24 02:52:55 +08:00
2020-08-31 05:42:24 +08:00
test("mime", "text/html", 6);
2020-08-20 05:00:51 +08:00
test("mime", "application/json", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("isProtected", "false", 7);
2020-12-15 22:15:41 +08:00
test("isProtected", "FALSE", 7);
2020-08-20 05:00:51 +08:00
test("isProtected", "true", 0);
2020-12-15 22:15:41 +08:00
test("isProtected", "TRUE", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("dateCreated", "'2020-05-14 12:11:42.001+0200'", 1);
test("dateCreated", "wrong", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("dateModified", "'2020-05-14 13:11:42.001+0200'", 1);
test("dateModified", "wrong", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("utcDateCreated", "'2020-05-14 10:11:42.001Z'", 1);
test("utcDateCreated", "wrong", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("utcDateModified", "'2020-05-14 11:11:42.001Z'", 1);
test("utcDateModified", "wrong", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("parentCount", "2", 1);
test("parentCount", "3", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("childrenCount", "2", 1);
test("childrenCount", "10", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("attributeCount", "3", 1);
test("attributeCount", "4", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("labelCount", "2", 1);
test("labelCount", "3", 0);
2020-05-24 02:52:55 +08:00
2020-08-20 05:00:51 +08:00
test("relationCount", "1", 1);
test("relationCount", "2", 0);
2020-05-24 04:18:06 +08:00
});
2020-05-22 15:38:30 +08:00
2020-08-20 05:00:51 +08:00
it("test order by", () => {
2020-05-25 06:25:47 +08:00
const italy = note("Italy").label("capital", "Rome");
const slovakia = note("Slovakia").label("capital", "Bratislava");
const austria = note("Austria").label("capital", "Vienna");
const ukraine = note("Ukraine").label("capital", "Kiev");
rootNote
.child(note("Europe")
.child(ukraine)
.child(slovakia)
.child(austria)
.child(italy));
const searchContext = new SearchContext();
2020-05-25 06:25:47 +08:00
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy note.title', searchContext);
2020-05-25 06:25:47 +08:00
expect(searchResults.length).toEqual(4);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Slovakia");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Ukraine");
2020-05-25 06:25:47 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy note.labels.capital', searchContext);
2020-05-25 06:25:47 +08:00
expect(searchResults.length).toEqual(4);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Slovakia");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Ukraine");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Austria");
2020-05-26 05:39:34 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy note.labels.capital DESC', searchContext);
2020-05-26 05:39:34 +08:00
expect(searchResults.length).toEqual(4);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
expect(becca.notes[searchResults[2].noteId].title).toEqual("Ukraine");
expect(becca.notes[searchResults[3].noteId].title).toEqual("Slovakia");
2020-05-26 05:39:34 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy note.labels.capital DESC limit 2', searchContext);
2020-05-26 05:39:34 +08:00
expect(searchResults.length).toEqual(2);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Austria");
expect(becca.notes[searchResults[1].noteId].title).toEqual("Italy");
2020-05-26 05:39:34 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy #capital DESC limit 1', searchContext);
2021-04-05 04:44:22 +08:00
expect(searchResults.length).toEqual(1);
2020-05-26 05:39:34 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('# note.parents.title = Europe orderBy #capital DESC limit 1000', searchContext);
2020-05-26 05:39:34 +08:00
expect(searchResults.length).toEqual(4);
2020-05-25 06:25:47 +08:00
});
2020-08-20 05:00:51 +08:00
it("test not(...)", () => {
2020-05-27 06:09:19 +08:00
const italy = note("Italy").label("capital", "Rome");
const slovakia = note("Slovakia").label("capital", "Bratislava");
rootNote
.child(note("Europe")
.child(slovakia)
.child(italy));
const searchContext = new SearchContext();
2020-05-27 06:09:19 +08:00
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# not(#capital) and note.noteId != root', searchContext);
2020-05-27 06:09:19 +08:00
expect(searchResults.length).toEqual(1);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Europe");
2020-08-20 05:00:51 +08:00
2021-04-18 02:52:46 +08:00
searchResults = searchService.findResultsWithQuery('#!capital and note.noteId != root', searchContext);
2020-08-20 05:00:51 +08:00
expect(searchResults.length).toEqual(1);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Europe");
2020-08-20 05:00:51 +08:00
});
it("test note.text *=* something", () => {
const italy = note("Italy").label("capital", "Rome");
const slovakia = note("Slovakia").label("capital", "Bratislava");
rootNote
.child(note("Europe")
.child(slovakia)
.child(italy));
const searchContext = new SearchContext();
2020-08-20 05:00:51 +08:00
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('# note.text *=* vaki and note.noteId != root', searchContext);
2020-08-20 05:00:51 +08:00
expect(searchResults.length).toEqual(1);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Slovakia");
2020-05-27 06:09:19 +08:00
});
it("test that fulltext does not match archived notes", () => {
const italy = note("Italy").label("capital", "Rome");
const slovakia = note("Slovakia").label("capital", "Bratislava");
rootNote
.child(note("Reddit").label('archived', '', true)
.child(note('Post X'))
.child(note('Post Y')))
.child(note ('Reddit is bad'));
2020-09-24 03:05:26 +08:00
const searchContext = new SearchContext({excludeArchived: true});
2021-04-18 02:52:46 +08:00
let searchResults = searchService.findResultsWithQuery('reddit', searchContext);
expect(searchResults.length).toEqual(1);
2021-04-17 05:00:08 +08:00
expect(becca.notes[searchResults[0].noteId].title).toEqual("Reddit is bad");
});
2020-05-25 06:25:47 +08:00
// FIXME: test what happens when we order without any filter criteria
2020-07-20 05:19:45 +08:00
2020-08-20 05:00:51 +08:00
// it("comparison between labels", () => {
2020-07-20 05:19:45 +08:00
// rootNote
// .child(note("Europe")
// .child(note("Austria")
// .label('capital', 'Vienna')
// .label('largestCity', 'Vienna'))
// .child(note("Canada")
// .label('capital', 'Ottawa')
// .label('largestCity', 'Toronto'))
// .child(note("Czech Republic")
// .label('capital', 'Prague')
// .label('largestCity', 'Prague'))
// );
//
// const searchContext = new SearchContext();
2020-07-20 05:19:45 +08:00
//
2021-04-18 02:52:46 +08:00
// const searchResults = searchService.findResultsWithQuery('#capital = #largestCity', searchContext);
2020-07-20 05:19:45 +08:00
// expect(searchResults.length).toEqual(2);
// expect(findNoteByTitle(searchResults, "Czech Republic")).toBeTruthy();
// expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
// })
2020-05-25 06:25:47 +08:00
});