def test_should_update_first_for_after(self):
        cursor = get_cursor('foo', 123)
        filter_string1 = FilterString('first=1 rows=10')
        filter_string2 = get_filter_string_for_pagination(filter_string1,
                                                          after=cursor)

        self.assertEqual(str(filter_string2), 'rows=10 first=125')
    def test_should_update_first_for_before(self):
        cursor = get_cursor('foo', 123)
        filter_string1 = FilterString('first=1 rows=10')
        filter_string2 = get_filter_string_for_pagination(filter_string1,
                                                          before=cursor,
                                                          last=10)

        self.assertEqual(str(filter_string2), 'rows=10 first=114')
    def test_should_prefer_after_for_before(self):
        after = get_cursor('foo', 100)
        before = get_cursor('foo', 200)
        filter_string1 = FilterString('first=1 rows=10')
        filter_string2 = get_filter_string_for_pagination(filter_string1,
                                                          after=after,
                                                          first=5,
                                                          last=15,
                                                          before=before)

        self.assertEqual(str(filter_string2), 'rows=5 first=102')
示例#4
0
def get_filter_string_for_pagination(
    filter_string: FilterStringModel,
    *,
    first: int = None,
    last: int = None,
    after: str = None,
    before: str = None,
) -> FilterStringModel:
    if not filter_string:
        filter_string = FilterStringModel()

    if first is not None:
        filter_string = filter_string.remove_rows()
        filter_string = filter_string.add_rows(first)
    elif last is not None:
        filter_string = filter_string.remove_rows()
        filter_string = filter_string.add_rows(last)

    if after is not None:
        offset = get_offset_from_cursor(after)
        # the edge node index is zero based the.
        # the filter index with first is one based.
        # this adds one to the offset.
        # because we want get the node AFTER the passed one we need to add
        # two to the offset
        offset += 2

        filter_string = filter_string.remove_first()
        filter_string = filter_string.add_first(offset)

    elif before is not None and last is not None:
        offset = get_offset_from_cursor(before)
        # the edge node index is zero based the.
        # the filter index with first is one based.
        # therefore last must be subtracted by one.
        # to get the index of the first entity we need to subtract last from
        # the offset
        offset -= last - 1

        if offset <= 0:
            offset = 1

        filter_string = filter_string.remove_first()
        filter_string = filter_string.add_first(offset)

    return filter_string
    def test_should_update_rows_for_last(self):
        filter_string1 = FilterString('foo=1 rows=2')
        filter_string2 = get_filter_string_for_pagination(filter_string1,
                                                          last=10)

        self.assertEqual(str(filter_string2), 'foo=1 rows=10')
    def test_should_return_filter_string(self):
        filter_string1 = FilterString('foo=1 bar=2')
        filter_string2 = get_filter_string_for_pagination(filter_string1)

        self.assertEqual(str(filter_string2), 'foo=1 bar=2')
        self.assertIs(filter_string1, filter_string2)