def test_read_data_offset_zero_offset():
    data = csv_parser.read_data_offset(10, 0)

    # Entry ids shouldn't change when the offset is zero.
    assert len(data) == 10
    assert data[0]['id'] == 0
    assert data[-1]['id'] == 9
def test_read_data_offset_large_limit():
    data = csv_parser.read_data_offset(110, 10)

    # All entries after 10 should be returned when the limit exceeds
    # the number of items left the dataset.
    assert len(data) == 90
    assert data[0]['id'] == 10
    assert data[-1]['id'] == 99
def test_read_data_offset_both_args():
    data = csv_parser.read_data_offset(5, 10)

    # Check the length and first and last ids when both args are
    # passed.
    assert len(data) == 5
    assert data[0]['id'] == 10
    assert data[-1]['id'] == 14
def test_read_data_offset_only_offset():
    data = csv_parser.read_data_offset(offset=10)

    # Check the length and first and last ids when only the offset is
    # given.
    assert len(data) == 90
    assert data[0]['id'] == 10
    assert data[-1]['id'] == 99
def test_read_data_offset_only_limit():
    data = csv_parser.read_data_offset(50)

    # Check the length and first and last ids when only the limit is
    # given.
    assert len(data) == 50
    assert data[0]['id'] == 0
    assert data[-1]['id'] == 49
def test_read_data_offset_types():
    data = csv_parser.read_data_offset()

    # This should give a list.
    assert type(data) == list

    # Each element should be a dictionary with three integer keys.
    for row in data:
        assert len(row.keys()) == 3

        for key in ('id', 'year', 'spots'):
            assert type(row[key]) == int
Пример #7
0
def spots_id(id):
    """Return a sunpots data row by id."""
    # Turn the id into an integer if it's non-negative, otherwise
    # return an error.
    try:
        # Might fail if it's not an integer.
        id = int(id)

        # Fail it also if it's below zero.
        if id < 0:
            raise ValueError
    except ValueError:
        return _make_error('invalid value provided for row id.'), 400

    data = csv_parser.read_data_offset(offset=id, limit=1)

    if len(data) == 1:
        return jsonify(data[0])
    else:
        return _make_error('row not found for row id.'), 404
Пример #8
0
def _handle_offset_case(limit, offset):
    # Converting the limit and offset to integers if they were
    # provided and checking if they are non-negative.
    try:
        # These conversions might fail if they aren't None or
        # integer strings.
        if limit is not None:
            limit = int(limit)
        if offset is not None:
            offset = int(offset)
    except ValueError:
        return _make_error(
            'limit and offset, if provided, must be integers.'
        ), 400
    else:
        if (limit is not None and limit < 0) or \
                (offset is not None and offset < 0):
            return _make_error(
                'limit and offset, if provided, must be non-negative'
            ), 400
        else:
            data = csv_parser.read_data_offset(limit=limit, offset=offset)
            return jsonify(data)
def test_read_data_offset_negative_offset_throws():
    # A ValueError is thrown when using a negative offset.
    with pytest.raises(ValueError):
        csv_parser.read_data_offset(1, -10)
def test_read_data_offset_zero_limit():
    data = csv_parser.read_data_offset(0, 10)

    # Nothing should be returned with a limit of zero.
    assert len(data) == 0
def test_read_data_offset_large_offset():
    data = csv_parser.read_data_offset(10, 150)

    # No entries should exist if the offset is after the end.
    assert len(data) == 0
def test_read_data_offset_no_args():
    data = csv_parser.read_data_offset()

    # The entire series should be returned when no args are passed.
    assert len(data) == 100