libraryTest.js (14332B)
1 describe("Zotero.Library", function() { 2 describe("#constructor()", function() { 3 it("should allow no arguments", function() { 4 assert.doesNotThrow(() => new Zotero.Library()); 5 }); 6 }); 7 8 describe("#libraryID", function() { 9 it("should not allow setting a library ID", function() { 10 let library = new Zotero.Library(); 11 assert.throws(() => library.libraryID = 1); 12 }); 13 it("should return a library ID for a saved library", function() { 14 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 15 assert.isAbove(library.libraryID, 0); 16 }) 17 }); 18 19 describe("#libraryType", function() { 20 it("should not allow creating a non-basic library", function() { 21 let library = new Zotero.Library(); 22 assert.throws(() => library.libraryType = 'group', /^Invalid library type /); 23 24 }); 25 it("should not allow setting a library type for a saved library", function* () { 26 let library = yield createGroup(); 27 assert.throws(() => library.libraryType = 'feed'); 28 }); 29 it("should not allow creating new unique libraries", function* () { 30 for (let i=0; i<Zotero.Library.prototype.fixedLibraries.length; i++) { 31 let libraryType = Zotero.Library.prototype.fixedLibraries[i]; 32 assert.throws(function() {new Zotero.Library({ libraryType })}, /^Cannot create library of type /, 'cannot create a new ' + libraryType + ' library'); 33 } 34 }); 35 }); 36 37 describe("#libraryTypeID", function () { 38 it("should return a group id for a group", function* () { 39 let library = yield createGroup(); 40 assert.typeOf(library.libraryTypeID, 'number'); 41 assert.equal(library.libraryTypeID, library.groupID); 42 }) 43 }) 44 45 describe("#libraryVersion", function() { 46 it("should be settable to increasing values", function() { 47 let library = new Zotero.Library(); 48 assert.throws(() => library.libraryVersion = -2); 49 assert.throws(() => library.libraryVersion = "a"); 50 assert.throws(() => library.libraryVersion = 1.1); 51 assert.doesNotThrow(() => library.libraryVersion = 0); 52 assert.doesNotThrow(() => library.libraryVersion = 5); 53 }); 54 it("should not be possible to decrement", function() { 55 let library = new Zotero.Library(); 56 library.libraryVersion = 5; 57 assert.throws(() => library.libraryVersion = 0); 58 }); 59 it("should be possible to set to -1", function() { 60 let library = new Zotero.Library(); 61 library.libraryVersion = 5; 62 assert.doesNotThrow(() => library.libraryVersion = -1); 63 }); 64 }); 65 66 describe("#editable", function() { 67 it("should return editable status", function() { 68 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 69 assert.isTrue(library.editable, 'user library is editable'); 70 }); 71 it("should allow setting editable status", function* () { 72 let library = yield createGroup({ editable: true }); 73 74 assert.isTrue(library.editable); 75 assert.isTrue(Zotero.Libraries.isEditable(library.libraryID), "sets editable in cache to true"); 76 assert.equal((yield Zotero.DB.valueQueryAsync("SELECT editable FROM libraries WHERE libraryID=?", library.libraryID)), 1) 77 78 library.editable = false; 79 yield library.saveTx(); 80 assert.isFalse(library.editable); 81 assert.isFalse(Zotero.Libraries.isEditable(library.libraryID), "sets editable in cache to false"); 82 assert.equal((yield Zotero.DB.valueQueryAsync("SELECT editable FROM libraries WHERE libraryID=?", library.libraryID)), 0) 83 }); 84 85 it("should also set filesEditable to false", function* () { 86 let library = yield createGroup({ editable: true, filesEditable: true }); 87 assert.isTrue(library.filesEditable); 88 89 library.editable = false; 90 yield library.saveTx(); 91 assert.isFalse(library.filesEditable); 92 assert.equal((yield Zotero.DB.valueQueryAsync("SELECT filesEditable FROM libraries WHERE libraryID=?", library.libraryID)), 0) 93 }); 94 95 it("should not be settable for user libraries", function* () { 96 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 97 assert.throws(function() {library.editable = false}, /^Cannot change _libraryEditable for user library$/, "does not allow setting user library as not editable"); 98 }); 99 }); 100 101 describe("#filesEditable", function() { 102 it("should always return true for user library", function() { 103 assert.isTrue(Zotero.Libraries.userLibrary.filesEditable); 104 }); 105 106 it("should return files editable status", function() { 107 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 108 assert.isTrue(library.filesEditable, 'user library is files editable'); 109 }); 110 111 it("should allow setting files editable status", function* () { 112 let library = yield createGroup({ filesEditable: true }); 113 114 assert.isTrue(library.filesEditable); 115 assert.isTrue(Zotero.Libraries.isFilesEditable(library.libraryID), "sets files editable in cache to true"); 116 117 library.filesEditable = false; 118 yield library.saveTx(); 119 assert.isFalse(library.filesEditable); 120 assert.isFalse(Zotero.Libraries.isFilesEditable(library.libraryID), "sets files editable in cache to false"); 121 }); 122 123 it("should not be settable for user libraries", function* () { 124 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 125 assert.throws(function() {library.filesEditable = false}, /^Cannot change _libraryFilesEditable for user library$/, "does not allow setting user library as not files editable"); 126 }); 127 }); 128 129 describe("#allowsLinkedFiles", function () { 130 it("should return true for personal library", function () { 131 assert.isTrue(Zotero.Libraries.userLibrary.allowsLinkedFiles); 132 }); 133 134 it("should return false for group libraries", async function () { 135 var group = await getGroup(); 136 assert.isFalse(group.allowsLinkedFiles); 137 }); 138 }); 139 140 describe("#archived", function() { 141 it("should return archived status", function() { 142 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 143 assert.isFalse(library.archived, 'user library is not archived'); 144 }); 145 146 it("should allow setting archived status", function* () { 147 let library = yield createGroup({ editable: false, archived: true }); 148 assert.isTrue(library.archived); 149 assert.equal((yield Zotero.DB.valueQueryAsync("SELECT archived FROM libraries WHERE libraryID=?", library.libraryID)), 1) 150 151 library.archived = false; 152 yield library.saveTx(); 153 assert.isFalse(library.archived); 154 assert.equal((yield Zotero.DB.valueQueryAsync("SELECT archived FROM libraries WHERE libraryID=?", library.libraryID)), 0) 155 }); 156 157 it("should not be settable for user libraries", function* () { 158 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 159 assert.throws(() => library.archived = true, /^Cannot change _libraryArchived for user library$/, "does not allow setting user library as archived"); 160 }); 161 162 it("should only be settable on read-only library", function* () { 163 let library = yield createGroup(); 164 assert.throws(() => library.archived = true, /^Cannot set editable library as archived$/); 165 }); 166 }); 167 168 describe("#save()", function() { 169 it("should require mandatory parameters to be set", function* () { 170 let library = new Zotero.Library({ editable: true, filesEditable: true }); 171 yield assert.isRejected(library.saveTx(), /^libraryType must be set before saving/, 'libraryType is mandatory'); 172 173 // Required group params 174 let groupID = Zotero.Utilities.rand(1000, 10000); 175 let name = 'foo'; 176 let description = ''; 177 let version = Zotero.Utilities.rand(1000, 10000); 178 library = new Zotero.Group({ filesEditable: true, groupID, name , description, version }); 179 yield assert.isRejected(library.saveTx(), /^editable must be set before saving/, 'editable is mandatory'); 180 181 library = new Zotero.Group({ editable: true, groupID, name , description, version }); 182 yield assert.isRejected(library.saveTx(), /^filesEditable must be set before saving/, 'filesEditable is mandatory'); 183 184 library = new Zotero.Group({ editable: true, filesEditable: true, groupID, name , description, version }); 185 yield assert.isFulfilled(library.saveTx()); 186 }); 187 it("should save new library to DB", function* () { 188 let library = yield createGroup({}); 189 190 assert.isAbove(library.libraryID, 0, "sets a libraryID"); 191 assert.isTrue(Zotero.Libraries.exists(library.libraryID)); 192 assert.equal(library.libraryType, 'group'); 193 194 let inDB = yield Zotero.DB.valueQueryAsync('SELECT COUNT(*) FROM libraries WHERE libraryID=?', library.libraryID); 195 assert.ok(inDB, 'added to DB'); 196 }); 197 it("should save library changes to DB", function* () { 198 let library = yield createGroup({ editable: true }); 199 200 library.editable = false; 201 yield library.saveTx(); 202 assert.isFalse(Zotero.Libraries.isEditable(library.libraryID)); 203 }); 204 205 it("should initialize library after creation", function* () { 206 let library = yield createGroup({}); 207 Zotero.SyncedSettings.get(library.libraryID, "tagColors"); 208 }); 209 }); 210 describe("#erase()", function() { 211 it("should erase a group library", function* () { 212 let library = yield createGroup(); 213 214 let libraryID = library.libraryID; 215 yield library.eraseTx(); 216 217 assert.isFalse(Zotero.Libraries.exists(libraryID), "library no longer exists in cache");assert.isFalse(Zotero.Libraries.exists(libraryID)); 218 219 let inDB = yield Zotero.DB.valueQueryAsync('SELECT COUNT(*) FROM libraries WHERE libraryID=?', libraryID); 220 assert.notOk(inDB, 'removed from DB'); 221 }); 222 223 it("should erase a read-only library", function* () { 224 let library = yield createGroup({ editable:false, filesEditable:false }); 225 226 yield assert.isFulfilled(library.eraseTx()); 227 }); 228 229 it("should not allow erasing permanent libraries", function* () { 230 let library = Zotero.Libraries.get(Zotero.Libraries.userLibraryID); 231 yield assert.isRejected(library.eraseTx(), /^Cannot erase library of type 'user'$/, "does not allow erasing user library"); 232 }); 233 234 it("should not allow erasing unsaved libraries", function* () { 235 let library = new Zotero.Library(); 236 yield assert.isRejected(library.eraseTx()); 237 }); 238 it("should throw when accessing erased library methods, except for #libraryID", function* () { 239 let library = yield createGroup(); 240 yield library.eraseTx(); 241 242 assert.doesNotThrow(() => library.libraryID); 243 assert.throws(() => library.name, /^Group \(\d+\) has been disabled$/); 244 assert.throws(() => library.editable = false, /^Group \(\d+\) has been disabled$/); 245 }); 246 it("should clear child items from caches and DB", function* () { 247 let group = yield createGroup(); 248 let libraryID = group.libraryID; 249 250 let collection = yield createDataObject('collection', { libraryID }); 251 assert.ok(yield Zotero.Collections.getAsync(collection.id)); 252 253 let item = yield createDataObject('item', { libraryID }); 254 assert.ok(yield Zotero.Items.getAsync(item.id)); 255 256 let search = yield createDataObject('search', { libraryID }); 257 assert.ok(yield Zotero.Searches.getAsync(search.id)); 258 259 yield group.eraseTx(); 260 261 assert.notOk((yield Zotero.Searches.getAsync(search.id)), 'search was unloaded'); 262 assert.notOk((yield Zotero.Collections.getAsync(collection.id)), 'collection was unloaded'); 263 assert.notOk((yield Zotero.Items.getAsync(item.id)), 'item was unloaded'); 264 }); 265 }); 266 describe("#hasCollections()", function() { 267 it("should throw if called before saving a library", function() { 268 let library = new Zotero.Library(); 269 assert.throws(() => library.hasCollections()); 270 }); 271 it("should stay up to date as collections are added and removed", function* () { 272 let library = yield createGroup({ editable: true }); 273 let libraryID = library.libraryID; 274 assert.isFalse(library.hasCollections()); 275 276 let c1 = yield createDataObject('collection', { libraryID }); 277 assert.isTrue(library.hasCollections()); 278 279 let c2 = yield createDataObject('collection', { libraryID }); 280 assert.isTrue(library.hasCollections()); 281 282 yield c1.eraseTx(); 283 assert.isTrue(library.hasCollections()); 284 285 yield c2.eraseTx(); 286 assert.isFalse(library.hasCollections()); 287 }) 288 }); 289 describe("#hasSearches()", function() { 290 it("should throw if called before saving a library", function() { 291 let library = new Zotero.Library(); 292 assert.throws(() => library.hasSearches()); 293 }); 294 it("should stay up to date as searches are added and removed", function* () { 295 let library = yield createGroup({ editable: true }); 296 let libraryID = library.libraryID; 297 assert.isFalse(library.hasSearches()); 298 299 let s1 = yield createDataObject('search', { libraryID }); 300 assert.isTrue(library.hasSearches()); 301 302 let s2 = yield createDataObject('search', { libraryID }); 303 assert.isTrue(library.hasSearches()); 304 305 yield s1.eraseTx(); 306 assert.isTrue(library.hasSearches()); 307 308 yield s2.eraseTx(); 309 assert.isFalse(library.hasSearches()); 310 }) 311 }); 312 describe("#hasItems()", function() { 313 it("should throw if called before saving a library", function* () { 314 let library = new Zotero.Library(); 315 try { 316 yield library.hasItems(); 317 assert.isFalse(true, "Library#hasItems did not throw an error"); 318 } catch (e) { 319 assert.ok(e); 320 } 321 }); 322 it("should stay up to date as items are added and removed", function* () { 323 let library = yield createGroup({ editable: true }); 324 let libraryID = library.libraryID; 325 var hasItems = yield library.hasItems(); 326 assert.isFalse(hasItems); 327 328 let i1 = yield createDataObject('item', { libraryID }); 329 hasItems = yield library.hasItems(); 330 assert.isTrue(hasItems); 331 332 let i2 = yield createDataObject('item', { libraryID }); 333 hasItems = yield library.hasItems(); 334 assert.isTrue(hasItems); 335 336 yield i1.eraseTx(); 337 hasItems = yield library.hasItems(); 338 assert.isTrue(hasItems); 339 340 yield i2.eraseTx(); 341 hasItems = yield library.hasItems(); 342 assert.isFalse(hasItems); 343 }); 344 }); 345 describe("#updateLastSyncTime()", function() { 346 it("should set sync time to current time", function* () { 347 let group = yield createGroup(); 348 assert.isFalse(group.lastSync); 349 350 group.updateLastSyncTime(); 351 assert.ok(group.lastSync); 352 assert.closeTo(Date.now(), group.lastSync.getTime(), 1000); 353 354 yield group.saveTx(); 355 356 let dbTime = yield Zotero.DB.valueQueryAsync('SELECT lastSync FROM libraries WHERE libraryID=?', group.libraryID); 357 assert.equal(dbTime*1000, group.lastSync.getTime()); 358 }) 359 }); 360 })