def test_sort_equal(): orders = ( Order('a', SortDirection.DESC), Order('b', SortDirection.ASC), Order('c', SortDirection.DESC), ) sort_1 = Sort(*orders) sort_2 = Sort(*orders) assert sort_1 == sort_2
def test_sort_desc(): orders = ( Order('a', SortDirection.ASC), Order('b', SortDirection.DESC), Order('c', SortDirection.ASC), ) sort = Sort(*orders) new_sort = sort.desc() assert all(order.direction == SortDirection.DESC for order in new_sort.orders)
def test_sort_hash(): orders = ( Order('a', SortDirection.DESC), Order('b', SortDirection.ASC), Order('c', SortDirection.DESC), ) sort_1 = Sort(*orders) sort_2 = Sort(*orders) sort_3 = Sort() sort_set = {sort_1, sort_2, sort_3} assert len(sort_set) == 2
def test_sort_and(): orders_1 = ( Order('a', SortDirection.DESC), Order('b'), ) orders_2 = ( Order('c'), Order('d', SortDirection.ASC), ) sort_1 = Sort(*orders_1) sort_2 = Sort(*orders_2) new_sort = sort_1.and_(sort_2) assert new_sort.orders == orders_1 + orders_2
def test_sort_orders(): orders = ( Order('a', SortDirection.DESC), Order('b'), ) sort = Sort(*orders) assert sort.orders == orders
def test_repr_sort(): sort = Sort( Order('foo', SortDirection.ASC), Order('bar', SortDirection.DESC), ) assert repr(sort) == "Sort('foo,-bar')"
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_fails_for_zero_fields(): with pytest.raises(ValueError, match='Specify at least one field.'): Sort.by()
def test_sort_not_equal_to_int(): sort = Sort() assert sort != 0
def test_empty_sort_orders(): sort = Sort() assert sort.orders == ()
def test_sort_by(): fields = ('a', 'b') orders = tuple(Order(field) for field in fields) sort = Sort.by(*fields) assert sort.orders == orders
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
def parse_sort(str_sort: Optional[str]) -> Optional[Sort]: if not str_sort: return None sort_parts = str_sort.split(',') orders = (parse_order(sort_part) for sort_part in sort_parts) return Sort(*orders)