1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
import unittest |
3
|
|
|
import transaction |
4
|
|
|
from depot.manager import DepotManager |
5
|
|
|
from pyramid import testing |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
from tracim.lib.core.content import ContentApi |
9
|
|
|
from tracim.lib.core.workspace import WorkspaceApi |
10
|
|
|
from tracim.models.data import Workspace, ContentType |
11
|
|
|
from tracim.models.data import Content |
12
|
|
|
from tracim.lib.utils.logger import logger |
13
|
|
|
from tracim.fixtures import FixturesLoader |
14
|
|
|
from tracim.fixtures.users_and_groups import Base as BaseFixture |
15
|
|
|
from tracim.config import CFG |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def eq_(a, b, msg=None): |
|
|
|
|
19
|
|
|
# TODO - G.M - 05-04-2018 - Remove this when all old nose code is removed |
|
|
|
|
20
|
|
|
assert a == b, msg or "%r != %r" % (a, b) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class BaseTest(unittest.TestCase): |
|
|
|
|
24
|
|
|
""" |
25
|
|
|
Pyramid default test. |
26
|
|
|
""" |
27
|
|
|
def setUp(self): |
28
|
|
|
logger.debug(self, 'Setup Test...') |
29
|
|
|
self.config = testing.setUp(settings={ |
30
|
|
|
'sqlalchemy.url': 'sqlite:///:memory:', |
31
|
|
|
'user.auth_token.validity': '604800', |
32
|
|
|
'depot_storage_dir': '/tmp/test/depot', |
33
|
|
|
'depot_storage_name': 'test', |
34
|
|
|
'preview_cache_dir': '/tmp/test/preview_cache', |
35
|
|
|
|
36
|
|
|
}) |
37
|
|
|
self.config.include('tracim.models') |
38
|
|
|
DepotManager._clear() |
|
|
|
|
39
|
|
|
DepotManager.configure( |
40
|
|
|
'test', {'depot.backend': 'depot.io.memory.MemoryFileStorage'} |
41
|
|
|
) |
42
|
|
|
settings = self.config.get_settings() |
43
|
|
|
self.app_config = CFG(settings) |
44
|
|
|
from tracim.models import ( |
45
|
|
|
get_engine, |
46
|
|
|
get_session_factory, |
47
|
|
|
get_tm_session, |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
self.engine = get_engine(settings) |
51
|
|
|
session_factory = get_session_factory(self.engine) |
52
|
|
|
|
53
|
|
|
self.session = get_tm_session(session_factory, transaction.manager) |
54
|
|
|
self.init_database() |
55
|
|
|
|
56
|
|
|
def init_database(self): |
|
|
|
|
57
|
|
|
logger.debug(self, 'Init Database Schema...') |
58
|
|
|
from tracim.models.meta import DeclarativeBase |
59
|
|
|
DeclarativeBase.metadata.create_all(self.engine) |
60
|
|
|
|
61
|
|
|
def tearDown(self): |
62
|
|
|
logger.debug(self, 'TearDown Test...') |
63
|
|
|
from tracim.models.meta import DeclarativeBase |
64
|
|
|
|
65
|
|
|
testing.tearDown() |
66
|
|
|
transaction.abort() |
67
|
|
|
DeclarativeBase.metadata.drop_all(self.engine) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class StandardTest(BaseTest): |
|
|
|
|
71
|
|
|
""" |
72
|
|
|
BaseTest with default fixtures |
73
|
|
|
""" |
74
|
|
|
fixtures = [BaseFixture] |
75
|
|
|
|
76
|
|
|
def init_database(self): |
77
|
|
|
BaseTest.init_database(self) |
78
|
|
|
fixtures_loader = FixturesLoader( |
79
|
|
|
session=self.session, |
80
|
|
|
config=CFG(self.config.get_settings())) |
81
|
|
|
fixtures_loader.loads(self.fixtures) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class DefaultTest(StandardTest): |
|
|
|
|
85
|
|
|
|
86
|
|
|
def _create_workspace_and_test(self, name, user) -> Workspace: |
87
|
|
|
""" |
88
|
|
|
All extra parameters (*args, **kwargs) are for Workspace init |
89
|
|
|
:return: Created workspace instance |
90
|
|
|
""" |
91
|
|
|
WorkspaceApi( |
92
|
|
|
current_user=user, |
93
|
|
|
session=self.session, |
94
|
|
|
).create_workspace(name, save_now=True) |
95
|
|
|
|
96
|
|
|
eq_( |
97
|
|
|
1, |
98
|
|
|
self.session.query(Workspace).filter( |
99
|
|
|
Workspace.label == name |
100
|
|
|
).count() |
101
|
|
|
) |
102
|
|
|
return self.session.query(Workspace).filter( |
103
|
|
|
Workspace.label == name |
104
|
|
|
).one() |
105
|
|
|
|
106
|
|
|
def _create_content_and_test( |
107
|
|
|
self, |
108
|
|
|
name, |
109
|
|
|
workspace, |
110
|
|
|
*args, |
111
|
|
|
**kwargs |
112
|
|
|
) -> Content: |
113
|
|
|
""" |
114
|
|
|
All extra parameters (*args, **kwargs) are for Content init |
115
|
|
|
:return: Created Content instance |
116
|
|
|
""" |
117
|
|
|
content = Content(*args, **kwargs) |
118
|
|
|
content.label = name |
119
|
|
|
content.workspace = workspace |
120
|
|
|
self.session.add(content) |
121
|
|
|
self.session.flush() |
122
|
|
|
|
123
|
|
|
content_api = ContentApi( |
124
|
|
|
current_user=None, |
125
|
|
|
session=self.session, |
126
|
|
|
config=self.app_config, |
127
|
|
|
) |
128
|
|
|
eq_( |
129
|
|
|
1, |
130
|
|
|
content_api.get_canonical_query().filter( |
131
|
|
|
Content.label == name |
132
|
|
|
).count() |
133
|
|
|
) |
134
|
|
|
return content_api.get_canonical_query().filter( |
135
|
|
|
Content.label == name |
136
|
|
|
).one() |
137
|
|
|
|
138
|
|
|
def _create_thread_and_test(self, |
139
|
|
|
user, |
140
|
|
|
workspace_name='workspace_1', |
141
|
|
|
folder_name='folder_1', |
142
|
|
|
thread_name='thread_1') -> Content: |
143
|
|
|
""" |
144
|
|
|
:return: Thread |
145
|
|
|
""" |
146
|
|
|
workspace = self._create_workspace_and_test(workspace_name, user) |
147
|
|
|
folder = self._create_content_and_test( |
148
|
|
|
folder_name, workspace, |
149
|
|
|
type=ContentType.Folder, |
150
|
|
|
owner=user |
151
|
|
|
) |
152
|
|
|
thread = self._create_content_and_test( |
153
|
|
|
thread_name, |
154
|
|
|
workspace, |
155
|
|
|
type=ContentType.Thread, |
156
|
|
|
parent=folder, |
157
|
|
|
owner=user |
158
|
|
|
) |
159
|
|
|
return thread |
160
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.