2015-05-05 07:17:12 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.DataObject", function() {
|
|
|
|
var types = ['collection', 'item', 'search'];
|
|
|
|
|
2015-05-05 18:39:24 +00:00
|
|
|
describe("#loadAllData()", function () {
|
|
|
|
it("should load data on a regular item", function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-05 18:39:24 +00:00
|
|
|
yield item.loadAllData();
|
|
|
|
assert.throws(item.getNote.bind(item), 'getNote() can only be called on notes and attachments');
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should load data on an attachment item", function* () {
|
|
|
|
var item = new Zotero.Item('attachment');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-05 18:39:24 +00:00
|
|
|
yield item.loadAllData();
|
|
|
|
assert.equal(item.getNote(), '');
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should load data on a note item", function* () {
|
|
|
|
var item = new Zotero.Item('note');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield item.saveTx();
|
2015-05-05 18:39:24 +00:00
|
|
|
yield item.loadAllData();
|
|
|
|
assert.equal(item.getNote(), '');
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-05-20 23:28:35 +00:00
|
|
|
describe("#version", function () {
|
|
|
|
it("should be set to 0 after creating object", function* () {
|
|
|
|
for (let type of types) {
|
|
|
|
let obj = yield createDataObject(type);
|
|
|
|
assert.equal(obj.version, 0);
|
|
|
|
yield obj.eraseTx();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be set after creating object", function* () {
|
|
|
|
for (let type of types) {
|
|
|
|
Zotero.logError(type);
|
|
|
|
let obj = yield createDataObject(type, { version: 1234 });
|
|
|
|
assert.equal(obj.version, 1234, type + " version mismatch");
|
|
|
|
yield obj.eraseTx();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-05-13 23:30:22 +00:00
|
|
|
describe("#synced", function () {
|
|
|
|
it("should be set to false after creating item", function* () {
|
|
|
|
var item = new Zotero.Item("book");
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
assert.isFalse(item.synced);
|
2015-05-20 23:28:35 +00:00
|
|
|
item.eraseTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be set to true when changed", function* () {
|
|
|
|
var item = new Zotero.Item("book");
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
|
|
|
|
item.synced = 1;
|
2015-05-20 23:28:35 +00:00
|
|
|
yield item.saveTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
assert.ok(item.synced);
|
|
|
|
|
2015-05-20 23:28:35 +00:00
|
|
|
item.eraseTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be set to false after modifying item", function* () {
|
|
|
|
var item = new Zotero.Item("book");
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
|
|
|
|
item.synced = 1;
|
2015-05-20 23:28:35 +00:00
|
|
|
yield item.saveTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
|
|
|
|
yield item.loadItemData();
|
|
|
|
item.setField('title', 'test');
|
2015-05-20 23:28:35 +00:00
|
|
|
yield item.saveTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
assert.isFalse(item.synced);
|
|
|
|
|
2015-05-20 23:28:35 +00:00
|
|
|
item.eraseTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be unchanged if skipSyncedUpdate passed", function* () {
|
|
|
|
var item = new Zotero.Item("book");
|
|
|
|
var id = yield item.saveTx();
|
|
|
|
|
|
|
|
item.synced = 1;
|
2015-05-20 23:28:35 +00:00
|
|
|
yield item.saveTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
|
|
|
|
yield item.loadItemData();
|
|
|
|
item.setField('title', 'test');
|
2015-05-20 23:28:35 +00:00
|
|
|
yield item.saveTx({
|
2015-05-13 23:30:22 +00:00
|
|
|
skipSyncedUpdate: true
|
|
|
|
});
|
|
|
|
assert.ok(item.synced);
|
|
|
|
|
2015-05-20 23:28:35 +00:00
|
|
|
item.eraseTx();
|
2015-05-13 23:30:22 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-05-05 07:17:12 +00:00
|
|
|
describe("#save()", function () {
|
|
|
|
it("should add new identifiers to cache", function* () {
|
|
|
|
// Collection
|
|
|
|
var objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType('collection');
|
|
|
|
var obj = new Zotero.Collection;
|
|
|
|
obj.name = "Test";
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield obj.saveTx();
|
2015-05-05 07:17:12 +00:00
|
|
|
var { libraryID, key } = objectsClass.getLibraryAndKeyFromID(id);
|
|
|
|
assert.typeOf(key, 'string');
|
|
|
|
assert.equal(objectsClass.getIDFromLibraryAndKey(libraryID, key), id);
|
|
|
|
|
|
|
|
// Search
|
|
|
|
var objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType('search');
|
|
|
|
var obj = new Zotero.Search;
|
|
|
|
obj.name = "Test";
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield obj.saveTx();
|
2015-05-05 07:17:12 +00:00
|
|
|
var { libraryID, key } = objectsClass.getLibraryAndKeyFromID(id);
|
|
|
|
assert.typeOf(key, 'string');
|
|
|
|
assert.equal(objectsClass.getIDFromLibraryAndKey(libraryID, key), id);
|
|
|
|
|
|
|
|
// Item
|
|
|
|
var objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType('item');
|
|
|
|
var obj = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var id = yield obj.saveTx();
|
2015-05-05 07:17:12 +00:00
|
|
|
var { libraryID, key } = objectsClass.getLibraryAndKeyFromID(id);
|
|
|
|
assert.typeOf(key, 'string');
|
|
|
|
assert.equal(objectsClass.getIDFromLibraryAndKey(libraryID, key), id);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|