|
1
|
|
|
import pytest |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
23
|
|
|
res = rowTable.output_result() |
|
24
|
|
|
|
|
25
|
|
|
if not len(res['data']) == 3: |
|
26
|
|
|
raise AssertionError() |
|
27
|
|
|
if 'ID' not in res['data'][0]: |
|
28
|
|
|
raise AssertionError() |
|
29
|
|
|
if 'Username' not in res['data'][0]: |
|
30
|
|
|
raise AssertionError() |
|
31
|
|
|
if 'Address' not in res['data'][0]: |
|
32
|
|
|
raise AssertionError() |
|
33
|
|
|
if 'Created at' not in res['data'][0]: |
|
34
|
|
|
raise AssertionError() |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def test_fields_search_filters(session): |
|
38
|
|
|
"""Test if the result's data are filtered after search.""" |
|
39
|
|
|
query = session.query() |
|
40
|
|
|
|
|
41
|
|
|
columns = [ |
|
42
|
|
|
ColumnDT(User.id, search_method='numeric'), |
|
43
|
|
|
ColumnDT(User.name, search_method='string_contains'), |
|
44
|
|
|
ColumnDT(User.birthday, search_method='date') |
|
45
|
|
|
] |
|
46
|
|
|
|
|
47
|
|
|
user = session.query(User).filter(User.id == 4).one() |
|
48
|
|
|
|
|
49
|
|
|
params = create_dt_params(columns) |
|
50
|
|
|
params['columns[0][search][value]'] = '=4' |
|
51
|
|
|
params['columns[1][search][value]'] = user.name |
|
52
|
|
|
params['columns[2][search][value]'] = '>1965-02-02' |
|
53
|
|
|
params['columns[2][search][value]'] = '<=99' |
|
54
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
55
|
|
|
res = rowTable.output_result() |
|
56
|
|
|
|
|
57
|
|
|
if not len(res['data']) == 1: |
|
58
|
|
|
raise AssertionError() |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def test_calculating_age_on_the_fly(session): |
|
62
|
|
|
"""Test if the result's have a custom field.""" |
|
63
|
|
|
query = session.query().filter(User.id > 5) |
|
64
|
|
|
|
|
65
|
|
|
columns = [ |
|
66
|
|
|
ColumnDT(User.id, search_method='numeric'), |
|
67
|
|
|
ColumnDT(User.name, search_method='string_contains'), |
|
68
|
|
|
ColumnDT(User.birthday, search_method='date'), |
|
69
|
|
|
ColumnDT(func.datetime('now') - User.birthday, search_method='numeric') |
|
70
|
|
|
] |
|
71
|
|
|
|
|
72
|
|
|
params = create_dt_params(columns) |
|
73
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
74
|
|
|
res = rowTable.output_result() |
|
75
|
|
|
|
|
76
|
|
|
if not len(res['data']) == 10: |
|
77
|
|
|
raise AssertionError() |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
@pytest.fixture(scope="function") |
|
81
|
|
|
def fixtures_filed_filtering(session): |
|
|
|
|
|
|
82
|
|
|
user51 = User(name='User 51') |
|
83
|
|
|
user52 = User(name='User 52') |
|
84
|
|
|
|
|
85
|
|
|
session.add(user51) |
|
86
|
|
|
session.add(user52) |
|
87
|
|
|
session.commit() |
|
88
|
|
|
|
|
89
|
|
|
yield |
|
90
|
|
|
|
|
91
|
|
|
session.delete(user51) |
|
92
|
|
|
session.delete(user52) |
|
93
|
|
|
session.commit() |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
@pytest.mark.usefixtures("fixtures_filed_filtering") |
|
|
|
|
|
|
97
|
|
|
def test_fields_filtering(session): |
|
98
|
|
|
"""Test if result's are filtered from global search field.""" |
|
99
|
|
|
columns = [ColumnDT(User.id, ), ColumnDT(User.name)] |
|
100
|
|
|
|
|
101
|
|
|
query = session.query().select_from(User) |
|
102
|
|
|
|
|
103
|
|
|
params = create_dt_params(columns, search='51') |
|
104
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
105
|
|
|
res = rowTable.output_result() |
|
106
|
|
|
|
|
107
|
|
|
if not len(res['data']) == 1: |
|
108
|
|
|
raise AssertionError() |
|
109
|
|
|
if not res['recordsTotal'] == '52': |
|
110
|
|
|
raise AssertionError() |
|
111
|
|
|
if not res['recordsFiltered'] == '1': |
|
112
|
|
|
raise AssertionError() |
|
113
|
|
|
if not res['data'][0]['1'] == 'User 51': |
|
114
|
|
|
raise AssertionError() |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
@pytest.fixture(scope="function") |
|
118
|
|
|
def fixtures_fields_global_search_filtering_with_regex(session): |
|
|
|
|
|
|
119
|
|
|
user51 = User(name='Run To') |
|
120
|
|
|
user52 = User(name='Feeeeear Of') |
|
121
|
|
|
|
|
122
|
|
|
session.add(user51) |
|
123
|
|
|
session.add(user52) |
|
124
|
|
|
session.commit() |
|
125
|
|
|
|
|
126
|
|
|
yield |
|
127
|
|
|
|
|
128
|
|
|
session.delete(user51) |
|
129
|
|
|
session.delete(user52) |
|
130
|
|
|
session.commit() |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
@pytest.mark.usefixtures("fixtures_fields_global_search_filtering_with_regex") |
|
134
|
|
|
def test_fields_global_search_filtering_with_regex(session): |
|
|
|
|
|
|
135
|
|
|
"""Test if result's are filtered from global search field.""" |
|
136
|
|
|
columns = [ColumnDT(User.id, ), ColumnDT(User.name)] |
|
137
|
|
|
|
|
138
|
|
|
query = session.query().select_from(User) |
|
139
|
|
|
|
|
140
|
|
|
params = create_dt_params(columns, search='Fe*ar') |
|
141
|
|
|
params['search[regex]'] = 'true' |
|
142
|
|
|
|
|
143
|
|
|
rowTable = DataTables(params, query, columns, allow_regex_searches=True) |
|
|
|
|
|
|
144
|
|
|
res = rowTable.output_result() |
|
145
|
|
|
|
|
146
|
|
|
if 'error' in res: |
|
147
|
|
|
# unfortunately sqlite doesn't support regexp out of the box' |
|
148
|
|
|
if 'no such function: REGEXP' not in res['error']: |
|
149
|
|
|
raise AssertionError() |
|
150
|
|
|
else: |
|
151
|
|
|
if not len(res['data']) == 1: |
|
152
|
|
|
raise AssertionError() |
|
153
|
|
|
if not res['recordsTotal'] == '1': |
|
154
|
|
|
raise AssertionError() |
|
155
|
|
|
if not res['recordsFiltered'] == '1': |
|
156
|
|
|
raise AssertionError() |
|
157
|
|
|
if not res['data'][0]['1'] == 'Feeeeear Of': |
|
158
|
|
|
raise AssertionError() |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
@pytest.fixture(scope="function") |
|
162
|
|
|
def fixtures_column_not_searchable(session): |
|
|
|
|
|
|
163
|
|
|
user51 = User(name='User 51') |
|
164
|
|
|
|
|
165
|
|
|
session.add(user51) |
|
166
|
|
|
session.commit() |
|
167
|
|
|
|
|
168
|
|
|
yield |
|
169
|
|
|
|
|
170
|
|
|
session.delete(user51) |
|
171
|
|
|
session.commit() |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
@pytest.mark.usefixtures("fixtures_column_not_searchable") |
|
175
|
|
|
def test_column_not_searchable(session): |
|
176
|
|
|
"""Test if result's are filtered from global search field.""" |
|
177
|
|
|
columns = [ |
|
178
|
|
|
ColumnDT(User.id, mData='ID'), |
|
179
|
|
|
ColumnDT(User.name, mData='Username', global_search=False) |
|
180
|
|
|
] |
|
181
|
|
|
|
|
182
|
|
|
query = session.query().select_from(User) |
|
183
|
|
|
|
|
184
|
|
|
params = create_dt_params(columns, search='User 51') |
|
185
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
186
|
|
|
res = rowTable.output_result() |
|
187
|
|
|
|
|
188
|
|
|
if not len(res['data']) == 0: |
|
|
|
|
|
|
189
|
|
|
raise AssertionError() |
|
190
|
|
|
if not res['recordsTotal'] == '51': |
|
191
|
|
|
raise AssertionError() |
|
192
|
|
|
if not res['recordsFiltered'] == '0': |
|
193
|
|
|
raise AssertionError() |
|
194
|
|
|
|
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.