1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
|
3
|
|
|
from datatables import ColumnDT, DataTables |
4
|
|
|
|
5
|
|
|
from .helpers import create_dt_params |
6
|
|
|
from .models import Address, User |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
def test_list(session): |
|
|
|
|
10
|
|
|
"""Test if it returns a list of users.""" |
11
|
|
|
columns = [ColumnDT(User.id)] |
12
|
|
|
|
13
|
|
|
query = session.query().select_from(User) |
14
|
|
|
|
15
|
|
|
params = create_dt_params(columns) |
16
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
17
|
|
|
res = rowTable.output_result() |
18
|
|
|
|
19
|
|
|
if not len(res['data']) == 10: |
20
|
|
|
raise AssertionError() |
21
|
|
|
if not len(res['data'][0]) == 1: |
22
|
|
|
raise AssertionError() |
23
|
|
|
if not res['recordsTotal'] == '50': |
24
|
|
|
raise AssertionError() |
25
|
|
|
if not res['recordsFiltered'] == '50': |
26
|
|
|
raise AssertionError() |
27
|
|
|
|
28
|
|
|
|
29
|
|
View Code Duplication |
def test_list_bad_length(session): |
|
|
|
|
30
|
|
|
"""Test if it returns an error for querying with a bad length.""" |
31
|
|
|
columns = [ColumnDT(User.id)] |
32
|
|
|
|
33
|
|
|
query = session.query() |
34
|
|
|
|
35
|
|
|
params = create_dt_params(columns, length=-10) |
36
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
37
|
|
|
res = rowTable.output_result() |
38
|
|
|
|
39
|
|
|
if 'Length should be' not in res['error']: |
40
|
|
|
raise AssertionError() |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_list_detail(session): |
44
|
|
|
"""Test if it returns a list of detailed users.""" |
45
|
|
|
columns = [ |
46
|
|
|
ColumnDT(User.id), |
47
|
|
|
ColumnDT(User.name), |
48
|
|
|
ColumnDT(Address.description), |
49
|
|
|
ColumnDT(User.created_at) |
50
|
|
|
] |
51
|
|
|
|
52
|
|
|
query = session.query() |
53
|
|
|
|
54
|
|
|
params = create_dt_params(columns) |
55
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
56
|
|
|
res = rowTable.output_result() |
57
|
|
|
|
58
|
|
|
if not len(res['data'][0]) == 4: |
59
|
|
|
raise AssertionError() |
60
|
|
|
|
61
|
|
|
|
62
|
|
View Code Duplication |
def test_list_fixed_length(session): |
|
|
|
|
63
|
|
|
"""Test if it returns a fixed list of users.""" |
64
|
|
|
columns = [ColumnDT(User.id)] |
65
|
|
|
|
66
|
|
|
query = session.query() |
67
|
|
|
|
68
|
|
|
params = create_dt_params(columns, length=7) |
69
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
70
|
|
|
res = rowTable.output_result() |
71
|
|
|
|
72
|
|
|
if not len(res['data']) == 7: |
73
|
|
|
raise AssertionError() |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_list_inner_join(session): |
77
|
|
|
"""Test if it returns a list of users with address.""" |
78
|
|
|
columns = [ColumnDT(User.id)] |
79
|
|
|
|
80
|
|
|
query = session.query().select_from(User).join(Address) |
81
|
|
|
|
82
|
|
|
params = create_dt_params(columns) |
83
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
84
|
|
|
res = rowTable.output_result() |
85
|
|
|
|
86
|
|
|
if not len(res['data']) == 3: |
87
|
|
|
raise AssertionError() |
88
|
|
|
if not res['recordsTotal'] == '3': |
89
|
|
|
raise AssertionError() |
90
|
|
|
if not res['recordsFiltered'] == '3': |
91
|
|
|
raise AssertionError() |
92
|
|
|
|
93
|
|
|
|
94
|
|
View Code Duplication |
def test_list_total_length(session): |
|
|
|
|
95
|
|
|
"""Test if it returns the total list of users.""" |
96
|
|
|
columns = [ColumnDT(User.id)] |
97
|
|
|
|
98
|
|
|
query = session.query() |
99
|
|
|
|
100
|
|
|
params = create_dt_params(columns, length=-1) |
101
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
102
|
|
|
res = rowTable.output_result() |
103
|
|
|
|
104
|
|
|
if not len(res['data']) == 50: |
105
|
|
|
raise AssertionError() |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
@pytest.fixture(scope="function") |
109
|
|
|
def fixtures_list_hybrid_attributes(session): |
|
|
|
|
110
|
|
|
user51 = User(name='User 51') |
111
|
|
|
|
112
|
|
|
session.add(user51) |
113
|
|
|
session.commit() |
114
|
|
|
|
115
|
|
|
yield |
116
|
|
|
|
117
|
|
|
session.delete(user51) |
118
|
|
|
session.commit() |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
@pytest.mark.usefixtures("fixtures_list_hybrid_attributes") |
122
|
|
|
def test_list_hybrid_attributes(session): |
123
|
|
|
"""Test if it returns a list of users with a hybrid property.""" |
124
|
|
|
columns = [ |
125
|
|
|
ColumnDT(User.id), |
126
|
|
|
ColumnDT(User.dummy), |
127
|
|
|
ColumnDT(User.name), |
128
|
|
|
ColumnDT(User.created_at) |
129
|
|
|
] |
130
|
|
|
|
131
|
|
|
session.query(*[User.id, User.dummy]).all() |
132
|
|
|
|
133
|
|
|
query = session.query() |
134
|
|
|
|
135
|
|
|
params = create_dt_params(columns, start=50, length=10) |
136
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
137
|
|
|
res = rowTable.output_result() |
138
|
|
|
|
139
|
|
|
if not len(res['data']) == 1: |
140
|
|
|
raise AssertionError() |
141
|
|
|
if not res['data'][0]['1'] == 'Us': |
142
|
|
|
raise AssertionError() |
143
|
|
|
if not res['data'][0]['2'] == 'User 51': |
144
|
|
|
raise AssertionError() |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
@pytest.fixture(scope="function") |
148
|
|
|
def fixtures_list_specific_page(session): |
|
|
|
|
149
|
|
|
user51 = User(name='User 51') |
150
|
|
|
user52 = User(name='User 52') |
151
|
|
|
|
152
|
|
|
session.add(user51) |
153
|
|
|
session.add(user52) |
154
|
|
|
session.commit() |
155
|
|
|
|
156
|
|
|
yield |
157
|
|
|
|
158
|
|
|
session.delete(user51) |
159
|
|
|
session.delete(user52) |
160
|
|
|
session.commit() |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
@pytest.mark.usefixtures("fixtures_list_specific_page") |
164
|
|
|
def test_list_specific_page(session): |
165
|
|
|
"""Test if it returns the list of users that are on page 6.""" |
166
|
|
|
columns = [ColumnDT(User.id)] |
167
|
|
|
|
168
|
|
|
query = session.query().select_from(User) |
169
|
|
|
|
170
|
|
|
params = create_dt_params(columns, start=50, length=10) |
171
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
172
|
|
|
res = rowTable.output_result() |
173
|
|
|
|
174
|
|
|
if not len(res['data']) == 2: |
175
|
|
|
raise AssertionError() |
176
|
|
|
if not res['recordsTotal'] == '52': |
177
|
|
|
raise AssertionError() |
178
|
|
|
if not res['recordsFiltered'] == '52': |
179
|
|
|
raise AssertionError() |
180
|
|
|
if not res['data'][0]['0'] == 51: |
181
|
|
|
raise AssertionError() |
182
|
|
|
if not res['data'][1]['0'] == 52: |
183
|
|
|
raise AssertionError() |
184
|
|
|
|
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.