|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
import time |
|
3
|
|
|
|
|
4
|
|
|
from depot.fields.upload import UploadedFile |
|
5
|
|
|
from sqlalchemy.sql.elements import and_ |
|
6
|
|
|
from sqlalchemy.testing import eq_ |
|
7
|
|
|
import transaction |
|
8
|
|
|
import pytest |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
# from tracim.lib.content import ContentApi |
|
11
|
|
|
from tracim.exceptions import ContentRevisionUpdateError |
|
12
|
|
|
from tracim.lib.core.content import ContentApi |
|
13
|
|
|
from tracim.models import Content |
|
14
|
|
|
from tracim.models.revision_protection import new_revision |
|
15
|
|
|
from tracim.models import User |
|
16
|
|
|
from tracim.models.data import ActionDescription |
|
17
|
|
|
from tracim.models.data import ContentRevisionRO |
|
18
|
|
|
from tracim.models.data import ContentType |
|
19
|
|
|
from tracim.models.data import Workspace |
|
20
|
|
|
from tracim.tests import StandardTest |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class TestContent(StandardTest): |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_update_without_prepare(self): |
|
|
|
|
|
|
26
|
|
|
content1 = self.test_create() |
|
27
|
|
|
with pytest.raises(ContentRevisionUpdateError): |
|
28
|
|
|
content1.description = 'FOO' |
|
29
|
|
|
# Raise ContentRevisionUpdateError because revision can't be updated |
|
30
|
|
|
|
|
31
|
|
|
def test_query(self): |
|
|
|
|
|
|
32
|
|
|
content1 = self.test_create() |
|
33
|
|
|
with new_revision( |
|
34
|
|
|
session=self.session, |
|
|
|
|
|
|
35
|
|
|
tm=transaction.manager, |
|
|
|
|
|
|
36
|
|
|
content=content1, |
|
|
|
|
|
|
37
|
|
|
): |
|
38
|
|
|
content1.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' |
|
39
|
|
|
self.session.flush() |
|
40
|
|
|
|
|
41
|
|
|
content2 = self.test_create(key='2') |
|
42
|
|
|
with new_revision( |
|
43
|
|
|
session=self.session, |
|
44
|
|
|
tm=transaction.manager, |
|
45
|
|
|
content=content2, |
|
46
|
|
|
): |
|
47
|
|
|
content2.description = 'TEST_CONTENT_DESCRIPTION_2_UPDATED' |
|
48
|
|
|
self.session.flush() |
|
49
|
|
|
|
|
50
|
|
|
workspace1 = self.session.query(Workspace)\ |
|
51
|
|
|
.filter(Workspace.label == 'TEST_WORKSPACE_1').one() |
|
52
|
|
|
workspace2 = self.session.query(Workspace)\ |
|
53
|
|
|
.filter(Workspace.label == 'TEST_WORKSPACE_2').one() |
|
54
|
|
|
|
|
55
|
|
|
# To get Content in database |
|
56
|
|
|
# we have to join Content and ContentRevisionRO |
|
57
|
|
|
# with particular condition: |
|
58
|
|
|
# Join have to be on most recent revision |
|
59
|
|
|
join_sub_query = self.session.query(ContentRevisionRO.revision_id)\ |
|
60
|
|
|
.filter(ContentRevisionRO.content_id == Content.id)\ |
|
61
|
|
|
.order_by(ContentRevisionRO.revision_id.desc())\ |
|
62
|
|
|
.limit(1)\ |
|
63
|
|
|
.correlate(Content) |
|
64
|
|
|
|
|
65
|
|
|
base_query = self.session.query(Content).join( |
|
66
|
|
|
ContentRevisionRO, |
|
67
|
|
|
and_( |
|
68
|
|
|
Content.id == ContentRevisionRO.content_id, |
|
69
|
|
|
ContentRevisionRO.revision_id == join_sub_query |
|
70
|
|
|
) |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
|
|
pattern = 'TEST_CONTENT_DESCRIPTION_%_UPDATED' |
|
74
|
|
|
eq_(2, base_query.filter(Content.description.like(pattern)).count()) |
|
75
|
|
|
|
|
76
|
|
|
eq_(1, base_query.filter(Content.workspace == workspace1).count()) |
|
77
|
|
|
eq_(1, base_query.filter(Content.workspace == workspace2).count()) |
|
78
|
|
|
|
|
79
|
|
|
content1_from_query = base_query\ |
|
80
|
|
|
.filter(Content.workspace == workspace1).one() |
|
81
|
|
|
eq_(content1.id, content1_from_query.id) |
|
82
|
|
|
eq_( |
|
83
|
|
|
'TEST_CONTENT_DESCRIPTION_1_UPDATED', |
|
84
|
|
|
content1_from_query.description |
|
85
|
|
|
) |
|
86
|
|
|
|
|
87
|
|
|
user_admin = self.session.query(User)\ |
|
|
|
|
|
|
88
|
|
|
.filter(User.email == '[email protected]').one() |
|
89
|
|
|
|
|
90
|
|
|
api = ContentApi( |
|
91
|
|
|
current_user=None, |
|
92
|
|
|
session=self.session, |
|
93
|
|
|
config=self.app_config, |
|
94
|
|
|
) |
|
95
|
|
|
|
|
96
|
|
|
content1_from_api = api.get_one( |
|
|
|
|
|
|
97
|
|
|
content1.id, |
|
98
|
|
|
ContentType.Page, |
|
99
|
|
|
workspace1 |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
def test_update(self): |
|
|
|
|
|
|
103
|
|
|
created_content = self.test_create() |
|
104
|
|
|
content = self.session.query(Content)\ |
|
105
|
|
|
.filter(Content.id == created_content.id).one() |
|
106
|
|
|
eq_(1, self.session.query(ContentRevisionRO) |
|
107
|
|
|
.filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count()) |
|
108
|
|
|
|
|
109
|
|
|
with new_revision( |
|
110
|
|
|
session=self.session, |
|
|
|
|
|
|
111
|
|
|
tm=transaction.manager, |
|
|
|
|
|
|
112
|
|
|
content=content |
|
|
|
|
|
|
113
|
|
|
): |
|
114
|
|
|
time.sleep(0.00001) |
|
115
|
|
|
content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' |
|
116
|
|
|
self.session.flush() |
|
117
|
|
|
|
|
118
|
|
|
eq_( |
|
119
|
|
|
2, |
|
120
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
121
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_1' |
|
122
|
|
|
).count() |
|
123
|
|
|
) |
|
124
|
|
|
eq_( |
|
125
|
|
|
1, |
|
126
|
|
|
self.session.query(Content).filter( |
|
127
|
|
|
Content.id == created_content.id |
|
128
|
|
|
).count() |
|
129
|
|
|
) |
|
130
|
|
|
|
|
131
|
|
|
with new_revision( |
|
132
|
|
|
session=self.session, |
|
|
|
|
|
|
133
|
|
|
tm=transaction.manager, |
|
|
|
|
|
|
134
|
|
|
content=content |
|
|
|
|
|
|
135
|
|
|
): |
|
136
|
|
|
time.sleep(0.00001) |
|
137
|
|
|
content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2' |
|
138
|
|
|
content.label = 'TEST_CONTENT_1_UPDATED_2' |
|
139
|
|
|
self.session.flush() |
|
140
|
|
|
|
|
141
|
|
|
eq_( |
|
142
|
|
|
1, |
|
143
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
144
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_1_UPDATED_2' |
|
145
|
|
|
).count() |
|
146
|
|
|
) |
|
147
|
|
|
eq_( |
|
148
|
|
|
1, |
|
149
|
|
|
self.session.query(Content).filter( |
|
150
|
|
|
Content.id == created_content.id |
|
151
|
|
|
).count() |
|
152
|
|
|
) |
|
153
|
|
|
|
|
154
|
|
|
revision_1 = self.session.query(ContentRevisionRO).filter( |
|
155
|
|
|
ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1' |
|
156
|
|
|
).one() |
|
157
|
|
|
revision_2 = self.session.query(ContentRevisionRO).filter( |
|
158
|
|
|
ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED' # nopep8 |
|
159
|
|
|
).one() |
|
160
|
|
|
revision_3 = self.session.query(ContentRevisionRO).filter( |
|
161
|
|
|
ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2' # nopep8 |
|
162
|
|
|
).one() |
|
163
|
|
|
|
|
164
|
|
|
# Updated dates must be different |
|
165
|
|
|
assert revision_1.updated < revision_2.updated < revision_3.updated |
|
166
|
|
|
# Created dates must be equal |
|
167
|
|
|
assert revision_1.created == revision_2.created == revision_3.created |
|
168
|
|
|
|
|
169
|
|
|
def test_creates(self): |
|
|
|
|
|
|
170
|
|
|
eq_( |
|
171
|
|
|
0, |
|
172
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
173
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_1' |
|
174
|
|
|
).count() |
|
175
|
|
|
) |
|
176
|
|
|
eq_( |
|
177
|
|
|
0, |
|
178
|
|
|
self.session.query(Workspace).filter( |
|
179
|
|
|
Workspace.label == 'TEST_WORKSPACE_1' |
|
180
|
|
|
).count() |
|
181
|
|
|
) |
|
182
|
|
|
|
|
183
|
|
|
user_admin = self.session.query(User).filter( |
|
184
|
|
|
User.email == '[email protected]' |
|
185
|
|
|
).one() |
|
186
|
|
|
workspace = Workspace(label="TEST_WORKSPACE_1") |
|
187
|
|
|
self.session.add(workspace) |
|
188
|
|
|
self.session.flush() |
|
189
|
|
|
eq_( |
|
190
|
|
|
1, |
|
191
|
|
|
self.session.query(Workspace).filter( |
|
192
|
|
|
Workspace.label == 'TEST_WORKSPACE_1' |
|
193
|
|
|
).count() |
|
194
|
|
|
) |
|
195
|
|
|
|
|
196
|
|
|
first_content = self._create_content( |
|
197
|
|
|
owner=user_admin, |
|
198
|
|
|
workspace=workspace, |
|
199
|
|
|
type=ContentType.Page, |
|
200
|
|
|
label='TEST_CONTENT_1', |
|
201
|
|
|
description='TEST_CONTENT_DESCRIPTION_1', |
|
202
|
|
|
revision_type=ActionDescription.CREATION, |
|
203
|
|
|
is_deleted=False, |
|
204
|
|
|
is_archived=False, |
|
205
|
|
|
) |
|
206
|
|
|
|
|
207
|
|
|
eq_( |
|
208
|
|
|
1, |
|
209
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
210
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_1' |
|
211
|
|
|
).count() |
|
212
|
|
|
) |
|
213
|
|
|
|
|
214
|
|
|
content = self.session.query(Content).filter( |
|
215
|
|
|
Content.id == first_content.id |
|
216
|
|
|
).one() |
|
217
|
|
|
eq_('TEST_CONTENT_1', content.label) |
|
218
|
|
|
eq_('TEST_CONTENT_DESCRIPTION_1', content.description) |
|
219
|
|
|
|
|
220
|
|
|
# Create a second content |
|
221
|
|
|
second_content = self._create_content( |
|
222
|
|
|
owner=user_admin, |
|
223
|
|
|
workspace=workspace, |
|
224
|
|
|
type=ContentType.Page, |
|
225
|
|
|
label='TEST_CONTENT_2', |
|
226
|
|
|
description='TEST_CONTENT_DESCRIPTION_2', |
|
227
|
|
|
revision_type=ActionDescription.CREATION |
|
228
|
|
|
) |
|
229
|
|
|
|
|
230
|
|
|
eq_( |
|
231
|
|
|
1, |
|
232
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
233
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_2' |
|
234
|
|
|
).count() |
|
235
|
|
|
) |
|
236
|
|
|
|
|
237
|
|
|
content = self.session.query(Content).filter( |
|
238
|
|
|
Content.id == second_content.id |
|
239
|
|
|
).one() |
|
240
|
|
|
eq_('TEST_CONTENT_2', content.label) |
|
241
|
|
|
eq_('TEST_CONTENT_DESCRIPTION_2', content.description) |
|
242
|
|
|
|
|
243
|
|
|
def test_create(self, key='1'): |
|
|
|
|
|
|
244
|
|
|
eq_( |
|
245
|
|
|
0, |
|
246
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
247
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_%s' % key).count() |
|
248
|
|
|
) |
|
249
|
|
|
eq_( |
|
250
|
|
|
0, |
|
251
|
|
|
self.session.query(Workspace).filter( |
|
252
|
|
|
Workspace.label == 'TEST_WORKSPACE_%s' % key).count() |
|
253
|
|
|
) |
|
254
|
|
|
|
|
255
|
|
|
user_admin = self.session.query(User).filter( |
|
256
|
|
|
User.email == '[email protected]' |
|
257
|
|
|
).one() |
|
258
|
|
|
workspace = Workspace(label="TEST_WORKSPACE_%s" % key) |
|
259
|
|
|
self.session.add(workspace) |
|
260
|
|
|
self.session.flush() |
|
261
|
|
|
eq_( |
|
262
|
|
|
1, |
|
263
|
|
|
self.session.query(Workspace).filter( |
|
264
|
|
|
Workspace.label == 'TEST_WORKSPACE_%s' % key |
|
265
|
|
|
).count() |
|
266
|
|
|
) |
|
267
|
|
|
|
|
268
|
|
|
created_content = self._create_content( |
|
269
|
|
|
owner=user_admin, |
|
270
|
|
|
workspace=workspace, |
|
271
|
|
|
type=ContentType.Page, |
|
272
|
|
|
label='TEST_CONTENT_%s' % key, |
|
273
|
|
|
description='TEST_CONTENT_DESCRIPTION_%s' % key, |
|
274
|
|
|
revision_type=ActionDescription.CREATION |
|
275
|
|
|
) |
|
276
|
|
|
|
|
277
|
|
|
eq_( |
|
278
|
|
|
1, |
|
279
|
|
|
self.session.query(ContentRevisionRO).filter( |
|
280
|
|
|
ContentRevisionRO.label == 'TEST_CONTENT_%s' % key |
|
281
|
|
|
).count() |
|
282
|
|
|
) |
|
283
|
|
|
|
|
284
|
|
|
content = self.session.query(Content).filter( |
|
285
|
|
|
Content.id == created_content.id |
|
286
|
|
|
).one() |
|
287
|
|
|
eq_('TEST_CONTENT_%s' % key, content.label) |
|
288
|
|
|
eq_('TEST_CONTENT_DESCRIPTION_%s' % key, content.description) |
|
289
|
|
|
|
|
290
|
|
|
return created_content |
|
291
|
|
|
|
|
292
|
|
|
def _get_user(self): |
|
293
|
|
|
email = '[email protected]' |
|
294
|
|
|
user_query = self.session.query(User) |
|
295
|
|
|
user_filter = user_query.filter(User.email == email) |
|
296
|
|
|
user = user_filter.one() |
|
297
|
|
|
return user |
|
298
|
|
|
|
|
299
|
|
|
def _create_content(self, *args, **kwargs): |
|
300
|
|
|
content = Content(*args, **kwargs) |
|
301
|
|
|
self.session.add(content) |
|
302
|
|
|
self.session.flush() |
|
303
|
|
|
return content |
|
304
|
|
|
|
|
305
|
|
|
def _create_content_from_nothing(self): |
|
306
|
|
|
user_admin = self._get_user() |
|
307
|
|
|
workspace = Workspace(label="TEST_WORKSPACE_1") |
|
308
|
|
|
content = self._create_content( |
|
309
|
|
|
owner=user_admin, |
|
310
|
|
|
workspace=workspace, |
|
311
|
|
|
type=ContentType.File, |
|
312
|
|
|
label='TEST_CONTENT_1', |
|
313
|
|
|
description='TEST_CONTENT_DESCRIPTION_1', |
|
314
|
|
|
revision_type=ActionDescription.CREATION, |
|
315
|
|
|
) |
|
316
|
|
|
return content |
|
317
|
|
|
|
|
318
|
|
|
def test_unit__content_depot_file(self): |
|
319
|
|
|
""" Depot file access thought content property methods. """ |
|
320
|
|
|
content = self._create_content_from_nothing() |
|
321
|
|
|
# tests uninitialized depot file |
|
322
|
|
|
eq_(content.depot_file, None) |
|
323
|
|
|
# initializes depot file |
|
324
|
|
|
# which is able to behave like a python file object |
|
325
|
|
|
content.depot_file = b'test' |
|
326
|
|
|
# tests initialized depot file |
|
327
|
|
|
assert content.depot_file |
|
328
|
|
|
# tests type of initialized depot file |
|
329
|
|
|
eq_(type(content.depot_file), UploadedFile) |
|
330
|
|
|
# tests content of initialized depot file |
|
331
|
|
|
# using depot_file.file of type StoredFile to fetch content back |
|
332
|
|
|
eq_(content.depot_file.file.read(), b'test') |
|
|
|
|
|
|
333
|
|
|
|
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.