def test_offset_objects(user: User, action: Action) -> None:
    for i in range(10):
        sampledb.logic.objects.create_object(
            action_id=action.id,
            data={'name': {
                '_type': 'text',
                'text': str(i)
            }},
            user_id=user.id)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()))
    assert len(objects) == 10
    for i, object in enumerate(objects, start=0):
        assert object.data['name']['text'] == str(i)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()),
        offset=6)
    assert len(objects) == 4
    for i, object in enumerate(objects, start=6):
        assert object.data['name']['text'] == str(i)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()),
        limit=3,
        offset=6)
    assert len(objects) == 3
    for i, object in enumerate(objects, start=6):
        assert object.data['name']['text'] == str(i)
Example #2
0
def test_order_by_object_id(user: User, action: Action) -> None:
    sampledb.logic.objects.create_object(
        action_id=action.id,
        data={'name': {
            '_type': 'text',
            'text': 'Name'
        }},
        user_id=user.id)
    sampledb.logic.objects.create_object(
        action_id=action.id,
        data={'name': {
            '_type': 'text',
            'text': 'Name'
        }},
        user_id=user.id)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()))
    assert objects[0].id <= objects[1].id

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.descending(object_sorting.object_id()))
    assert objects[0].id >= objects[1].id
def test_limit_objects(user: User, action: Action) -> None:
    for i in range(10):
        sampledb.logic.objects.create_object(
            action_id=action.id,
            data={'name': {
                '_type': 'text',
                'text': str(i)
            }},
            user_id=user.id)

    num_objects_found = []

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()),
        num_objects_found=num_objects_found)
    assert len(objects) == 10
    assert num_objects_found[0] == 10
    for i, object in enumerate(objects):
        assert object.data['name']['text'] == str(i)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()),
        limit=4,
        num_objects_found=num_objects_found)
    assert len(objects) == 4
    assert num_objects_found[0] == 10
    for i, object in enumerate(objects):
        assert object.data['name']['text'] == str(i)

    objects = sampledb.logic.objects.get_objects(
        filter_func=lambda data: True,
        sorting_func=object_sorting.ascending(object_sorting.object_id()),
        limit=20,
        num_objects_found=num_objects_found)
    assert len(objects) == 10
    assert num_objects_found[0] == 10
    for i, object in enumerate(objects):
        assert object.data['name']['text'] == str(i)