2015-05-25 02:14:52 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.DataObjects", function () {
|
|
|
|
var types = ['collection', 'item', 'search'];
|
|
|
|
|
2016-05-05 10:03:32 +00:00
|
|
|
describe("#get()", function () {
|
|
|
|
it("should return false for nonexistent objects", function* () {
|
|
|
|
assert.isFalse(Zotero.Items.get(3464363));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-25 02:14:52 +00:00
|
|
|
describe("#getLibraryAndKeyFromID()", function () {
|
|
|
|
it("should return a libraryID and key within a transaction", function* () {
|
|
|
|
for (let type of types) {
|
|
|
|
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
|
|
|
let obj = createUnsavedDataObject(type);
|
|
|
|
yield obj.save();
|
|
|
|
|
|
|
|
var {libraryID, key} = objectsClass.getLibraryAndKeyFromID(obj.id);
|
|
|
|
assert.equal(libraryID, Zotero.Libraries.userLibraryID);
|
|
|
|
assert.ok(key);
|
|
|
|
assert.typeOf(key, 'string');
|
|
|
|
assert.equal(key, obj.key);
|
|
|
|
|
|
|
|
yield obj.erase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false after a save failure", function* () {
|
|
|
|
for (let type of types) {
|
|
|
|
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
|
|
|
|
var obj;
|
|
|
|
try {
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
|
|
|
obj = createUnsavedDataObject(type);
|
|
|
|
yield obj.save();
|
|
|
|
throw 'Aborting transaction -- ignore';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The registered identifiers should be reset in a rollback handler
|
|
|
|
var libraryKey = objectsClass.getLibraryAndKeyFromID(obj.id);
|
|
|
|
assert.isFalse(libraryKey);
|
|
|
|
}
|
|
|
|
});
|
2015-06-16 23:50:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("#exists()", function () {
|
|
|
|
it("should return false after object is deleted", function* () {
|
|
|
|
for (let type of types) {
|
|
|
|
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
|
|
|
|
let obj = yield createDataObject(type);
|
|
|
|
let id = obj.id;
|
|
|
|
yield obj.eraseTx();
|
|
|
|
assert.isFalse(objectsClass.exists(id), type + " does not exist");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2015-05-30 23:07:12 +00:00
|
|
|
|
|
|
|
describe("#_setIdentifier", function () {
|
|
|
|
it("should not allow an id change", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
try {
|
|
|
|
item.id = item.id + 1;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
assert.equal(e.message, "ID cannot be changed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.fail("ID change allowed");
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not allow a key change", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
try {
|
|
|
|
item.key = Zotero.DataObjectUtilities.generateKey();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
assert.equal(e.message, "Key cannot be changed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.fail("Key change allowed");
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not allow key to be set if id is set", function* () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
item.id = Zotero.Utilities.rand(100000, 1000000);
|
|
|
|
try {
|
|
|
|
item.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
item.key = Zotero.DataObjectUtilities.generateKey();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
assert.equal(e.message, "Cannot set key if id is already set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.fail("ID change allowed");
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not allow id to be set if key is set", function* () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
item.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
item.key = Zotero.DataObjectUtilities.generateKey();
|
|
|
|
try {
|
|
|
|
item.id = Zotero.Utilities.rand(100000, 1000000);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
assert.equal(e.message, "Cannot set id if key is already set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.fail("Key change allowed");
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not allow key to be set if library isn't set", function* () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
try {
|
|
|
|
item.key = Zotero.DataObjectUtilities.generateKey();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
assert.equal(e.message, "libraryID must be set before key");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.fail("libraryID change allowed");
|
|
|
|
})
|
|
|
|
})
|
2015-05-25 02:14:52 +00:00
|
|
|
})
|