1
|
|
|
from sqlalchemy import func |
|
|
|
|
2
|
|
|
|
3
|
|
|
from datatables import ColumnDT, DataTables |
4
|
|
|
|
5
|
|
|
from .helpers import create_dt_params |
6
|
|
|
from .models import Address, User |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def get_result(session, column, search_method, search_value): |
|
|
|
|
10
|
|
|
columns = [ColumnDT(column, search_method=search_method)] |
11
|
|
|
query = session.query() |
12
|
|
|
params = create_dt_params(columns) |
13
|
|
|
params['columns[0][search][value]'] = search_value |
14
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
15
|
|
|
return rowTable.output_result() |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def test_method_none(session): |
|
|
|
|
19
|
|
|
res = get_result( |
20
|
|
|
session=session, |
21
|
|
|
column=User.id, |
22
|
|
|
search_method='none', |
23
|
|
|
search_value='abc') |
24
|
|
|
assert res['recordsFiltered'] == '50' |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_method_numeric(session): |
|
|
|
|
28
|
|
|
res = get_result( |
29
|
|
|
session=session, |
30
|
|
|
column=User.id, |
31
|
|
|
search_method='numeric', |
32
|
|
|
search_value='10') |
33
|
|
|
assert res['recordsFiltered'] == '1' |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_method_numeric_illegal_input(session): |
|
|
|
|
37
|
|
|
res = get_result( |
38
|
|
|
session=session, |
39
|
|
|
column=User.id, |
40
|
|
|
search_method='numeric', |
41
|
|
|
search_value='abc') |
42
|
|
|
try: |
43
|
|
|
float('abc') |
44
|
|
|
except ValueError as exc: |
45
|
|
|
expectedError = str(exc) |
|
|
|
|
46
|
|
|
assert expectedError in res['error'] |
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_method_numeric_eq(session): |
|
|
|
|
50
|
|
|
res = get_result( |
51
|
|
|
session=session, |
52
|
|
|
column=User.id, |
53
|
|
|
search_method='numeric', |
54
|
|
|
search_value='=10') |
55
|
|
|
assert res['recordsFiltered'] == '1' |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def test_method_numeric_gt(session): |
|
|
|
|
59
|
|
|
res = get_result( |
60
|
|
|
session=session, |
61
|
|
|
column=User.id, |
62
|
|
|
search_method='numeric', |
63
|
|
|
search_value='>10') |
64
|
|
|
assert res['recordsFiltered'] == '40' |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_method_numeric_lte(session): |
|
|
|
|
68
|
|
|
res = get_result( |
69
|
|
|
session=session, |
70
|
|
|
column=User.id, |
71
|
|
|
search_method='numeric', |
72
|
|
|
search_value='<=10') |
73
|
|
|
assert res['recordsFiltered'] == '10' |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_yadcf_range_number_gt(session): |
|
|
|
|
77
|
|
|
res = get_result( |
78
|
|
|
session=session, |
79
|
|
|
column=User.id, |
80
|
|
|
search_method='yadcf_range_number', |
81
|
|
|
search_value='10-yadcf_delim-') |
82
|
|
|
assert res['recordsFiltered'] == '41' |
83
|
|
|
assert res['yadcf_data_0'] == (1, 50) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_yadcf_range_number_lt(session): |
|
|
|
|
87
|
|
|
res = get_result( |
88
|
|
|
session=session, |
89
|
|
|
column=User.id, |
90
|
|
|
search_method='yadcf_range_number', |
91
|
|
|
search_value='-yadcf_delim-10') |
92
|
|
|
assert res['recordsFiltered'] == '10' |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_yadcf_range_number_range(session): |
|
|
|
|
96
|
|
|
res = get_result( |
97
|
|
|
session=session, |
98
|
|
|
column=User.id, |
99
|
|
|
search_method='yadcf_range_number', |
100
|
|
|
search_value='10-yadcf_delim-15') |
101
|
|
|
assert res['recordsFiltered'] == '6' |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test_string_contains(session): |
|
|
|
|
105
|
|
|
res = get_result( |
106
|
|
|
session=session, |
107
|
|
|
column=Address.description, |
108
|
|
|
search_method='string_contains', |
109
|
|
|
search_value='street') |
110
|
|
|
assert res['recordsFiltered'] == '1' |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def test_like(session): |
|
|
|
|
114
|
|
|
res = get_result( |
115
|
|
|
session=session, |
116
|
|
|
column=Address.description, |
117
|
|
|
search_method='like', |
118
|
|
|
search_value='%Street%') |
119
|
|
|
assert res['recordsFiltered'] == '1' |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_ilike(session): |
|
|
|
|
123
|
|
|
res = get_result( |
124
|
|
|
session=session, |
125
|
|
|
column=Address.description, |
126
|
|
|
search_method='ilike', |
127
|
|
|
search_value='%street%') |
128
|
|
|
assert res['recordsFiltered'] == '1' |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def test_date_lt(session): |
|
|
|
|
132
|
|
|
res = get_result( |
133
|
|
|
session=session, |
134
|
|
|
column=User.birthday, |
135
|
|
|
search_method='date', |
136
|
|
|
search_value='<1970-01-03') |
137
|
|
|
print(res) |
138
|
|
|
assert res['recordsFiltered'] == '1' |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def test_yadcf_range_date(session): |
|
|
|
|
142
|
|
|
res = get_result( |
143
|
|
|
session=session, |
144
|
|
|
column=User.birthday, |
145
|
|
|
search_method='yadcf_range_date', |
146
|
|
|
search_value='1970-01-03-yadcf_delim-1970-01-13') |
147
|
|
|
print(res) |
148
|
|
|
assert res['recordsFiltered'] == '1' |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
def test_yadcf_autocomplete(session): |
|
|
|
|
152
|
|
|
res = get_result( |
153
|
|
|
session=session, |
154
|
|
|
column=Address.description, |
155
|
|
|
search_method='yadcf_autocomplete', |
156
|
|
|
search_value='Avenue') |
157
|
|
|
assert set(res['yadcf_data_0']) == set(['Avenue', 'Road', 'Street']) |
158
|
|
|
assert res['recordsFiltered'] == '1' |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
def test_yadcf_select(session): |
|
|
|
|
162
|
|
|
res = get_result( |
163
|
|
|
session=session, |
164
|
|
|
column=Address.description, |
165
|
|
|
search_method='yadcf_select', |
166
|
|
|
search_value='Road') |
167
|
|
|
assert set(res['yadcf_data_0']) == set(['Avenue', 'Road', 'Street']) |
168
|
|
|
assert res['recordsFiltered'] == '1' |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
def test_yadcf_multi_select(session): |
|
|
|
|
172
|
|
|
res = get_result( |
173
|
|
|
session=session, |
174
|
|
|
column=Address.description, |
175
|
|
|
search_method='yadcf_multi_select', |
176
|
|
|
search_value='Avenue|StreetRoad') |
177
|
|
|
assert set(res['yadcf_data_0']) == set(['Avenue', 'Road', 'Street']) |
178
|
|
|
assert res['recordsFiltered'] == '1' |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_group_by(session): |
182
|
|
|
"""Test group by after a join query.""" |
183
|
|
|
columns = [ColumnDT(func.count(User.id)), ColumnDT(Address.id)] |
184
|
|
|
|
185
|
|
|
query = session.query().\ |
186
|
|
|
select_from(User).\ |
187
|
|
|
join(Address).\ |
188
|
|
|
group_by(Address) |
189
|
|
|
|
190
|
|
|
params = create_dt_params(columns) |
191
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
192
|
|
|
res = rowTable.output_result() |
193
|
|
|
|
194
|
|
|
assert len(res['data']) == 3 |
195
|
|
|
|
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.