Add basic tests for full-text search
- also add importFileAttachment support function
This commit is contained in:
parent
9e573cdce2
commit
aac1255d3d
4 changed files with 96 additions and 1 deletions
|
@ -241,4 +241,17 @@ function resetDB() {
|
|||
}).then(function() {
|
||||
return Zotero.Schema.schemaUpdatePromise;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Imports an attachment from a test file.
|
||||
* @param {string} filename - The filename to import (in data directory)
|
||||
* @return {Promise<Zotero.Item>}
|
||||
*/
|
||||
function importFileAttachment(filename) {
|
||||
let testfile = getTestDataDirectory();
|
||||
filename.split('/').forEach((part) => testfile.append(part));
|
||||
return Zotero.Attachments.importFromFile({file: testfile});
|
||||
}
|
||||
|
||||
|
|
9
test/tests/data/search/foo.html
Normal file
9
test/tests/data/search/foo.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>hello</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>foo</p>
|
||||
</body>
|
||||
</html>
|
9
test/tests/data/search/foobar.html
Normal file
9
test/tests/data/search/foobar.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>hello</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>foo bar</p>
|
||||
</body>
|
||||
</html>
|
|
@ -83,4 +83,68 @@ describe("Zotero.Search", function() {
|
|||
assert.propertyVal(conditions[0], 'value', 'foo')
|
||||
});
|
||||
});
|
||||
|
||||
describe("#search()", function () {
|
||||
let win;
|
||||
let fooItem;
|
||||
let foobarItem;
|
||||
|
||||
before(function* () {
|
||||
// Hidden browser, which requires a browser window, needed for charset detection
|
||||
// (until we figure out a better way)
|
||||
win = yield loadBrowserWindow();
|
||||
fooItem = yield importFileAttachment("search/foo.html");
|
||||
foobarItem = yield importFileAttachment("search/foobar.html");
|
||||
});
|
||||
|
||||
after(function* () {
|
||||
if (win) {
|
||||
win.close();
|
||||
}
|
||||
yield fooItem.erase();
|
||||
yield foobarItem.erase();
|
||||
});
|
||||
|
||||
it("should return matches with full-text conditions", function* () {
|
||||
let s = new Zotero.Search();
|
||||
s.addCondition('fulltextWord', 'contains', 'foo');
|
||||
let matches = yield s.search();
|
||||
assert.lengthOf(matches, 2);
|
||||
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
||||
});
|
||||
|
||||
it("should not return non-matches with full-text conditions", function* () {
|
||||
let s = new Zotero.Search();
|
||||
s.addCondition('fulltextWord', 'contains', 'baz');
|
||||
let matches = yield s.search();
|
||||
assert.lengthOf(matches, 0);
|
||||
});
|
||||
|
||||
it("should return matches for full-text conditions in ALL mode", function* () {
|
||||
let s = new Zotero.Search();
|
||||
s.addCondition('joinMode', 'all');
|
||||
s.addCondition('fulltextWord', 'contains', 'foo');
|
||||
s.addCondition('fulltextWord', 'contains', 'bar');
|
||||
let matches = yield s.search();
|
||||
assert.deepEqual(matches, [foobarItem.id]);
|
||||
});
|
||||
|
||||
it("should not return non-matches for full-text conditions in ALL mode", function* () {
|
||||
let s = new Zotero.Search();
|
||||
s.addCondition('joinMode', 'all');
|
||||
s.addCondition('fulltextWord', 'contains', 'mjktkiuewf');
|
||||
s.addCondition('fulltextWord', 'contains', 'zijajkvudk');
|
||||
let matches = yield s.search();
|
||||
assert.lengthOf(matches, 0);
|
||||
});
|
||||
|
||||
it("should return a match that satisfies only one of two full-text condition in ANY mode", function* () {
|
||||
let s = new Zotero.Search();
|
||||
s.addCondition('joinMode', 'any');
|
||||
s.addCondition('fulltextWord', 'contains', 'bar');
|
||||
s.addCondition('fulltextWord', 'contains', 'baz');
|
||||
let matches = yield s.search();
|
||||
assert.deepEqual(matches, [foobarItem.id]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue