Completed
Pull Request — master (#110)
by Michel
01:13
created

tests.test_ordering.OrderingTest.test_ordering()   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nop 1
dl 0
loc 24
rs 9.65
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
    if not res['data'][0]['1'] == 'zzz_User':
48
        raise AssertionError()
49
50
    # Ascending
51
    params = create_dt_params(columns, order=[{"column": 1, "dir": "asc"}])
52
53
    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...
54
    res = rowTable.output_result()
55
56
    if not res['data'][0]['1'] == '000_User':
57
        raise AssertionError()
58
59
60
@pytest.mark.usefixtures("fixtures_ordering")
61
def test_ordering_nulls(session):
62
    """Test if it returns a list with the correct nulls order."""
63
    columns = [
64
        ColumnDT(User.id, ),
65
        ColumnDT(User.name),
66
        ColumnDT(Address.description, nulls_order='nullsfirst'),
67
        ColumnDT(User.created_at)
68
    ]
69
70
    query = session.query().select_from(User).join(Address)
71
72
    # NULLS FIRST
73
    params = create_dt_params(columns, order=[{"column": 2, "dir": "desc"}])
74
    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...
75
    res = rowTable.output_result()
76
77
    if 'error' in res:
78
        # sqlite3 doesn't support nulls ordering
79
        if 'sqlite3.OperationalError) near "NULLS"' not in res['error']:
80
            raise AssertionError()
81
82
    columns = [
83
        ColumnDT(User.id, ),
84
        ColumnDT(User.name),
85
        ColumnDT(Address.description, nulls_order='nullslast'),
86
        ColumnDT(User.created_at)
87
    ]
88
89
    # NULLS LAST
90
    params = create_dt_params(columns, order=[{"column": 2, "dir": "asc"}])
91
92
    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...
93
    res = rowTable.output_result()
94
95
    if 'error' in res:
96
        # sqlite3 doesn't support nulls ordering
97
        if 'sqlite3.OperationalError) near "NULLS"' not in res['error']:
98
            raise AssertionError()
99
100
101
@pytest.mark.usefixtures("fixtures_ordering")
102
def test_ordering_relation(session):
103
    """Test if it returns a list when ordering a foreign key."""
104
    columns = [
105
        ColumnDT(User.id, ),
106
        ColumnDT(User.name),
107
        ColumnDT(Address.description),
108
        ColumnDT(User.created_at)
109
    ]
110
111
    query = session.query().select_from(User).join(Address)
112
113
    # Descending
114
    params = create_dt_params(columns, order=[{"column": 2, "dir": "desc"}])
115
    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...
116
    res = rowTable.output_result()
117
118
    if not res['data'][0]['1'] == 'UserLastAddress':
119
        raise AssertionError()
120
    if not res['data'][0]['2'] == 'zzz_Address':
121
        raise AssertionError()
122
123
    columns = [
124
        ColumnDT(User.id, ),
125
        ColumnDT(User.name),
126
        ColumnDT(Address.description),
127
        ColumnDT(User.created_at)
128
    ]
129
130
    # Ascending
131
    params = create_dt_params(columns, order=[{"column": 2, "dir": "asc"}])
132
133
    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...
134
    res = rowTable.output_result()
135
136
    if not res['data'][0]['1'] == 'UserFirstAddress':
137
        raise AssertionError()
138
    if not res['data'][0]['2'] == '000_Address':
139
        raise AssertionError()
140