www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

dataObjectsTest.js (4108B)


      1 "use strict";
      2 
      3 describe("Zotero.DataObjects", function () {
      4 	var types = ['collection', 'item', 'search'];
      5 	
      6 	describe("#get()", function () {
      7 		it("should return false for nonexistent objects", function* () {
      8 			assert.isFalse(Zotero.Items.get(3464363));
      9 		});
     10 	});
     11 	
     12 	describe("#getAsync()", function () {
     13 		// TEMP: Currently just a warning
     14 		it.skip("show throw if passed an invalid id", function* () {
     15 			var e = yield getPromiseError(Zotero.Items.getAsync("[Object]"));
     16 			assert.ok(e);
     17 			assert.include(e.message, '(string)');
     18 		});
     19 	});
     20 	
     21 	describe("#getLibraryAndKeyFromID()", function () {
     22 		it("should return a libraryID and key within a transaction", function* () {
     23 			for (let type of types) {
     24 				let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
     25 				yield Zotero.DB.executeTransaction(function* () {
     26 					let obj = createUnsavedDataObject(type);
     27 					yield obj.save();
     28 					
     29 					var {libraryID, key} = objectsClass.getLibraryAndKeyFromID(obj.id);
     30 					assert.equal(libraryID, Zotero.Libraries.userLibraryID);
     31 					assert.ok(key);
     32 					assert.typeOf(key, 'string');
     33 					assert.equal(key, obj.key);
     34 					
     35 					yield obj.erase();
     36 				});
     37 			}
     38 		});
     39 		
     40 		it("should return false after a save failure", function* () {
     41 			for (let type of types) {
     42 				let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
     43 				var obj;
     44 				try {
     45 					yield Zotero.DB.executeTransaction(function* () {
     46 						obj = createUnsavedDataObject(type);
     47 						yield obj.save();
     48 						throw 'Aborting transaction -- ignore';
     49 					});
     50 				}
     51 				catch (e) {
     52 					if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
     53 				}
     54 				
     55 				// The registered identifiers should be reset in a rollback handler
     56 				var libraryKey = objectsClass.getLibraryAndKeyFromID(obj.id);
     57 				assert.isFalse(libraryKey);
     58 			}
     59 		});
     60 	})
     61 	
     62 	describe("#exists()", function () {
     63 		it("should return false after object is deleted", function* () {
     64 			for (let type of types) {
     65 				let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
     66 				let obj = yield createDataObject(type);
     67 				let id = obj.id;
     68 				yield obj.eraseTx();
     69 				assert.isFalse(objectsClass.exists(id), type + " does not exist");
     70 			}
     71 		})
     72 	})
     73 	
     74 	describe("#_setIdentifier", function () {
     75 		it("should not allow an id change", function* () {
     76 			var item = yield createDataObject('item');
     77 			try {
     78 				item.id = item.id + 1;
     79 			}
     80 			catch (e) {
     81 				assert.equal(e.message, "ID cannot be changed");
     82 				return;
     83 			}
     84 			assert.fail("ID change allowed");
     85 		})
     86 		
     87 		it("should not allow a key change", function* () {
     88 			var item = yield createDataObject('item');
     89 			try {
     90 				item.key = Zotero.DataObjectUtilities.generateKey();
     91 			}
     92 			catch (e) {
     93 				assert.equal(e.message, "Key cannot be changed");
     94 				return;
     95 			}
     96 			assert.fail("Key change allowed");
     97 		})
     98 		
     99 		it("should not allow key to be set if id is set", function* () {
    100 			var item = createUnsavedDataObject('item');
    101 			item.id = Zotero.Utilities.rand(100000, 1000000);
    102 			try {
    103 				item.libraryID = Zotero.Libraries.userLibraryID;
    104 				item.key = Zotero.DataObjectUtilities.generateKey();
    105 			}
    106 			catch (e) {
    107 				assert.equal(e.message, "Cannot set key if id is already set");
    108 				return;
    109 			}
    110 			assert.fail("ID change allowed");
    111 		})
    112 		
    113 		it("should not allow id to be set if key is set", function* () {
    114 			var item = createUnsavedDataObject('item');
    115 			item.libraryID = Zotero.Libraries.userLibraryID;
    116 			item.key = Zotero.DataObjectUtilities.generateKey();
    117 			try {
    118 				item.id = Zotero.Utilities.rand(100000, 1000000);
    119 			}
    120 			catch (e) {
    121 				assert.equal(e.message, "Cannot set id if key is already set");
    122 				return;
    123 			}
    124 			assert.fail("Key change allowed");
    125 		})
    126 		
    127 		it("should not allow key to be set if library isn't set", function* () {
    128 			var item = createUnsavedDataObject('item');
    129 			try {
    130 				item.key = Zotero.DataObjectUtilities.generateKey();
    131 			}
    132 			catch (e) {
    133 				assert.equal(e.message, "libraryID must be set before key");
    134 				return;
    135 			}
    136 			assert.fail("libraryID change allowed");
    137 		})
    138 	})
    139 })