tests.test_fields   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 175
Duplicated Lines 8.57 %

Importance

Changes 0
Metric Value
wmc 10
eloc 114
dl 15
loc 175
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_fields_search_filters() 0 21 1
A test_column_not_searchable() 0 17 1
A test_fields_mdata() 0 20 1
A fixtures_fields_global_search_filtering_with_regex() 0 14 1
A test_fields_global_search_filtering_with_regex() 0 21 2
A test_calculating_age_on_the_fly() 0 16 1
A fixtures_column_not_searchable() 0 11 1
A test_fields_filtering() 15 15 1
A fixtures_filed_filtering() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import pytest
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...
introduced by
Unable to import 'pytest'
Loading history...
2
from sqlalchemy import func
3
4
from datatables import ColumnDT, DataTables
5
6
from .helpers import create_dt_params
7
from .models import Address, User
8
9
10
def test_fields_mdata(session):
11
    """Test if the result's data have mData set."""
12
    columns = [
13
        ColumnDT(User.id, mData='ID'),
14
        ColumnDT(User.name, mData='Username'),
15
        ColumnDT(Address.description, mData='Address'),
16
        ColumnDT(User.created_at, mData='Created at')
17
    ]
18
19
    query = session.query().select_from(User).join(Address)
20
21
    params = create_dt_params(columns)
22
    rowTable = DataTables(params, query, columns)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
23
    res = rowTable.output_result()
24
25
    assert len(res['data']) == 3
26
    assert 'ID' in res['data'][0]
27
    assert 'Username' in res['data'][0]
28
    assert 'Address' in res['data'][0]
29
    assert 'Created at' in res['data'][0]
30
31
32
def test_fields_search_filters(session):
33
    """Test if the result's data are filtered after search."""
34
    query = session.query()
35
36
    columns = [
37
        ColumnDT(User.id, search_method='numeric'),
38
        ColumnDT(User.name, search_method='string_contains'),
39
        ColumnDT(User.birthday, search_method='date')
40
    ]
41
42
    user = session.query(User).filter(User.id == 4).one()
43
44
    params = create_dt_params(columns)
45
    params['columns[0][search][value]'] = '=4'
46
    params['columns[1][search][value]'] = user.name
47
    params['columns[2][search][value]'] = '>1965-02-02'
48
    params['columns[2][search][value]'] = '<=99'
49
    rowTable = DataTables(params, query, columns)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
50
    res = rowTable.output_result()
51
52
    assert len(res['data']) == 1
53
54
55
def test_calculating_age_on_the_fly(session):
56
    """Test if the result's have a custom field."""
57
    query = session.query().filter(User.id > 5)
58
59
    columns = [
60
        ColumnDT(User.id, search_method='numeric'),
61
        ColumnDT(User.name, search_method='string_contains'),
62
        ColumnDT(User.birthday, search_method='date'),
63
        ColumnDT(func.datetime('now') - User.birthday, search_method='numeric')
64
    ]
65
66
    params = create_dt_params(columns)
67
    rowTable = DataTables(params, query, columns)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
68
    res = rowTable.output_result()
69
70
    assert len(res['data']) == 10
71
72
73
@pytest.fixture(scope="function")
74
def fixtures_filed_filtering(session):
0 ignored issues
show
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...
75
    user51 = User(name='User 51')
76
    user52 = User(name='User 52')
77
78
    session.add(user51)
79
    session.add(user52)
80
    session.commit()
81
82
    yield
83
84
    session.delete(user51)
85
    session.delete(user52)
86
    session.commit()
87
88
89 View Code Duplication
@pytest.mark.usefixtures("fixtures_filed_filtering")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
90
def test_fields_filtering(session):
91
    """Test if result's are filtered from global search field."""
92
    columns = [ColumnDT(User.id, ), ColumnDT(User.name)]
93
94
    query = session.query().select_from(User)
95
96
    params = create_dt_params(columns, search='51')
97
    rowTable = DataTables(params, query, columns)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
98
    res = rowTable.output_result()
99
100
    assert len(res['data']) == 1
101
    assert res['recordsTotal'] == '52'
102
    assert res['recordsFiltered'] == '1'
103
    assert res['data'][0]['1'] == 'User 51'
104
105
106
@pytest.fixture(scope="function")
107
def fixtures_fields_global_search_filtering_with_regex(session):
0 ignored issues
show
Coding Style Naming introduced by
The name fixtures_fields_global_search_filtering_with_regex does not conform to the function 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...
108
    user51 = User(name='Run To')
109
    user52 = User(name='Feeeeear Of')
110
111
    session.add(user51)
112
    session.add(user52)
113
    session.commit()
114
115
    yield
116
117
    session.delete(user51)
118
    session.delete(user52)
119
    session.commit()
120
121
122
@pytest.mark.usefixtures("fixtures_fields_global_search_filtering_with_regex")
123
def test_fields_global_search_filtering_with_regex(session):
0 ignored issues
show
Coding Style Naming introduced by
The name test_fields_global_search_filtering_with_regex does not conform to the function 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...
124
    """Test if result's are filtered from global search field."""
125
    columns = [ColumnDT(User.id, ), ColumnDT(User.name)]
126
127
    query = session.query().select_from(User)
128
129
    params = create_dt_params(columns, search='Fe*ar')
130
    params['search[regex]'] = 'true'
131
132
    rowTable = DataTables(params, query, columns, allow_regex_searches=True)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
133
    res = rowTable.output_result()
134
135
    if 'error' in res:
136
        # unfortunately sqlite doesn't support regexp out of the box'
137
        assert 'no such function: REGEXP' in res['error']
138
    else:
139
        assert len(res['data']) == 1
140
        assert res['recordsTotal'] == '1'
141
        assert res['recordsFiltered'] == '1'
142
        assert res['data'][0]['1'] == 'Feeeeear Of'
143
144
145
@pytest.fixture(scope="function")
146
def fixtures_column_not_searchable(session):
0 ignored issues
show
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...
147
    user51 = User(name='User 51')
148
149
    session.add(user51)
150
    session.commit()
151
152
    yield
153
154
    session.delete(user51)
155
    session.commit()
156
157
158
@pytest.mark.usefixtures("fixtures_column_not_searchable")
159
def test_column_not_searchable(session):
160
    """Test if result's are filtered from global search field."""
161
    columns = [
162
        ColumnDT(User.id, mData='ID'),
163
        ColumnDT(User.name, mData='Username', global_search=False)
164
    ]
165
166
    query = session.query().select_from(User)
167
168
    params = create_dt_params(columns, search='User 51')
169
    rowTable = DataTables(params, query, columns)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable 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...
170
    res = rowTable.output_result()
171
172
    assert len(res['data']) == 0
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
173
    assert res['recordsTotal'] == '51'
174
    assert res['recordsFiltered'] == '0'
175