|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
from collections import OrderedDict |
|
3
|
|
|
|
|
4
|
|
|
from sqlalchemy import inspect |
|
5
|
|
|
|
|
6
|
|
|
from tracim.models import ContentRevisionRO |
|
7
|
|
|
from tracim.models import User |
|
8
|
|
|
from tracim.models.data import ContentType |
|
9
|
|
|
from tracim.tests import DefaultTest |
|
10
|
|
|
from tracim.tests import eq_ |
|
11
|
|
|
|
|
12
|
|
|
class TestContentRevision(DefaultTest): |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def _new_from(self, revision): |
|
|
|
|
|
|
15
|
|
|
excluded_columns = ( |
|
16
|
|
|
'revision_id', |
|
17
|
|
|
'_sa_instance_state', |
|
18
|
|
|
'depot_file', |
|
19
|
|
|
'node', |
|
20
|
|
|
'revision_read_statuses', |
|
21
|
|
|
) |
|
22
|
|
|
revision_columns = [attr.key for attr in inspect(revision).attrs |
|
23
|
|
|
if attr.key not in excluded_columns] |
|
24
|
|
|
new_revision = ContentRevisionRO() |
|
25
|
|
|
|
|
26
|
|
|
for revision_column in revision_columns: |
|
27
|
|
|
old_revision_column_value = getattr(revision, revision_column) |
|
28
|
|
|
setattr(new_revision, revision_column, old_revision_column_value) |
|
29
|
|
|
|
|
30
|
|
|
return new_revision |
|
31
|
|
|
|
|
32
|
|
|
def _get_dict_representation(self, revision): |
|
|
|
|
|
|
33
|
|
|
keys_to_remove = ('updated', '_sa_instance_state') |
|
34
|
|
|
dict_repr = OrderedDict(sorted(revision.__dict__.items())) |
|
35
|
|
|
for key_to_remove in keys_to_remove: |
|
36
|
|
|
dict_repr.pop(key_to_remove, None) |
|
37
|
|
|
return dict_repr |
|
38
|
|
|
|
|
39
|
|
|
def test_new_revision(self): |
|
|
|
|
|
|
40
|
|
|
admin = self.session.query(User).filter( |
|
41
|
|
|
User.email == '[email protected]' |
|
42
|
|
|
).one() |
|
43
|
|
|
workspace = self._create_workspace_and_test( |
|
44
|
|
|
name='workspace_1', |
|
45
|
|
|
user=admin |
|
46
|
|
|
) |
|
47
|
|
|
folder = self._create_content_and_test( |
|
48
|
|
|
name='folder_1', |
|
49
|
|
|
workspace=workspace, |
|
50
|
|
|
type=ContentType.Folder |
|
51
|
|
|
) |
|
52
|
|
|
page = self._create_content_and_test( |
|
53
|
|
|
workspace=workspace, |
|
54
|
|
|
parent=folder, |
|
55
|
|
|
name='file_1', |
|
56
|
|
|
description='content of file_1', |
|
57
|
|
|
type=ContentType.Page, |
|
58
|
|
|
owner=admin |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
self.session.flush() |
|
62
|
|
|
|
|
63
|
|
|
# Model create a new instance with list of column |
|
64
|
|
|
new_revision_by_model = ContentRevisionRO.new_from(page.revision) |
|
65
|
|
|
# Test create a new instance from dynamic listing of model |
|
66
|
|
|
# columns mapping |
|
67
|
|
|
new_revision_by_test = self._new_from(page.revision) |
|
68
|
|
|
|
|
69
|
|
|
new_revision_by_model_dict = self._get_dict_representation( |
|
70
|
|
|
new_revision_by_model |
|
71
|
|
|
) |
|
72
|
|
|
new_revision_by_test_dict = self._get_dict_representation( |
|
73
|
|
|
new_revision_by_test |
|
74
|
|
|
) |
|
75
|
|
|
|
|
76
|
|
|
# They must be identical |
|
77
|
|
|
eq_(new_revision_by_model_dict, new_revision_by_test_dict) |
|
78
|
|
|
|
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.