def test_deleteExistingItemWithExistingPhysicalFileByOwner(self): userThatDeletesItem = context.itemWithFile.ownerUserLogin try: uow = self.repo.createUnitOfWork() _itemBeforeDelete = uow.executeCommand( cmds.GetExpungedItemCommand(context.itemWithFile.id)) cmd = cmds.DeleteItemCommand(context.itemWithFile.id, userThatDeletesItem, delete_physical_file=True) uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() deletedItem = uow.executeCommand( cmds.GetExpungedItemCommand(context.itemWithFile.id)) self.assertFalse(deletedItem.alive) self.assertIsNone(deletedItem.data_ref) #NOTE that tags/fields collections are not cleared, as you might expect self.assertTrue(len(deletedItem.item_tags) >= 0) self.assertTrue(len(deletedItem.item_fields) >= 0) finally: uow.close() self.assertFalse( os.path.exists( os.path.join(context.COPY_OF_TEST_REPO_BASE_PATH, context.itemWithFile.relFilePath)))
def test_saveNewItemWithFileOutsideRepo(self): ''' User wants to add an external file into the repo. File is copied to the repo. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "..", "tmp", "file.txt")) self.dstRelPath = os.path.join("dir1", "dir2", "dir3", "newFile.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) self.assertTrue(os.path.exists(self.srcAbsPath)) finally: uow.close()
def test_saveNewItemWithFileInsideRepo(self): ''' There is an untracked file inside the repo tree. User wants to add such file into the repo to make it a stored file. File is not copied, because it is alredy in the repo tree. It's essential that srcAbsPath and dstRelPath point to the same file. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "this", "is", "untracked", "file.txt")) self.dstRelPath = os.path.join("this", "is", "untracked", "file.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) finally: uow.close()
def test_saveNewItemWithACopyOfAStoredFileInRepo(self): ''' User wants to add a copy of a stored file from the repo into the same repo but to the another location. The copy of the original file will be attached to the new Item object. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "lyrics", "led_zeppelin", "stairway_to_heaven.txt")) self.dstRelPath = os.path.join("dir1", "dir2", "dir3", "copy_of_stairway_to_heaven.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) self.assertTrue(os.path.exists(self.srcAbsPath)) finally: uow.close()
def __editManyItems(self, itemIds): updated, skipped = 0, 0 uow = self._tool.repo.createUnitOfWork() try: items = [] for itemId in itemIds: items.append( uow.executeCommand(cmds.GetExpungedItemCommand(itemId))) if not self._dialogs.execItemsDialog(items, self._tool.gui, self._tool.repo, ItemsDialog.EDIT_MODE, sameDstPath=False): return updated, skipped thread = UpdateGroupOfItemsThread(self._tool.gui, self._tool.repo, items) self._dialogs.startThreadWithWaitDialog(thread, self._tool.gui, indeterminate=False) updated = thread.updatedCount skipped = thread.skippedCount finally: uow.close() return updated, skipped
def __getItemById(self, itemId): item = None uow = self.repo.createUnitOfWork() try: item = uow.executeCommand(cmds.GetExpungedItemCommand(itemId)) finally: uow.close() assert item is not None return item
def test_saveNewMinimalItem(self): # "Minimal item" here means that this item has no data references, tags or fields item = db.Item("user", "Minimal Item") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) finally: uow.close()
def doWork(self): self.filesExportedCount = 0 self.filesSkippedCount = 0 uow = self.repo.createUnitOfWork() try: i = 0 for itemId in self.item_ids: item = uow.executeCommand(cmds.GetExpungedItemCommand(itemId)) if not item.hasDataRef(): continue src_file_path = os.path.join(self.repo.base_path, item.data_ref.url) if not os.path.exists(src_file_path): logger.info( "Skipping file='{}' during export files operation: file not found." .format(src_file_path)) self.filesSkippedCount += 1 continue unique_path = dst_file_path = os.path.join( self.dst_path, os.path.basename(src_file_path)) filename_suffix = 1 #Generate unique file name. I don't want different files with same name to overwrite each other while os.path.exists(unique_path): name, ext = os.path.splitext(dst_file_path) unique_path = name + str(filename_suffix) + ext filename_suffix += 1 shutil.copy(src_file_path, unique_path) self.filesExportedCount += 1 i += 1 self.emit(QtCore.SIGNAL("progress"), int(100.0 * float(i) / len(self.item_ids))) finally: uow.close()
def __editSingleItem(self, itemId): updated, skipped = 0, 0 uow = self._tool.repo.createUnitOfWork() try: item = uow.executeCommand(cmds.GetExpungedItemCommand(itemId)) if not self._dialogs.execItemDialog( item=item, gui=self._tool.gui, repo=self._tool.repo, dialogMode=ItemDialog.EDIT_MODE): return updated, skipped srcAbsPath = item.data_ref.srcAbsPath if item.data_ref is not None else None dstRelPath = item.data_ref.dstRelPath if item.data_ref is not None else None cmd = cmds.UpdateExistingItemCommand(item, srcAbsPath, dstRelPath, self._tool.user.login) uow.executeCommand(cmd) updated += 1 except: skipped += 1 finally: uow.close() return updated, skipped
def test_passBadIdToGetItem(self): cmd = cmds.GetExpungedItemCommand("This str is NOT a valid item id!") self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))
def test_getNonExistingItem(self): cmd = cmds.GetExpungedItemCommand(context.nonExistingItem.id) self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))
def test_getExistingItem(self): item = self.uow.executeCommand( cmds.GetExpungedItemCommand(context.itemWithFile.id)) self.assertEqual(item.title, context.itemWithFile.title)