www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 1ecd934d042d4bb2ad0abed64f0aad60f0cb0a4d
parent 365f8181e3ed227b71b8dbe6cf45b3029a7ba22c
Author: Dan Stillman <dstillman@zotero.org>
Date:   Sun, 31 May 2015 17:46:26 -0400

Merge pull request #743 from egh/feature/search_tests

Add basic tests for fulltext search
Diffstat:
Mtest/content/support.js | 16++++++++++++++--
Atest/tests/data/search/foo.html | 9+++++++++
Atest/tests/data/search/foobar.html | 9+++++++++
Mtest/tests/searchTest.js | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/test/content/support.js b/test/content/support.js @@ -241,4 +241,17 @@ function resetDB() { }).then(function() { return Zotero.Schema.schemaUpdatePromise; }); -} -\ No newline at end of file +} + + +/** + * 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}); +} + diff --git a/test/tests/data/search/foo.html b/test/tests/data/search/foo.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> + <head> + <title>hello</title> + </head> + <body> + <p>foo</p> + </body> +</html> diff --git a/test/tests/data/search/foobar.html b/test/tests/data/search/foobar.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> + <head> + <title>hello</title> + </head> + <body> + <p>foo bar</p> + </body> +</html> diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js @@ -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]); + }); + }); });