commit aac1255d3d181923f0bb5af2eb8f66168c5f83b3
parent 9e573cdce284ad7049f5698e2f3d31faf6145a14
Author: Erik Hetzner <ehetzner@plos.org>
Date: Sun, 31 May 2015 14:39:37 -0700
Add basic tests for full-text search
- also add importFileAttachment support function
Diffstat:
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]);
+ });
+ });
});