tracim.tests.models.test_content_revision   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 55
dl 0
loc 78
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B TestContentRevision.test_new_revision() 0 39 1
A TestContentRevision._get_dict_representation() 0 6 2
A TestContentRevision._new_from() 0 17 4
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
13
14
    def _new_from(self, revision):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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