__init__.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import unittest
  2. import transaction
  3. from pyramid import testing
  4. from tracim.models.data import Workspace
  5. from tracim.models.data import Content
  6. from tracim.logger import logger
  7. from tracim.fixtures import FixturesLoader
  8. from tracim.fixtures.users_and_groups import Base as BaseFixture
  9. from tracim.config import CFG
  10. class BaseTest(unittest.TestCase):
  11. """
  12. Pyramid default test.
  13. """
  14. def setUp(self):
  15. logger.debug(self, 'Setup Test...')
  16. self.config = testing.setUp(settings={
  17. 'sqlalchemy.url': 'sqlite:///:memory:',
  18. 'user.auth_token.validity':'604800',
  19. 'depot_storage_dir': '/tmp/test/depot',
  20. 'depot_storage_name': 'test',
  21. 'preview_cache_dir': '/tmp/test/preview_cache',
  22. })
  23. self.config.include('tracim.models')
  24. settings = self.config.get_settings()
  25. from tracim.models import (
  26. get_engine,
  27. get_session_factory,
  28. get_tm_session,
  29. )
  30. self.engine = get_engine(settings)
  31. session_factory = get_session_factory(self.engine)
  32. self.session = get_tm_session(session_factory, transaction.manager)
  33. self.init_database()
  34. def init_database(self):
  35. logger.debug(self, 'Init Database Schema...')
  36. from tracim.models.meta import DeclarativeBase
  37. DeclarativeBase.metadata.create_all(self.engine)
  38. def tearDown(self):
  39. logger.debug(self, 'TearDown Test...')
  40. from tracim.models.meta import DeclarativeBase
  41. testing.tearDown()
  42. transaction.abort()
  43. DeclarativeBase.metadata.drop_all(self.engine)
  44. # class DefaultTest(object):
  45. #
  46. # def _create_workspace_and_test(self, name, user) -> Workspace:
  47. # """
  48. # All extra parameters (*args, **kwargs) are for Workspace init
  49. # :return: Created workspace instance
  50. # """
  51. # WorkspaceApi(user).create_workspace(name, save_now=True)
  52. #
  53. # eq_(1, self._session.query(Workspace).filter(Workspace.label == name).count())
  54. # return self._session.query(Workspace).filter(Workspace.label == name).one()
  55. #
  56. # def _create_content_and_test(self, name, workspace, *args, **kwargs) -> Content:
  57. # """
  58. # All extra parameters (*args, **kwargs) are for Content init
  59. # :return: Created Content instance
  60. # """
  61. # content = Content(*args, **kwargs)
  62. # content.label = name
  63. # content.workspace = workspace
  64. # self._session.add(content)
  65. # self._session.flush()
  66. #
  67. # eq_(1, ContentApi.get_canonical_query().filter(Content.label == name).count())
  68. # return ContentApi.get_canonical_query().filter(Content.label == name).one()
  69. class StandardTest(BaseTest):
  70. """
  71. BaseTest with default fixtures
  72. """
  73. fixtures = [BaseFixture]
  74. def init_database(self):
  75. BaseTest.init_database(self)
  76. fixtures_loader = FixturesLoader(
  77. session=self.session,
  78. config=CFG(self.config.get_settings()))
  79. fixtures_loader.loads(self.fixtures)