Exemple #1
0
def test_range_wrapper_seeks(force_seek):
    """
    Given a file descriptor, it adjust the offset of the read to specified
    offset using force_seek().
    """
    fd = mock.Mock()
    offset = length = 20
    mod.RangeWrapper(fd, offset, length)
    force_seek.assert_called_once_with(fd, offset, mod.RangeWrapper.chunk)
Exemple #2
0
def test_range_wrapper_read_with_size():
    """
    Given file descriptor and length, when calling read() with size that is
    less than length, the descriptor's read() method is called with the
    specified size.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    ret.read(10)
    fd.read.assert_called_once_with(10)
Exemple #3
0
def test_range_wrapper_read_on_closed_descriptor():
    """
    Given closed file descriptor, when read() method is invoked, ValueError is
    raised.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    ret.close()
    with pytest.raises(ValueError):
        ret.read()
Exemple #4
0
def test_range_wrapper_close_fails_silently():
    """
    Given file descriptor with no close() method, when close() method is
    called, it fails silently.
    """
    fd = mock.Mock()
    del fd.close
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    ret.close()
    assert ret.fd is None
Exemple #5
0
def test_range_wrapper_read_without_size():
    """
    Given file descriptor and length, when read() method is invoked without
    arguments, the file descriptor's read() method is invoked with ``length``
    as argument and data read from descriptor is returned.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    data = ret.read()
    fd.read.assert_called_once_with(length)
    assert data == fd.read.return_value
Exemple #6
0
def test_range_wrapper_close():
    """
    Given file descriptor, when close() method is called, it closes the file
    descriptor itself and clears the reference to file descriptor.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    assert ret.fd is fd
    ret.close()
    assert ret.fd is None
    assert fd.close.called
Exemple #7
0
def test_range_wrapper():
    """
    Given a file descriptor, offset, and length, returns a file-like object
    that provides read() and close() methods.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    assert hasattr(ret, 'read')
    assert hasattr(ret.read, '__call__')
    assert hasattr(ret, 'close')
    assert hasattr(ret.close, '__call__')
Exemple #8
0
def test_range_wrapper_multiple_reads():
    """
    Given file descriptor and length, when calling read() multiple times with
    size that is less than length, file descriptor's read() method is invoked
    multiple times until sum of all sizes is larger than length and read()
    method returns empty string once that limit is reached.
    """
    fd = mock.Mock()
    offset = length = 20
    ret = mod.RangeWrapper(fd, offset, length)
    data = ret.read(10)
    assert data == fd.read.return_value
    data = ret.read(10)
    assert data == fd.read.return_value
    data = ret.read(10)
    assert data == ''
    data = ret.read(10)
    assert data == ''
    fd.read.assert_has_calls([mock.call(10), mock.call(10)])