|
1
|
|
|
import pytest |
|
2
|
|
|
from sqlalchemy import func |
|
3
|
|
|
|
|
4
|
|
|
from datatables import ColumnDT, DataTables |
|
5
|
|
|
|
|
6
|
|
|
from .helpers import ( |
|
7
|
|
|
create_dt_params, |
|
8
|
|
|
create_dt_params_with_mData, |
|
9
|
|
|
create_dt_params_with_mData_shuffled, |
|
10
|
|
|
create_dt_params_with_mData_with_extra_data, |
|
11
|
|
|
) |
|
12
|
|
|
from .models import Address, User |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@pytest.mark.parametrize( |
|
16
|
|
|
("create_dt_params_function"), |
|
17
|
|
|
( |
|
18
|
|
|
(create_dt_params), |
|
19
|
|
|
(create_dt_params_with_mData), |
|
20
|
|
|
(create_dt_params_with_mData_shuffled), |
|
21
|
|
|
(create_dt_params_with_mData_with_extra_data), |
|
22
|
|
|
), |
|
23
|
|
|
) |
|
24
|
|
|
def test_fields_mdata(session, create_dt_params_function): |
|
25
|
|
|
"""Test if the result's data have mData set.""" |
|
26
|
|
|
columns = [ |
|
27
|
|
|
ColumnDT(User.id, mData="ID"), |
|
28
|
|
|
ColumnDT(User.name, mData="Username"), |
|
29
|
|
|
ColumnDT(Address.description, mData="Address"), |
|
30
|
|
|
ColumnDT(User.created_at, mData="Created at"), |
|
31
|
|
|
] |
|
32
|
|
|
|
|
33
|
|
|
query = session.query().select_from(User).join(Address) |
|
34
|
|
|
|
|
35
|
|
|
params = create_dt_params_function(columns) |
|
36
|
|
|
rowTable = DataTables(params, query, columns) |
|
37
|
|
|
res = rowTable.output_result() |
|
38
|
|
|
assert len(res["data"]) == 3 |
|
39
|
|
|
assert "ID" in res["data"][0] |
|
40
|
|
|
assert "Username" in res["data"][0] |
|
41
|
|
|
assert "Address" in res["data"][0] |
|
42
|
|
|
assert "Created at" in res["data"][0] |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_fields_search_filters(session): |
|
46
|
|
|
"""Test if the result's data are filtered after search.""" |
|
47
|
|
|
query = session.query() |
|
48
|
|
|
|
|
49
|
|
|
columns = [ |
|
50
|
|
|
ColumnDT(User.id, search_method="numeric"), |
|
51
|
|
|
ColumnDT(User.name, search_method="string_contains"), |
|
52
|
|
|
ColumnDT(User.birthday, search_method="date"), |
|
53
|
|
|
] |
|
54
|
|
|
|
|
55
|
|
|
user = session.query(User).filter(User.id == 4).one() |
|
56
|
|
|
|
|
57
|
|
|
params = create_dt_params(columns) |
|
58
|
|
|
params["columns[0][search][value]"] = "=4" |
|
59
|
|
|
params["columns[1][search][value]"] = user.name |
|
60
|
|
|
params["columns[2][search][value]"] = ">1965-02-02" |
|
61
|
|
|
params["columns[2][search][value]"] = "<=99" |
|
62
|
|
|
rowTable = DataTables(params, query, columns) |
|
63
|
|
|
res = rowTable.output_result() |
|
64
|
|
|
|
|
65
|
|
|
assert len(res["data"]) == 1 |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def test_calculating_age_on_the_fly(session): |
|
69
|
|
|
"""Test if the result's have a custom field.""" |
|
70
|
|
|
query = session.query().filter(User.id > 5) |
|
71
|
|
|
|
|
72
|
|
|
columns = [ |
|
73
|
|
|
ColumnDT(User.id, search_method="numeric"), |
|
74
|
|
|
ColumnDT(User.name, search_method="string_contains"), |
|
75
|
|
|
ColumnDT(User.birthday, search_method="date"), |
|
76
|
|
|
ColumnDT(func.datetime("now") - User.birthday, search_method="numeric"), |
|
77
|
|
|
] |
|
78
|
|
|
|
|
79
|
|
|
params = create_dt_params(columns) |
|
80
|
|
|
rowTable = DataTables(params, query, columns) |
|
81
|
|
|
res = rowTable.output_result() |
|
82
|
|
|
|
|
83
|
|
|
assert len(res["data"]) == 10 |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
@pytest.fixture(scope="function") |
|
87
|
|
|
def fixtures_filed_filtering(session): |
|
88
|
|
|
user51 = User(name="User 51") |
|
89
|
|
|
user52 = User(name="User 52") |
|
90
|
|
|
|
|
91
|
|
|
session.add(user51) |
|
92
|
|
|
session.add(user52) |
|
93
|
|
|
session.commit() |
|
94
|
|
|
|
|
95
|
|
|
yield |
|
96
|
|
|
|
|
97
|
|
|
session.delete(user51) |
|
98
|
|
|
session.delete(user52) |
|
99
|
|
|
session.commit() |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
@pytest.mark.usefixtures("fixtures_filed_filtering") |
|
|
|
|
|
|
103
|
|
|
def test_fields_filtering(session): |
|
104
|
|
|
"""Test if result's are filtered from global search field.""" |
|
105
|
|
|
columns = [ |
|
106
|
|
|
ColumnDT( |
|
107
|
|
|
User.id, |
|
108
|
|
|
), |
|
109
|
|
|
ColumnDT(User.name), |
|
110
|
|
|
] |
|
111
|
|
|
|
|
112
|
|
|
query = session.query().select_from(User) |
|
113
|
|
|
|
|
114
|
|
|
params = create_dt_params(columns, search="51") |
|
115
|
|
|
rowTable = DataTables(params, query, columns) |
|
116
|
|
|
res = rowTable.output_result() |
|
117
|
|
|
|
|
118
|
|
|
assert len(res["data"]) == 1 |
|
119
|
|
|
assert res["recordsTotal"] == "52" |
|
120
|
|
|
assert res["recordsFiltered"] == "1" |
|
121
|
|
|
assert res["data"][0]["1"] == "User 51" |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
@pytest.fixture(scope="function") |
|
125
|
|
|
def fixtures_fields_global_search_filtering_with_regex(session): |
|
126
|
|
|
user51 = User(name="Run To") |
|
127
|
|
|
user52 = User(name="Feeeeear Of") |
|
128
|
|
|
|
|
129
|
|
|
session.add(user51) |
|
130
|
|
|
session.add(user52) |
|
131
|
|
|
session.commit() |
|
132
|
|
|
|
|
133
|
|
|
yield |
|
134
|
|
|
|
|
135
|
|
|
session.delete(user51) |
|
136
|
|
|
session.delete(user52) |
|
137
|
|
|
session.commit() |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
@pytest.mark.usefixtures("fixtures_fields_global_search_filtering_with_regex") |
|
141
|
|
|
def test_fields_global_search_filtering_with_regex(session): |
|
142
|
|
|
"""Test if result's are filtered from global search field.""" |
|
143
|
|
|
columns = [ |
|
144
|
|
|
ColumnDT( |
|
145
|
|
|
User.id, |
|
146
|
|
|
), |
|
147
|
|
|
ColumnDT(User.name), |
|
148
|
|
|
] |
|
149
|
|
|
|
|
150
|
|
|
query = session.query().select_from(User) |
|
151
|
|
|
|
|
152
|
|
|
params = create_dt_params(columns, search="Fe*ar") |
|
153
|
|
|
params["search[regex]"] = "true" |
|
154
|
|
|
|
|
155
|
|
|
rowTable = DataTables(params, query, columns, allow_regex_searches=True) |
|
156
|
|
|
res = rowTable.output_result() |
|
157
|
|
|
|
|
158
|
|
|
if "error" in res: |
|
159
|
|
|
# unfortunately sqlite doesn't support regexp out of the box' |
|
160
|
|
|
assert "no such function: REGEXP" in res["error"] |
|
161
|
|
|
else: |
|
162
|
|
|
assert len(res["data"]) == 1 |
|
163
|
|
|
assert res["recordsTotal"] == "1" |
|
164
|
|
|
assert res["recordsFiltered"] == "1" |
|
165
|
|
|
assert res["data"][0]["1"] == "Feeeeear Of" |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
@pytest.fixture(scope="function") |
|
169
|
|
|
def fixtures_column_not_searchable(session): |
|
170
|
|
|
user51 = User(name="User 51") |
|
171
|
|
|
|
|
172
|
|
|
session.add(user51) |
|
173
|
|
|
session.commit() |
|
174
|
|
|
|
|
175
|
|
|
yield |
|
176
|
|
|
|
|
177
|
|
|
session.delete(user51) |
|
178
|
|
|
session.commit() |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
@pytest.mark.usefixtures("fixtures_column_not_searchable") |
|
182
|
|
|
def test_column_not_searchable(session): |
|
183
|
|
|
"""Test if result's are filtered from global search field.""" |
|
184
|
|
|
columns = [ |
|
185
|
|
|
ColumnDT(User.id, mData="ID"), |
|
186
|
|
|
ColumnDT(User.name, mData="Username", global_search=False), |
|
187
|
|
|
] |
|
188
|
|
|
|
|
189
|
|
|
query = session.query().select_from(User) |
|
190
|
|
|
|
|
191
|
|
|
params = create_dt_params(columns, search="User 51") |
|
192
|
|
|
rowTable = DataTables(params, query, columns) |
|
193
|
|
|
res = rowTable.output_result() |
|
194
|
|
|
|
|
195
|
|
|
assert len(res["data"]) == 0 |
|
196
|
|
|
assert res["recordsTotal"] == "51" |
|
197
|
|
|
assert res["recordsFiltered"] == "0" |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
@pytest.mark.parametrize( |
|
201
|
|
|
("yadcf_data_param", "result"), |
|
202
|
|
|
( |
|
203
|
|
|
(True, True), |
|
204
|
|
|
(False, False), |
|
205
|
|
|
), |
|
206
|
|
|
) |
|
207
|
|
|
def test_column_yadcf_data(session, yadcf_data_param, result): |
|
208
|
|
|
"""Test if result's have yadcf data or not based on the given parameters.""" |
|
209
|
|
|
columns = [ |
|
210
|
|
|
ColumnDT(User.id, mData="ID"), |
|
211
|
|
|
ColumnDT( |
|
212
|
|
|
User.name, |
|
213
|
|
|
mData="Username", |
|
214
|
|
|
search_method="yadcf_select", |
|
215
|
|
|
yadcf_data=yadcf_data_param, |
|
216
|
|
|
), |
|
217
|
|
|
] |
|
218
|
|
|
|
|
219
|
|
|
query = session.query().select_from(User) |
|
220
|
|
|
|
|
221
|
|
|
params = create_dt_params(columns) |
|
222
|
|
|
rowTable = DataTables(params, query, columns) |
|
223
|
|
|
res = rowTable.output_result() |
|
224
|
|
|
|
|
225
|
|
|
assert len(res["data"]) == 10 |
|
226
|
|
|
assert res["recordsTotal"] == "50" |
|
227
|
|
|
assert ("yadcf_data_1" in res) == result |
|
228
|
|
|
|