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