| 1 |  |  | import pytest | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | from sqlalchemy import func | 
            
                                                        
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | from datatables import ColumnDT, DataTables | 
            
                                                        
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | from .helpers import create_dt_params | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | from .models import Address, User | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | def test_fields_mdata(session): | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |     """Test if the result's data have mData set.""" | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |     columns = [ | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         ColumnDT(User.id, mData='ID'), | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         ColumnDT(User.name, mData='Username'), | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |         ColumnDT(Address.description, mData='Address'), | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |         ColumnDT(User.created_at, mData='Created at') | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |     ] | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |     query = session.query().select_from(User).join(Address) | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |     params = create_dt_params(columns) | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |     rowTable = DataTables(params, query, columns) | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     assert len(res['data']) == 3 | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |     assert 'ID' in res['data'][0] | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     assert 'Username' in res['data'][0] | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     assert 'Address' in res['data'][0] | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |     assert 'Created at' in res['data'][0] | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 32 |  |  | def test_fields_search_filters(session): | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |     """Test if the result's data are filtered after search.""" | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |     query = session.query() | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |     columns = [ | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         ColumnDT(User.id, search_method='numeric'), | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         ColumnDT(User.name, search_method='string_contains'), | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         ColumnDT(User.birthday, search_method='date') | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     ] | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     user = session.query(User).filter(User.id == 4).one() | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |     params = create_dt_params(columns) | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     params['columns[0][search][value]'] = '=4' | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |     params['columns[1][search][value]'] = user.name | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     params['columns[2][search][value]'] = '>1965-02-02' | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |     params['columns[2][search][value]'] = '<=99' | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     rowTable = DataTables(params, query, columns) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     assert len(res['data']) == 1 | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 |  |  | def test_calculating_age_on_the_fly(session): | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     """Test if the result's have a custom field.""" | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     query = session.query().filter(User.id > 5) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     columns = [ | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         ColumnDT(User.id, search_method='numeric'), | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         ColumnDT(User.name, search_method='string_contains'), | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         ColumnDT(User.birthday, search_method='date'), | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         ColumnDT(func.datetime('now') - User.birthday, search_method='numeric') | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     ] | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     params = create_dt_params(columns) | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     rowTable = DataTables(params, query, columns) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     assert len(res['data']) == 10 | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | @pytest.fixture(scope="function") | 
            
                                                        
            
                                    
            
            
                | 74 |  |  | def fixtures_filed_filtering(session): | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     user51 = User(name='User 51') | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     user52 = User(name='User 52') | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     session.add(user51) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     session.add(user52) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |     yield | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     session.delete(user51) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     session.delete(user52) | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication | @pytest.mark.usefixtures("fixtures_filed_filtering") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | def test_fields_filtering(session): | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     """Test if result's are filtered from global search field.""" | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     columns = [ColumnDT(User.id, ), ColumnDT(User.name)] | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     query = session.query().select_from(User) | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     params = create_dt_params(columns, search='51') | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     rowTable = DataTables(params, query, columns) | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |     assert len(res['data']) == 1 | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     assert res['recordsTotal'] == '52' | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     assert res['recordsFiltered'] == '1' | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |     assert res['data'][0]['1'] == 'User 51' | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  | @pytest.fixture(scope="function") | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | def fixtures_fields_global_search_filtering_with_regex(session): | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |     user51 = User(name='Run To') | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |     user52 = User(name='Feeeeear Of') | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |     session.add(user51) | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     session.add(user52) | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |     yield | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |     session.delete(user51) | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |     session.delete(user52) | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  | @pytest.mark.usefixtures("fixtures_fields_global_search_filtering_with_regex") | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | def test_fields_global_search_filtering_with_regex(session): | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     """Test if result's are filtered from global search field.""" | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     columns = [ColumnDT(User.id, ), ColumnDT(User.name)] | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     query = session.query().select_from(User) | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     params = create_dt_params(columns, search='Fe*ar') | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |     params['search[regex]'] = 'true' | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |     rowTable = DataTables(params, query, columns, allow_regex_searches=True) | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |     if 'error' in res: | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |         # unfortunately sqlite doesn't support regexp out of the box' | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |         assert 'no such function: REGEXP' in res['error'] | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |     else: | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |         assert len(res['data']) == 1 | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |         assert res['recordsTotal'] == '1' | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |         assert res['recordsFiltered'] == '1' | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |         assert res['data'][0]['1'] == 'Feeeeear Of' | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 145 |  |  | @pytest.fixture(scope="function") | 
            
                                                        
            
                                    
            
            
                | 146 |  |  | def fixtures_column_not_searchable(session): | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |     user51 = User(name='User 51') | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |     session.add(user51) | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |     yield | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |     session.delete(user51) | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |     session.commit() | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 158 |  |  | @pytest.mark.usefixtures("fixtures_column_not_searchable") | 
            
                                                        
            
                                    
            
            
                | 159 |  |  | def test_column_not_searchable(session): | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     """Test if result's are filtered from global search field.""" | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |     columns = [ | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |         ColumnDT(User.id, mData='ID'), | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |         ColumnDT(User.name, mData='Username', global_search=False) | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |     ] | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |     query = session.query().select_from(User) | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |     params = create_dt_params(columns, search='User 51') | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |     rowTable = DataTables(params, query, columns) | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |     res = rowTable.output_result() | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |     assert len(res['data']) == 0 | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |     assert res['recordsTotal'] == '51' | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |     assert res['recordsFiltered'] == '0' | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |