def test_dict_context(info_and_user_query): info, user_query = info_and_user_query info.context = {} filters = {} with pytest.warns(None) as record: UserFilter.filter(info, user_query, filters) if record: pytest.fail('No warnings expected!')
def test_slots_context(info_and_user_query): info, user_query = info_and_user_query class Context: __slots__ = () info.context = Context() filters = {} with pytest.warns(RuntimeWarning): UserFilter.filter(info, user_query, filters)
def test_object_context(info_and_user_query): info, user_query = info_and_user_query class Context: pass info.context = Context() filters = {} with pytest.warns(None) as record: UserFilter.filter(info, user_query, filters) if record: pytest.fail('No warnings expected!')
def test_empty_filters_query(info_and_user_query): info, user_query = info_and_user_query query = UserFilter.filter(info, user_query, {}) where_clause = query.whereclause assert where_clause is None
def test_enum(info_and_user_query): info, user_query = info_and_user_query filters = {'status': 'ONLINE'} query = UserFilter.filter(info, user_query, filters) ok = '"user".status = :status_1' where_clause = str(query.whereclause) assert where_clause == ok
def test_filters(info_and_user_query): info, user_query = info_and_user_query filters = {'username_ilike': '%user%', 'balance_gt': 20} query = UserFilter.filter(info, user_query, filters) ok = ('lower("user".username) LIKE lower(:username_1) ' 'AND "user".balance > :balance_1') where_clause = str(query.whereclause) assert where_clause == ok
def test_enum(info_and_user_query): info, user_query = info_and_user_query filters = {'status': models.StatusEnum.online.value} query = UserFilter.filter(info, user_query, filters) where_clause = query.whereclause ok = '"user".status = :status_1' assert str(where_clause) == ok assert where_clause.right.effective_value == models.StatusEnum.online
def test_complex_filters(info_and_user_query): info, user_query = info_and_user_query filters = { 'is_admin': False, 'or': [ { 'and': [ { 'username_ilike': '%loki%' }, { 'balance_range': { 'begin': 500, 'end': 1000 } }, { 'is_moderator': True }, ] }, { 'or': [ { 'not': { 'is_active': True }, 'is_moderator': True }, { 'member_of_group': 'Valgalla', 'username_not_in': ['1'] }, {}, ] }, ], } query = UserFilter.filter(info, user_query, filters) ok = ('"user".username != :username_1 AND ' '(lower("user".username) LIKE lower(:username_2)' ' AND "user".balance BETWEEN :balance_1 AND :balance_2' ' AND is_moderator.id IS NOT NULL' ' OR "user".is_active != true' ' AND is_moderator.id IS NOT NULL' ' OR of_group.name = :name_1' ' AND "user".username NOT IN (:username_3))') where_clause = str(query.whereclause) assert where_clause == ok str_query = str(query) assert str_query.lower().count('join') == 4, str_query
def test_custom_filter(info_and_user_query): info, user_query = info_and_user_query filters = {'is_admin': True} query = UserFilter.filter(info, user_query, filters) ok = '"user".username = :username_1' where_clause = str(query.whereclause) assert where_clause == ok assert 'join' not in str(query).lower()
def test_wrong_filter(info_and_user_query): info, user_query = info_and_user_query filters = {'is_admin_true': True} with pytest.raises(KeyError, match='Field not found: is_admin_true'): UserFilter.filter(info, user_query, filters)