def test_wrong_page_size(self, session, page_number, page_size):
        query = session.query(User)

        with pytest.raises(InvalidPage) as err:
            apply_pagination(query, page_number, page_size)

        expected_error = 'Page size should not be negative: {}'.format(
            page_size)
        assert error_value(err) == expected_error
    def test_invalid_direction(self, session):
        query = session.query(User)
        order_by = [{'field': 'name', 'direction': 'invalid_direction'}]

        with pytest.raises(BadSortFormat) as err:
            apply_sort(query, order_by)

        expected_error = 'Direction `invalid_direction` not valid.'
        assert expected_error == error_value(err)
    def test_direction_not_provided(self, session):
        query = session.query(User)
        order_by = [{'field': 'name'}]

        with pytest.raises(BadSortFormat) as err:
            apply_sort(query, order_by)

        expected_error = '`field` and `direction` are mandatory attributes.'
        assert expected_error == error_value(err)
    def test_wrong_sort_format(self, session, sort):
        query = session.query(User)
        order_by = [sort]

        with pytest.raises(BadSortFormat) as err:
            apply_sort(query, order_by)

        expected_error = 'Sort spec `{}` should be a dictionary.'.format(sort)
        assert expected_error == error_value(err)
    def test_wrong_page_number_with_no_results(self, session, page_number,
                                               page_size):
        query = session.query(User)

        with pytest.raises(InvalidPage) as err:
            apply_pagination(query, page_number, page_size)

        expected_error = 'Page number should be positive: {}'.format(
            page_number)
        assert error_value(err) == expected_error
    def test_invalid_field(self, session):
        query = session.query(User)
        order_by = [{'field': 'invalid_field', 'direction': 'asc'}]

        with pytest.raises(FieldNotFound) as err:
            apply_sort(query, order_by)

        expected_error = (
            "Model <class 'orm.orm_data_access.models.User'> has no column `invalid_field`."
        )
        assert expected_error == error_value(err)