assert page_position == PagePosition() @pytest.mark.parametrize( ('query_string', 'expected_page_position'), ( ('limit=1&offset=3', PagePosition(1, 3)), ('limit=1', PagePosition(1)), ('limit=', PagePosition(None)), ('limit=0', PagePosition(None)), ('offset=3', PagePosition(None, 3)), ('', PagePosition(None, None)), ('offset=0', PagePosition(None, 0)), ('limit=10&offset=20&order_by=-id,name', PagePosition(10, 20, Sort.by('id').desc().and_(Sort.by('name')))), ('order_by=', PagePosition(None, None)), ), ) def test_resolve_argument_ok_in_page_position_argument_resolver( query_string, expected_page_position): @winter.web.pagination.order_by(['name', 'id', 'email', 'x']) def method(page_position: PagePosition): # pragma: no cover return page_position argument = method.get_argument('page_position') resolver = PagePositionArgumentResolver(allow_any_order_by_field=True) request = Mock(spec=DRFRequest) request.query_params = QueryDict(query_string)
def test_sort_by(): fields = ('a', 'b') orders = tuple(Order(field) for field in fields) sort = Sort.by(*fields) assert sort.orders == orders
def test_sort_by_fails_for_zero_fields(): with pytest.raises(ValueError, match='Specify at least one field.'): Sort.by()
from sqlalchemy import Integer from sqlalchemy import MetaData from sqlalchemy import Table from sqlalchemy import create_engine from sqlalchemy import select from winter.data.pagination import PagePosition from winter.data.pagination import Sort from winter_sqlalchemy import paginate @pytest.mark.parametrize( 'limit, offset, sort, expected_ids', [ (None, None, None, None), (None, None, Sort.by('id'), [1, 2, 3, 4, 5]), (None, None, Sort.by('id').desc(), [5, 4, 3, 2, 1]), (3, None, Sort.by('id'), [1, 2, 3]), (6, None, Sort.by('id'), [1, 2, 3, 4, 5]), (2, 2, Sort.by('id'), [3, 4]), (None, 2, Sort.by('id'), [3, 4, 5]), ], ) def test_paginate(id_database, limit, offset, sort, expected_ids): engine, table = id_database statement = paginate(select([table.c.id]), PagePosition(limit, offset, sort)) result = engine.execute(statement) ids = [row[0] for row in result] if expected_ids is None: assert len(ids) == 5