# Path: reggata/tests/abstract_test_cases.py
# class AbstractTestCaseWithRepoAndSingleUOW(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
#         self.uow = self.repo.createUnitOfWork()
# 
#     def tearDown(self):
#         self.uow.close()
#         self.uow = None
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
# class AbstractTestCaseWithRepo(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
# 
#     def tearDown(self):
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
#     def getExistingItem(self, itemId):
#         try:
#             uow = self.repo.createUnitOfWork()
#             item = uow.executeCommand(GetExpungedItemCommand(itemId))
#             self.assertIsNotNone(item)
#         finally:
#             uow.close()
#         return item
# 
#     def getDataRef(self, url):
#         # In the case when this DataRef is a file, url should be a relative path
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef) \
#                     .filter(DataRef.url_raw==helpers.to_db_format(url)).first()
#             # NOTE: dataRef could be None
#         finally:
#             uow.close()
#         return dataRef
# 
# 
#     def getDataRefById(self, objId):
#         '''
#             Returns a DataRef object with given id or None, if DataRef object not found.
#         '''
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef).filter(DataRef.id==objId).first()
#         finally:
#             uow.close()
#         return dataRef
# 
#     
#     def getDataRefsFromDir(self, dirRelPath):
#         dataRefs = []
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRefs = uow._session.query(DataRef).filter(
#                 DataRef.url_raw.like(helpers.to_db_format(dirRelPath) + "/%")).all()
#         finally:
#             uow.close()
#         return dataRefs
# 
# 
#     def updateExistingItem(self, detachedItem, newSrcAbsPath, newDstRelPath, user_login):
#         item = None
#         try:
#             uow = self.repo.createUnitOfWork()
#             cmd = UpdateExistingItemCommand(detachedItem, newSrcAbsPath, newDstRelPath, user_login)
#             item = uow.executeCommand(cmd)
#         finally:
#             uow.close()
#         return item


from reggata.tests.abstract_test_cases import AbstractTestCaseWithRepoAndSingleUOW, \
    AbstractTestCaseWithRepo
import reggata
import reggata.tests.tests_context as context
import reggata.data.db_schema as db
import reggata.data.commands as cmds
import reggata.errors as err
import reggata.helpers as hlp
import os



class GetItemTest(AbstractTestCaseWithRepoAndSingleUOW):
    def test_getExistingItem(self):
        item = self.uow.executeCommand(cmds.GetExpungedItemCommand(context.itemWithFile.id))
        self.assertEqual(item.title, context.itemWithFile.title)

    def test_getNonExistingItem(self):
        cmd = cmds.GetExpungedItemCommand(context.nonExistingItem.id)
        self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))

    def test_passBadIdToGetItem(self):
        cmd = cmds.GetExpungedItemCommand("This str is NOT a valid item id!")
        self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))



# Path: reggata/tests/abstract_test_cases.py
# class AbstractTestCaseWithRepoAndSingleUOW(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
#         self.uow = self.repo.createUnitOfWork()
# 
#     def tearDown(self):
#         self.uow.close()
#         self.uow = None
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
# class AbstractTestCaseWithRepo(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
# 
#     def tearDown(self):
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
#     def getExistingItem(self, itemId):
#         try:
#             uow = self.repo.createUnitOfWork()
#             item = uow.executeCommand(GetExpungedItemCommand(itemId))
#             self.assertIsNotNone(item)
#         finally:
#             uow.close()
#         return item
# 
#     def getDataRef(self, url):
#         # In the case when this DataRef is a file, url should be a relative path
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef) \
#                     .filter(DataRef.url_raw==helpers.to_db_format(url)).first()
#             # NOTE: dataRef could be None
#         finally:
#             uow.close()
#         return dataRef
# 
# 
#     def getDataRefById(self, objId):
#         '''
#             Returns a DataRef object with given id or None, if DataRef object not found.
#         '''
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef).filter(DataRef.id==objId).first()
#         finally:
#             uow.close()
#         return dataRef
# 
#     
#     def getDataRefsFromDir(self, dirRelPath):
#         dataRefs = []
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRefs = uow._session.query(DataRef).filter(
#                 DataRef.url_raw.like(helpers.to_db_format(dirRelPath) + "/%")).all()
#         finally:
#             uow.close()
#         return dataRefs
# 
# 
#     def updateExistingItem(self, detachedItem, newSrcAbsPath, newDstRelPath, user_login):
#         item = None
#         try:
#             uow = self.repo.createUnitOfWork()
#             cmd = UpdateExistingItemCommand(detachedItem, newSrcAbsPath, newDstRelPath, user_login)
#             item = uow.executeCommand(cmd)
#         finally:
#             uow.close()
#         return item


# Path: reggata/tests/test_repo_mgr.py
from reggata.tests.abstract_test_cases import AbstractTestCaseWithRepoAndSingleUOW, \
    AbstractTestCaseWithRepo
import reggata
import reggata.tests.tests_context as context
import reggata.data.db_schema as db
import reggata.data.commands as cmds
import reggata.errors as err
import reggata.helpers as hlp
import os




class GetItemTest(AbstractTestCaseWithRepoAndSingleUOW):
    def test_getExistingItem(self):
        item = self.uow.executeCommand(cmds.GetExpungedItemCommand(context.itemWithFile.id))
        self.assertEqual(item.title, context.itemWithFile.title)

    def test_getNonExistingItem(self):
        cmd = cmds.GetExpungedItemCommand(context.nonExistingItem.id)
        self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))

    def test_passBadIdToGetItem(self):
        cmd = cmds.GetExpungedItemCommand("This str is NOT a valid item id!")
        self.assertRaises(err.NotFoundError, self.uow.executeCommand, (cmd))



class SaveNewItemTest(AbstractTestCaseWithRepo):