commit db2348f10025c80d0e6b0d7919fc239c88195716
parent 8ff258fe03852cf358b2f54aa54db50103fd2347
Author: Dan Stillman <dstillman@zotero.org>
Date: Sat, 25 Apr 2015 03:14:53 -0400
Tests for data layer and item fixes/changes
Diffstat:
2 files changed, 80 insertions(+), 0 deletions(-)
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -0,0 +1,44 @@
+describe("Zotero.Item", function() {
+ describe("#getField()", function () {
+ it("should return false for valid unset fields on unsaved items", function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ assert.equal(item.getField('rights'), false);
+ });
+
+ it("should return false for valid unset fields on unsaved items after setting on another field", function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ item.setField('title', 'foo');
+ assert.equal(item.getField('rights'), false);
+ });
+
+ it("should return false for invalid unset fields on unsaved items after setting on another field", function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ item.setField('title', 'foo');
+ assert.equal(item.getField('invalid'), false);
+ });
+ });
+
+ describe("#parentID", function () {
+ it("should create a child note", function () {
+ return Zotero.DB.executeTransaction(function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ var parentItemID = yield item.save();
+
+ item = new Zotero.Item('note');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ item.parentID = parentItemID;
+ var childItemID = yield item.save();
+
+ item = yield Zotero.Items.getAsync(childItemID);
+ Zotero.debug('=-=-=');
+ Zotero.debug(item.parentID);
+ assert.ok(item.parentID);
+ assert.equal(item.parentID, parentItemID);
+ }.bind(this));
+ });
+ });
+});
diff --git a/test/tests/itemsTest.js b/test/tests/itemsTest.js
@@ -0,0 +1,36 @@
+describe("Zotero.Items", function() {
+ describe("#getLibraryAndKeyFromID()", function () {
+ it("should return a libraryID and key within a transaction", function* () {
+ return Zotero.DB.executeTransaction(function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ var itemID = yield item.save();
+
+ var {libraryID, key} = Zotero.Items.getLibraryAndKeyFromID(itemID);
+ assert.equal(libraryID, Zotero.Libraries.userLibraryID);
+ assert.ok(key);
+ assert.typeOf(key, 'string');
+ assert.equal(key, item.key);
+ });
+ });
+
+ it("should return false after a save failure", function* () {
+ var itemID;
+ try {
+ yield Zotero.DB.executeTransaction(function* () {
+ var item = new Zotero.Item('book');
+ item.libraryID = Zotero.Libraries.userLibraryID;
+ itemID = yield item.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 = Zotero.Items.getLibraryAndKeyFromID(itemID);
+ assert.isFalse(libraryKey);
+ });
+ });
+});