2015-04-17 04:20:16 +00:00
|
|
|
describe("Zotero.Search", function() {
|
2017-02-21 05:02:39 +00:00
|
|
|
describe("#addCondition()", function () {
|
|
|
|
it("should convert old-style 'collection' condition value", function* () {
|
|
|
|
var col = yield createDataObject('collection');
|
|
|
|
var item = yield createDataObject('item', { collections: [col.id] });
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('collection', 'is', '0_' + col.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [item.id]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is for Zotero.Search._loadConditions()
|
|
|
|
describe("Loading", function () {
|
|
|
|
it("should convert old-style 'collection' condition value", function* () {
|
|
|
|
var col = yield createDataObject('collection');
|
|
|
|
var item = yield createDataObject('item', { collections: [col.id] });
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('collection', 'is', col.key);
|
|
|
|
yield s.saveTx();
|
|
|
|
yield Zotero.DB.queryAsync(
|
|
|
|
"UPDATE savedSearchConditions SET value=? WHERE savedSearchID=? AND condition=?",
|
|
|
|
["0_" + col.key, s.id, 'collection']
|
|
|
|
);
|
|
|
|
yield s.reload(['conditions'], true);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [item.id]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-04-17 04:20:16 +00:00
|
|
|
describe("#save()", function () {
|
|
|
|
it("should fail without a name", function* () {
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.addCondition('title', 'is', 'test');
|
2015-05-10 08:20:47 +00:00
|
|
|
var e = yield getPromiseError(s.saveTx());
|
2015-04-17 04:20:16 +00:00
|
|
|
assert.ok(e);
|
|
|
|
assert.equal(e.constructor.name, Error.prototype.constructor.name); // TEMP: Error mismatch
|
|
|
|
assert.equal(e.message, "Name not provided for saved search");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should save a new search", function* () {
|
|
|
|
// Save search
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('title', 'is', 'test');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield s.saveTx();
|
2015-04-17 04:20:16 +00:00
|
|
|
assert.typeOf(id, 'number');
|
|
|
|
|
|
|
|
// Check saved search
|
Deasyncification :back: :cry:
While trying to get translation and citing working with asynchronously
generated data, we realized that drag-and-drop support was going to
be...problematic. Firefox only supports synchronous methods for
providing drag data (unlike, it seems, the DataTransferItem interface
supported by Chrome), which means that we'd need to preload all relevant
data on item selection (bounded by export.quickCopy.dragLimit) and keep
the translate/cite methods synchronous (or maintain two separate
versions).
What we're trying instead is doing what I said in #518 we weren't going
to do: loading most object data on startup and leaving many more
functions synchronous. Essentially, this takes the various load*()
methods described in #518, moves them to startup, and makes them operate
on entire libraries rather than individual objects.
The obvious downside here (other than undoing much of the work of the
last many months) is that it increases startup time, potentially quite a
lot for larger libraries. On my laptop, with a 3,000-item library, this
adds about 3 seconds to startup time. I haven't yet tested with larger
libraries. But I'm hoping that we can optimize this further to reduce
that delay. Among other things, this is loading data for all libraries,
when it should be able to load data only for the library being viewed.
But this is also fundamentally just doing some SELECT queries and
storing the results, so it really shouldn't need to be that slow (though
performance may be bounded a bit here by XPCOM overhead).
If we can make this fast enough, it means that third-party plugins
should be able to remain much closer to their current designs. (Some
things, including saving, will still need to be made asynchronous.)
2016-03-07 21:05:51 +00:00
|
|
|
s = Zotero.Searches.get(id);
|
2015-04-17 04:20:16 +00:00
|
|
|
assert.ok(s);
|
|
|
|
assert.instanceOf(s, Zotero.Search);
|
2015-05-05 07:13:08 +00:00
|
|
|
assert.equal(s.libraryID, Zotero.Libraries.userLibraryID);
|
2015-04-17 04:20:16 +00:00
|
|
|
assert.equal(s.name, "Test");
|
|
|
|
var conditions = s.getConditions();
|
|
|
|
assert.lengthOf(Object.keys(conditions), 1);
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.property(conditions, "0");
|
|
|
|
var condition = conditions[0];
|
2015-04-17 04:20:16 +00:00
|
|
|
assert.propertyVal(condition, 'condition', 'title')
|
|
|
|
assert.propertyVal(condition, 'operator', 'is')
|
|
|
|
assert.propertyVal(condition, 'value', 'test')
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.propertyVal(condition, 'required', false)
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should add a condition to an existing search", function* () {
|
|
|
|
// Save search
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('title', 'is', 'test');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield s.saveTx();
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.typeOf(id, 'number');
|
|
|
|
|
|
|
|
// Add condition
|
|
|
|
s = yield Zotero.Searches.getAsync(id);
|
|
|
|
s.addCondition('title', 'contains', 'foo');
|
2015-05-10 08:20:47 +00:00
|
|
|
var saved = yield s.saveTx();
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.isTrue(saved);
|
|
|
|
|
|
|
|
// Check saved search
|
|
|
|
s = yield Zotero.Searches.getAsync(id);
|
|
|
|
var conditions = s.getConditions();
|
|
|
|
assert.lengthOf(Object.keys(conditions), 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove a condition from an existing search", function* () {
|
|
|
|
// Save search
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('title', 'is', 'test');
|
|
|
|
s.addCondition('title', 'contains', 'foo');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield s.saveTx();
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.typeOf(id, 'number');
|
|
|
|
|
|
|
|
// Remove condition
|
|
|
|
s = yield Zotero.Searches.getAsync(id);
|
|
|
|
s.removeCondition(0);
|
2015-05-10 08:20:47 +00:00
|
|
|
var saved = yield s.saveTx();
|
2015-04-17 23:27:37 +00:00
|
|
|
assert.isTrue(saved);
|
|
|
|
|
|
|
|
// Check saved search
|
|
|
|
s = yield Zotero.Searches.getAsync(id);
|
|
|
|
var conditions = s.getConditions();
|
|
|
|
assert.lengthOf(Object.keys(conditions), 1);
|
|
|
|
assert.property(conditions, "0");
|
|
|
|
assert.propertyVal(conditions[0], 'value', 'foo')
|
2015-04-17 04:20:16 +00:00
|
|
|
});
|
|
|
|
});
|
2015-05-31 21:39:37 +00:00
|
|
|
|
|
|
|
describe("#search()", function () {
|
2017-07-26 09:35:58 +00:00
|
|
|
var win;
|
|
|
|
var userLibraryID;
|
|
|
|
var fooItem;
|
|
|
|
var foobarItem;
|
2019-02-19 06:58:22 +00:00
|
|
|
var bazItem;
|
2017-07-26 09:35:58 +00:00
|
|
|
var fooItemGroup;
|
|
|
|
var foobarItemGroup;
|
2019-02-19 06:58:22 +00:00
|
|
|
var bazItemGroup;
|
2015-05-31 21:39:37 +00:00
|
|
|
|
2019-02-19 06:58:22 +00:00
|
|
|
before(async function () {
|
|
|
|
await resetDB({
|
|
|
|
thisArg: this,
|
|
|
|
skipBundledFiles: true
|
|
|
|
});
|
|
|
|
|
2015-05-31 21:39:37 +00:00
|
|
|
// Hidden browser, which requires a browser window, needed for charset detection
|
|
|
|
// (until we figure out a better way)
|
2019-02-19 06:58:22 +00:00
|
|
|
win = await loadBrowserWindow();
|
|
|
|
fooItem = await importFileAttachment("search/foo.html");
|
|
|
|
foobarItem = await importFileAttachment("search/foobar.html");
|
|
|
|
bazItem = await importFileAttachment("search/baz.pdf");
|
2017-07-26 09:35:58 +00:00
|
|
|
userLibraryID = fooItem.libraryID;
|
|
|
|
|
2019-02-19 06:58:22 +00:00
|
|
|
let group = await getGroup();
|
|
|
|
fooItemGroup = await importFileAttachment("search/foo.html", { libraryID: group.libraryID });
|
|
|
|
foobarItemGroup = await importFileAttachment("search/foobar.html", { libraryID: group.libraryID });
|
|
|
|
bazItemGroup = await importFileAttachment("search/baz.pdf", { libraryID: group.libraryID });
|
2015-05-31 21:39:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function* () {
|
|
|
|
if (win) {
|
|
|
|
win.close();
|
|
|
|
}
|
2016-05-06 08:08:09 +00:00
|
|
|
yield fooItem.eraseTx();
|
|
|
|
yield foobarItem.eraseTx();
|
2019-02-19 06:58:22 +00:00
|
|
|
yield bazItem.eraseTx();
|
2017-07-26 09:35:58 +00:00
|
|
|
yield fooItemGroup.eraseTx();
|
|
|
|
yield foobarItemGroup.eraseTx();
|
2019-02-19 06:58:22 +00:00
|
|
|
yield bazItemGroup.eraseTx();
|
2015-05-31 21:39:37 +00:00
|
|
|
});
|
2016-05-06 08:08:09 +00:00
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
describe("Conditions", function () {
|
2022-10-18 02:54:59 +00:00
|
|
|
describe("title", function () {
|
|
|
|
// TEMP
|
|
|
|
it("shouldn't match parent attachments with annotations for 'title' 'does not contain' condition", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var title = "Attachment Title";
|
|
|
|
attachment.setField('title', title);
|
|
|
|
await attachment.saveTx();
|
|
|
|
await createAnnotation('highlight', attachment);
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('title', 'doesNotContain', title);
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notInclude(matches, attachment.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
describe("collection", function () {
|
|
|
|
it("should find item in collection", function* () {
|
|
|
|
var col = yield createDataObject('collection');
|
|
|
|
var item = yield createDataObject('item', { collections: [col.id] });
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.addCondition('collection', 'is', col.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [item.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find items not in collection", function* () {
|
|
|
|
var col = yield createDataObject('collection');
|
|
|
|
var item = yield createDataObject('item', { collections: [col.id] });
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.addCondition('collection', 'isNot', col.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.notInclude(matches, item.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't find item in collection with no items", function* () {
|
|
|
|
var col = yield createDataObject('collection');
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.addCondition('collection', 'is', col.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find item in subcollection in recursive mode", function* () {
|
|
|
|
var col1 = yield createDataObject('collection');
|
|
|
|
var col2 = yield createDataObject('collection', { parentID: col1.id });
|
|
|
|
var item = yield createDataObject('item', { collections: [col2.id] });
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.addCondition('collection', 'is', col1.key);
|
|
|
|
s.addCondition('recursive', 'true');
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [item.id]);
|
|
|
|
});
|
2018-11-28 22:19:19 +00:00
|
|
|
|
|
|
|
it("should return no results for a collection that doesn't exist in recursive mode", async function () {
|
|
|
|
var item = await createDataObject('item');
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('collection', 'is', Zotero.DataObjectUtilities.generateKey());
|
|
|
|
s.addCondition('recursive', 'true');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
2016-10-06 05:14:37 +00:00
|
|
|
});
|
2016-05-06 08:08:09 +00:00
|
|
|
|
2022-08-17 00:49:07 +00:00
|
|
|
describe("tag", function () {
|
|
|
|
// TEMP
|
|
|
|
it("should match parent attachments for annotation tags", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var annotation = await createAnnotation('highlight', attachment);
|
|
|
|
var tag = Zotero.Utilities.randomString();
|
|
|
|
annotation.addTag(tag);
|
|
|
|
await annotation.saveTx();
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('tag', 'is', tag);
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.sameMembers(matches, [attachment.id]);
|
|
|
|
});
|
2022-10-18 02:54:59 +00:00
|
|
|
|
|
|
|
// TEMP
|
|
|
|
it("shouldn't match parent attachments with annotations for 'tag' 'is not' condition", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
await createAnnotation('highlight', attachment);
|
|
|
|
var tag = Zotero.Utilities.randomString();
|
|
|
|
attachment.addTag(tag);
|
|
|
|
await attachment.saveTx();
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('tag', 'isNot', tag);
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notInclude(matches, attachment.id);
|
|
|
|
});
|
2022-08-17 00:49:07 +00:00
|
|
|
});
|
|
|
|
|
2019-12-06 10:12:10 +00:00
|
|
|
describe("dateAdded", function () {
|
|
|
|
it("should handle 'today'", async function () {
|
|
|
|
var item = await createDataObject('item');
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('dateAdded', 'is', 'today');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.includeMembers(matches, [item.id]);
|
|
|
|
|
|
|
|
// Make sure 'yesterday' doesn't match
|
|
|
|
s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('dateAdded', 'is', 'yesterday');
|
|
|
|
matches = await s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
describe("fileTypeID", function () {
|
|
|
|
it("should search by attachment file type", function* () {
|
|
|
|
let s = new Zotero.Search();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2016-10-06 05:14:37 +00:00
|
|
|
s.addCondition('fileTypeID', 'is', Zotero.FileTypes.getID('webpage'));
|
|
|
|
let matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
|
|
|
});
|
|
|
|
});
|
2016-05-06 08:08:09 +00:00
|
|
|
|
2017-07-26 09:35:58 +00:00
|
|
|
describe("fulltextContent", function () {
|
|
|
|
it("should find text in HTML files", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('fulltextContent', 'contains', 'foo bar');
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work in subsearch", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('fulltextContent', 'contains', 'foo bar');
|
|
|
|
|
|
|
|
var s2 = new Zotero.Search();
|
|
|
|
s2.setScope(s);
|
|
|
|
s2.addCondition('title', 'contains', 'foobar');
|
|
|
|
var matches = yield s2.search();
|
|
|
|
assert.sameMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
2019-02-19 06:58:22 +00:00
|
|
|
it("should find matching items with joinMode=ANY with no other conditions", async function () {
|
2017-07-26 09:35:58 +00:00
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
2019-02-19 06:58:22 +00:00
|
|
|
s.addCondition('fulltextContent', 'contains', 'foo');
|
|
|
|
s.addCondition('fulltextContent', 'contains', 'bar');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find matching items with joinMode=ANY and non-matching other condition", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent', 'contains', 'foo');
|
|
|
|
s.addCondition('fulltextContent', 'contains', 'bar');
|
2017-07-26 09:35:58 +00:00
|
|
|
s.addCondition('title', 'contains', 'nomatch');
|
|
|
|
var matches = yield s.search();
|
2019-02-19 06:58:22 +00:00
|
|
|
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
2017-07-26 09:35:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should find matching items in regexp mode with joinMode=ANY with matching other condition", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
|
|
|
|
s.addCondition('title', 'is', fooItem.getField('title'));
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find matching item in regexp mode with joinMode=ANY and non-matching other condition", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
|
|
|
|
s.addCondition('title', 'contains', 'nomatch');
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find item matching other condition in regexp mode when joinMode=ANY", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent/regexp', 'contains', 'nomatch');
|
|
|
|
s.addCondition('title', 'is', foobarItem.getField('title'));
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find matching item in regexp mode with joinMode=ANY and recursive mode flag", function* () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
|
|
|
|
s.addCondition('recursive', 'true');
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.sameMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
2019-02-19 06:58:22 +00:00
|
|
|
|
|
|
|
it("should find items that don't contain a single word with joinMode=ANY", async function () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent', 'doesNotContain', 'foo');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notIncludeMembers(matches, [fooItem.id, foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find items that don't contain a phrase with joinMode=ANY", async function () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent', 'doesNotContain', 'foo bar');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notIncludeMembers(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find items that don't contain a regexp pattern with joinMode=ANY", async function () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextContent/regexp', 'doesNotContain', 'foo.+bar');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notIncludeMembers(matches, [foobarItem.id]);
|
|
|
|
assert.includeMembers(matches, [fooItem.id, bazItem.id]);
|
|
|
|
});
|
2017-07-26 09:35:58 +00:00
|
|
|
});
|
|
|
|
|
2020-09-20 06:28:44 +00:00
|
|
|
describe("annotationText", function () {
|
|
|
|
it("should return matches for annotation text", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var annotation = await createAnnotation('highlight', attachment);
|
|
|
|
var str = annotation.annotationText.substr(0, 7);
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('annotationText', 'contains', str);
|
|
|
|
var matches = await s.search();
|
2022-08-17 00:49:07 +00:00
|
|
|
// TEMP: Match parent attachment
|
|
|
|
assert.sameMembers(matches, [attachment.id]);
|
2020-09-20 06:28:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("annotationComment", function () {
|
|
|
|
it("should return matches for annotation comment", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var annotation = await createAnnotation('note', attachment);
|
|
|
|
var str = annotation.annotationComment.substr(0, 7);
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('annotationComment', 'contains', str);
|
|
|
|
var matches = await s.search();
|
2022-08-17 00:49:07 +00:00
|
|
|
// TEMP: Match parent attachment
|
|
|
|
assert.sameMembers(matches, [attachment.id]);
|
2020-09-20 06:28:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
describe("fulltextWord", function () {
|
|
|
|
it("should return matches with full-text conditions", function* () {
|
|
|
|
let s = new Zotero.Search();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2016-10-06 05:14:37 +00:00
|
|
|
s.addCondition('fulltextWord', 'contains', 'foo');
|
|
|
|
let matches = yield s.search();
|
|
|
|
assert.lengthOf(matches, 2);
|
|
|
|
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
|
|
|
|
});
|
2016-05-06 08:08:09 +00:00
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
it("should not return non-matches with full-text conditions", function* () {
|
|
|
|
let s = new Zotero.Search();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2019-02-19 06:58:22 +00:00
|
|
|
s.addCondition('fulltextWord', 'contains', 'nomatch');
|
2016-10-06 05:14:37 +00:00
|
|
|
let matches = yield s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
2016-05-06 08:08:09 +00:00
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
it("should return matches for full-text conditions in ALL mode", function* () {
|
|
|
|
let s = new Zotero.Search();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2016-10-06 05:14:37 +00:00
|
|
|
s.addCondition('joinMode', 'all');
|
|
|
|
s.addCondition('fulltextWord', 'contains', 'foo');
|
|
|
|
s.addCondition('fulltextWord', 'contains', 'bar');
|
|
|
|
let matches = yield s.search();
|
|
|
|
assert.deepEqual(matches, [foobarItem.id]);
|
|
|
|
});
|
2016-04-25 04:48:38 +00:00
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
it("should not return non-matches for full-text conditions in ALL mode", function* () {
|
|
|
|
let s = new Zotero.Search();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2016-10-06 05:14:37 +00:00
|
|
|
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();
|
2017-07-26 09:35:58 +00:00
|
|
|
s.libraryID = userLibraryID;
|
2016-10-06 05:14:37 +00:00
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('fulltextWord', 'contains', 'bar');
|
2019-02-19 06:58:22 +00:00
|
|
|
s.addCondition('fulltextWord', 'contains', 'nomatch');
|
2016-10-06 05:14:37 +00:00
|
|
|
let matches = yield s.search();
|
|
|
|
assert.deepEqual(matches, [foobarItem.id]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-11 05:22:00 +00:00
|
|
|
describe("includeParentsAndChildren", function () {
|
|
|
|
it("should handle ANY search with no-op condition", async function () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('savedSearch', 'is', Zotero.Utilities.randomString());
|
|
|
|
s.addCondition('includeParentsAndChildren', 'true');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
2020-02-11 18:08:20 +00:00
|
|
|
|
|
|
|
it("should handle ANY search with two no-op conditions", async function () {
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('joinMode', 'any');
|
|
|
|
s.addCondition('savedSearch', 'is', Zotero.Utilities.randomString());
|
|
|
|
s.addCondition('savedSearch', 'is', Zotero.Utilities.randomString());
|
|
|
|
s.addCondition('includeParentsAndChildren', 'true');
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
2020-02-11 05:22:00 +00:00
|
|
|
});
|
|
|
|
|
2017-01-21 08:38:36 +00:00
|
|
|
describe("key", function () {
|
|
|
|
it("should allow more than max bound parameters", function* () {
|
|
|
|
let s = new Zotero.Search();
|
|
|
|
let max = Zotero.DB.MAX_BOUND_PARAMETERS + 100;
|
|
|
|
for (let i = 0; i < max; i++) {
|
|
|
|
s.addCondition('key', 'is', Zotero.DataObjectUtilities.generateKey());
|
|
|
|
}
|
|
|
|
yield s.search();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-06 05:14:37 +00:00
|
|
|
describe("savedSearch", function () {
|
|
|
|
it("should return items in the saved search", function* () {
|
|
|
|
var search = yield createDataObject('search');
|
|
|
|
var itemTitle = search.getConditions()[0].value;
|
|
|
|
var item = yield createDataObject('item', { title: itemTitle })
|
|
|
|
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
s.addCondition('savedSearch', 'is', search.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.deepEqual(matches, [item.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return items not in the saved search for isNot operator", function* () {
|
|
|
|
var search = yield createDataObject('search');
|
|
|
|
var itemTitle = search.getConditions()[0].value;
|
|
|
|
var item = yield createDataObject('item', { title: itemTitle })
|
|
|
|
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
s.addCondition('savedSearch', 'isNot', search.key);
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.notInclude(matches, item.id);
|
|
|
|
});
|
2018-11-28 22:19:19 +00:00
|
|
|
|
|
|
|
it("should return no results for a search that doesn't exist", async function () {
|
|
|
|
var item = await createDataObject('item');
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = item.libraryID;
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('savedSearch', 'is', Zotero.DataObjectUtilities.generateKey());
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.lengthOf(matches, 0);
|
|
|
|
});
|
2016-10-06 05:14:37 +00:00
|
|
|
});
|
2017-04-18 01:34:08 +00:00
|
|
|
|
|
|
|
describe("unfiled", function () {
|
|
|
|
it("shouldn't include items in My Publications", function* () {
|
|
|
|
var item1 = yield createDataObject('item');
|
|
|
|
var item2 = yield createDataObject('item', { inPublications: true });
|
|
|
|
|
|
|
|
var s = new Zotero.Search;
|
|
|
|
s.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
s.addCondition('unfiled', 'true');
|
|
|
|
var matches = yield s.search();
|
|
|
|
assert.include(matches, item1.id);
|
|
|
|
assert.notInclude(matches, item2.id);
|
|
|
|
});
|
|
|
|
});
|
2022-08-17 00:49:07 +00:00
|
|
|
|
|
|
|
describe("Quick search", function () {
|
|
|
|
describe("All Fields & Tags", function () {
|
|
|
|
it("should match parent attachment for annotation tag", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var annotation = await createAnnotation('highlight', attachment);
|
|
|
|
var tag = Zotero.Utilities.randomString();
|
|
|
|
annotation.addTag(tag);
|
|
|
|
await annotation.saveTx();
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('quicksearch-fields', 'contains', tag);
|
|
|
|
var matches = await s.search();
|
|
|
|
// TEMP: Match parent attachment
|
|
|
|
assert.sameMembers(matches, [attachment.id]);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("Everything", function () {
|
|
|
|
it("should match parent attachment for annotation comment", async function () {
|
|
|
|
var attachment = await importPDFAttachment();
|
|
|
|
var annotation = await createAnnotation('highlight', attachment);
|
|
|
|
var comment = annotation.annotationComment;
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
s.addCondition('quicksearch-everything', 'contains', comment);
|
|
|
|
var matches = await s.search();
|
|
|
|
// TEMP: Match parent attachment
|
|
|
|
assert.sameMembers(matches, [attachment.id]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-11-28 09:30:22 +00:00
|
|
|
|
|
|
|
describe("deleted", function () {
|
|
|
|
describe("if not present", function () {
|
|
|
|
it("should not match regular items in trash with annotated child attachments", async function () {
|
|
|
|
var item = await createDataObject('item');
|
|
|
|
item.deleted = true;
|
|
|
|
await item.saveTx();
|
|
|
|
var attachment = await importPDFAttachment(item);
|
|
|
|
await createAnnotation('highlight', attachment);
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notInclude(matches, attachment.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not match regular items with annotated child attachments in trash", async function () {
|
|
|
|
var item = await createDataObject('item');
|
|
|
|
var attachment = await importPDFAttachment(item);
|
|
|
|
attachment.deleted = true;
|
|
|
|
await attachment.saveTx();
|
|
|
|
await createAnnotation('highlight', attachment);
|
|
|
|
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.libraryID = userLibraryID;
|
|
|
|
var matches = await s.search();
|
|
|
|
assert.notInclude(matches, attachment.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-04-25 04:48:38 +00:00
|
|
|
});
|
2015-05-31 21:39:37 +00:00
|
|
|
});
|
2016-03-26 06:59:54 +00:00
|
|
|
|
2021-01-13 05:40:13 +00:00
|
|
|
describe("#deleted", function () {
|
|
|
|
it("should set trash status", async function () {
|
|
|
|
var search = await createDataObject('search');
|
|
|
|
assert.isFalse(search.deleted);
|
|
|
|
search.deleted = true;
|
|
|
|
await search.saveTx();
|
|
|
|
assert.isTrue(search.deleted);
|
|
|
|
search.deleted = false;
|
|
|
|
await search.saveTx();
|
|
|
|
assert.isFalse(search.deleted);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-26 06:59:54 +00:00
|
|
|
describe("#toJSON()", function () {
|
|
|
|
it("should output all data", function* () {
|
|
|
|
let s = new Zotero.Search();
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('joinMode', 'any');
|
2016-06-20 06:08:25 +00:00
|
|
|
s.addCondition('fulltextContent/regexp', 'contains', 's.+');
|
2016-03-26 06:59:54 +00:00
|
|
|
let json = s.toJSON();
|
|
|
|
assert.equal(json.name, "Test");
|
2016-06-20 06:08:25 +00:00
|
|
|
|
2016-03-26 06:59:54 +00:00
|
|
|
assert.lengthOf(json.conditions, 2);
|
2016-06-20 06:08:25 +00:00
|
|
|
|
2016-03-26 06:59:54 +00:00
|
|
|
assert.equal(json.conditions[0].condition, 'joinMode');
|
|
|
|
assert.equal(json.conditions[0].operator, 'any');
|
2016-06-20 06:08:25 +00:00
|
|
|
// TODO: Change to 'is' + 'any'?
|
|
|
|
assert.strictEqual(json.conditions[0].value, '');
|
|
|
|
assert.notProperty(json.conditions[0], 'id');
|
|
|
|
assert.notProperty(json.conditions[0], 'required');
|
|
|
|
assert.notProperty(json.conditions[0], 'mode');
|
|
|
|
|
|
|
|
assert.equal(json.conditions[1].condition, 'fulltextContent/regexp');
|
2016-03-26 06:59:54 +00:00
|
|
|
assert.equal(json.conditions[1].operator, 'contains');
|
2016-06-20 06:08:25 +00:00
|
|
|
assert.equal(json.conditions[1].value, 's.+');
|
|
|
|
assert.notProperty(json.conditions[1], 'mode');
|
2016-03-26 06:59:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#fromJSON()", function () {
|
|
|
|
it("should update all data", function* () {
|
|
|
|
let s = new Zotero.Search();
|
|
|
|
s.name = "Test";
|
|
|
|
s.addCondition('joinMode', 'any');
|
2016-07-07 11:55:15 +00:00
|
|
|
s.addCondition('title', 'isNot', 'foo');
|
2016-03-26 06:59:54 +00:00
|
|
|
let json = s.toJSON();
|
|
|
|
json.name = "Test 2";
|
|
|
|
json.conditions = [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'contains',
|
|
|
|
value: 'foo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
condition: 'year',
|
|
|
|
operator: 'is',
|
|
|
|
value: '2016'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
s.fromJSON(json);
|
|
|
|
assert.equal(s.name, "Test 2");
|
|
|
|
var conditions = s.getConditions();
|
|
|
|
assert.lengthOf(Object.keys(conditions), 2);
|
|
|
|
assert.equal(conditions["0"].condition, 'title');
|
|
|
|
assert.equal(conditions["0"].operator, 'contains');
|
|
|
|
assert.equal(conditions["0"].value, 'foo');
|
|
|
|
assert.equal(conditions["1"].condition, 'year');
|
|
|
|
assert.equal(conditions["1"].operator, 'is');
|
|
|
|
assert.equal(conditions["1"].value, '2016');
|
|
|
|
});
|
Type/field handling overhaul
This changes the way item types, item fields, creator types, and CSL
mappings are defined and handled, in preparation for updated types and
fields.
Instead of being predefined in SQL files or code, type/field info is
read from a bundled JSON file shared with other parts of the Zotero
ecosystem [1], referred to as the "global schema". Updates to the
bundled schema file are automatically applied to the database at first
run, allowing changes to be made consistently across apps.
When syncing, invalid JSON properties are now rejected instead of being
ignored and processed later, which will allow for schema changes to be
made without causing problems in existing clients. We considered many
alternative approaches, but this approach is by far the simplest,
safest, and most transparent to the user.
For now, there are no actual changes to types and fields, since we'll
first need to do a sync cut-off for earlier versions that don't reject
invalid properties.
For third-party code, the main change is that type and field IDs should
no longer be hard-coded, since they may not be consistent in new
installs. For example, code should use `Zotero.ItemTypes.getID('note')`
instead of hard-coding `1`.
[1] https://github.com/zotero/zotero-schema
2019-05-16 08:56:46 +00:00
|
|
|
|
|
|
|
it("should ignore unknown property in non-strict mode", function () {
|
|
|
|
var json = {
|
|
|
|
name: "Search",
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'contains',
|
|
|
|
value: 'foo'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
foo: "Bar"
|
|
|
|
};
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
s.fromJSON(json);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw on unknown property in strict mode", function () {
|
|
|
|
var json = {
|
|
|
|
name: "Search",
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'contains',
|
|
|
|
value: 'foo'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
foo: "Bar"
|
|
|
|
};
|
|
|
|
var s = new Zotero.Search();
|
|
|
|
var f = () => {
|
|
|
|
s.fromJSON(json, { strict: true });
|
|
|
|
};
|
|
|
|
assert.throws(f, /^Unknown search property/);
|
|
|
|
});
|
2016-03-26 06:59:54 +00:00
|
|
|
});
|
2015-04-17 04:20:16 +00:00
|
|
|
});
|