tracim.tests   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 110
dl 0
loc 160
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseTest.init_database() 0 4 1
A DefaultTest._create_thread_and_test() 0 22 1
B DefaultTest._create_content_and_test() 0 31 1
A BaseTest.tearDown() 0 7 1
A StandardTest.init_database() 0 6 1
B BaseTest.setUp() 0 28 1
A DefaultTest._create_workspace_and_test() 0 19 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A eq_() 0 3 2
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
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):
0 ignored issues
show
Coding Style Naming introduced by
The name a does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name b does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function 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...
19
    # TODO - G.M - 05-04-2018 - Remove this when all old nose code is removed
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
20
    assert a == b, msg or "%r != %r" % (a, b)
21
22
23
class BaseTest(unittest.TestCase):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
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()
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _clear was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
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):
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...
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):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
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):
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...
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