Passed
Branch v2 (18c5d8)
by Michel
02:43
created

OrderingTest.test_ordering_nulls()   A

Complexity

Conditions 3

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nop 1
dl 0
loc 36
rs 9.304
c 0
b 0
f 0
1
import pytest
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'pytest'
Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name rowTable does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
128
    res = rowTable.output_result()
129
130
    assert res['data'][0]['1'] == 'UserFirstAddress'
131
    assert res['data'][0]['2'] == '000_Address'
132