|
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
|
|
|
@pytest.fixture(scope="function") |
|
10
|
|
|
def fixtures_ordering(session): |
|
11
|
|
|
"""Set up fake population before tests.""" |
|
12
|
|
|
user51 = User(name='000_User') |
|
13
|
|
|
user52 = User(name='zzz_User') |
|
14
|
|
|
addr4 = Address(description='000_Address') |
|
15
|
|
|
addr5 = Address(description='zzz_Address') |
|
16
|
|
|
user53 = User(name='UserFirstAddress', address=addr4) |
|
17
|
|
|
user54 = User(name='UserLastAddress', address=addr5) |
|
18
|
|
|
session.add(user51) |
|
19
|
|
|
session.add(user52) |
|
20
|
|
|
session.add(user53) |
|
21
|
|
|
session.add(user54) |
|
22
|
|
|
session.commit() |
|
23
|
|
|
|
|
24
|
|
|
yield |
|
25
|
|
|
|
|
26
|
|
|
session.delete(user51) |
|
27
|
|
|
session.delete(user52) |
|
28
|
|
|
session.delete(user53) |
|
29
|
|
|
session.delete(user54) |
|
30
|
|
|
session.delete(addr4) |
|
31
|
|
|
session.delete(addr5) |
|
32
|
|
|
session.commit() |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
@pytest.mark.usefixtures("fixtures_ordering") |
|
36
|
|
|
def test_ordering(session): |
|
37
|
|
|
"""Test if it returns a list with the correct order.""" |
|
38
|
|
|
columns = [ColumnDT(User.id, ), ColumnDT(User.name)] |
|
39
|
|
|
|
|
40
|
|
|
query = session.query().select_from(User) |
|
41
|
|
|
|
|
42
|
|
|
# Descending |
|
43
|
|
|
params = create_dt_params(columns, order=[{"column": 1, "dir": "desc"}]) |
|
44
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
45
|
|
|
res = rowTable.output_result() |
|
46
|
|
|
|
|
47
|
|
|
assert res['data'][0]['1'] == 'zzz_User' |
|
48
|
|
|
|
|
49
|
|
|
# Ascending |
|
50
|
|
|
params = create_dt_params(columns, order=[{"column": 1, "dir": "asc"}]) |
|
51
|
|
|
|
|
52
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
53
|
|
|
res = rowTable.output_result() |
|
54
|
|
|
|
|
55
|
|
|
assert res['data'][0]['1'] == '000_User' |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
@pytest.mark.usefixtures("fixtures_ordering") |
|
59
|
|
|
def test_ordering_nulls(session): |
|
60
|
|
|
"""Test if it returns a list with the correct nulls order.""" |
|
61
|
|
|
columns = [ |
|
62
|
|
|
ColumnDT(User.id, ), |
|
63
|
|
|
ColumnDT(User.name), |
|
64
|
|
|
ColumnDT(Address.description, nulls_order='nullsfirst'), |
|
65
|
|
|
ColumnDT(User.created_at) |
|
66
|
|
|
] |
|
67
|
|
|
|
|
68
|
|
|
query = session.query().select_from(User).join(Address) |
|
69
|
|
|
|
|
70
|
|
|
# NULLS FIRST |
|
71
|
|
|
params = create_dt_params(columns, order=[{"column": 2, "dir": "desc"}]) |
|
72
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
73
|
|
|
res = rowTable.output_result() |
|
74
|
|
|
|
|
75
|
|
|
if 'error' in res: |
|
76
|
|
|
# sqlite3 doesn't support nulls ordering |
|
77
|
|
|
assert 'sqlite3.OperationalError) near "NULLS"' in res['error'] |
|
78
|
|
|
|
|
79
|
|
|
columns = [ |
|
80
|
|
|
ColumnDT(User.id, ), |
|
81
|
|
|
ColumnDT(User.name), |
|
82
|
|
|
ColumnDT(Address.description, nulls_order='nullslast'), |
|
83
|
|
|
ColumnDT(User.created_at) |
|
84
|
|
|
] |
|
85
|
|
|
|
|
86
|
|
|
# NULLS LAST |
|
87
|
|
|
params = create_dt_params(columns, order=[{"column": 2, "dir": "asc"}]) |
|
88
|
|
|
|
|
89
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
90
|
|
|
res = rowTable.output_result() |
|
91
|
|
|
|
|
92
|
|
|
if 'error' in res: |
|
93
|
|
|
# sqlite3 doesn't support nulls ordering |
|
94
|
|
|
assert 'sqlite3.OperationalError) near "NULLS"' in res['error'] |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
@pytest.mark.usefixtures("fixtures_ordering") |
|
98
|
|
|
def test_ordering_relation(session): |
|
99
|
|
|
"""Test if it returns a list when ordering a foreign key.""" |
|
100
|
|
|
columns = [ |
|
101
|
|
|
ColumnDT(User.id, ), |
|
102
|
|
|
ColumnDT(User.name), |
|
103
|
|
|
ColumnDT(Address.description), |
|
104
|
|
|
ColumnDT(User.created_at) |
|
105
|
|
|
] |
|
106
|
|
|
|
|
107
|
|
|
query = session.query().select_from(User).join(Address) |
|
108
|
|
|
|
|
109
|
|
|
# Descending |
|
110
|
|
|
params = create_dt_params(columns, order=[{"column": 2, "dir": "desc"}]) |
|
111
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
112
|
|
|
res = rowTable.output_result() |
|
113
|
|
|
|
|
114
|
|
|
assert res['data'][0]['1'] == 'UserLastAddress' |
|
115
|
|
|
assert res['data'][0]['2'] == 'zzz_Address' |
|
116
|
|
|
|
|
117
|
|
|
columns = [ |
|
118
|
|
|
ColumnDT(User.id, ), |
|
119
|
|
|
ColumnDT(User.name), |
|
120
|
|
|
ColumnDT(Address.description), |
|
121
|
|
|
ColumnDT(User.created_at) |
|
122
|
|
|
] |
|
123
|
|
|
|
|
124
|
|
|
# Ascending |
|
125
|
|
|
params = create_dt_params(columns, order=[{"column": 2, "dir": "asc"}]) |
|
126
|
|
|
|
|
127
|
|
|
rowTable = DataTables(params, query, columns) |
|
|
|
|
|
|
128
|
|
|
res = rowTable.output_result() |
|
129
|
|
|
|
|
130
|
|
|
assert res['data'][0]['1'] == 'UserFirstAddress' |
|
131
|
|
|
assert res['data'][0]['2'] == '000_Address' |
|
132
|
|
|
|
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.