Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Coverage for 'gcloud._apitools.stream_slice' #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions gcloud/_apitools/stream_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ class StreamSlice(object):
"""Provides a slice-like object for streams."""

def __init__(self, stream, max_bytes):
self.__stream = stream
self.__remaining_bytes = max_bytes
self.__max_bytes = max_bytes
self._stream = stream
self._remaining_bytes = max_bytes
self._max_bytes = max_bytes

def __str__(self):
def __repr__(self):
return 'Slice of stream %s with %s/%s bytes not yet read' % (
self.__stream, self.__remaining_bytes, self.__max_bytes)
self._stream, self._remaining_bytes, self._max_bytes)

def __len__(self):
return self.__max_bytes
return self._max_bytes

def __nonzero__(self):
# For 32-bit python2.x, len() cannot exceed a 32-bit number; avoid
# accidental len() calls from httplib in the form of "if this_object:".
return bool(self.__max_bytes)
return bool(self._max_bytes)

@property
def length(self):
# For 32-bit python2.x, len() cannot exceed a 32-bit number.
return self.__max_bytes
return self._max_bytes

def read(self, size=None): # pylint: disable=missing-docstring
"""Read at most size bytes from this slice.
Expand All @@ -50,15 +50,15 @@ def read(self, size=None): # pylint: disable=missing-docstring

"""
if size is not None:
read_size = min(size, self.__remaining_bytes)
read_size = min(size, self._remaining_bytes)
else:
read_size = self.__remaining_bytes
data = self.__stream.read(read_size)
read_size = self._remaining_bytes
data = self._stream.read(read_size)
if read_size > 0 and not data:
raise exceptions.StreamExhausted(
'Not enough bytes in stream; expected %d, exhausted '
'after %d' % (
self.__max_bytes,
self.__max_bytes - self.__remaining_bytes))
self.__remaining_bytes -= len(data)
self._max_bytes,
self._max_bytes - self._remaining_bytes))
self._remaining_bytes -= len(data)
return data
69 changes: 69 additions & 0 deletions gcloud/_apitools/test_stream_slice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# pylint: skip-file
import unittest2


class Test_StreamSlice(unittest2.TestCase):

def _getTargetClass(self):
from gcloud._apitools.stream_slice import StreamSlice
return StreamSlice

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_ctor(self):
from io import BytesIO
CONTENT = b'CONTENT GOES HERE'
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
self.assertTrue(stream_slice._stream is stream)
self.assertEqual(stream_slice._remaining_bytes, MAXSIZE)
self.assertEqual(stream_slice._max_bytes, MAXSIZE)
self.assertEqual(len(stream_slice), MAXSIZE)
self.assertEqual(stream_slice.length, MAXSIZE)

def test___nonzero___empty(self):
from io import BytesIO
CONTENT = b''
MAXSIZE = 0
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
self.assertFalse(stream_slice)

def test___nonzero___nonempty(self):
from io import BytesIO
CONTENT = b'CONTENT GOES HERE'
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
self.assertTrue(stream_slice)

def test_read_exhausted(self):
from io import BytesIO
from gcloud._apitools.exceptions import StreamExhausted
CONTENT = b''
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
with self.assertRaises(StreamExhausted):
stream_slice.read()

def test_read_implicit_size(self):
from io import BytesIO
CONTENT = b'CONTENT GOES HERE'
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
self.assertEqual(stream_slice.read(), CONTENT[:MAXSIZE])
self.assertEqual(stream_slice._remaining_bytes, 0)

def test_read_explicit_size(self):
from io import BytesIO
CONTENT = b'CONTENT GOES HERE'
MAXSIZE = 4
SIZE = 3
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
self.assertEqual(stream_slice.read(SIZE), CONTENT[:SIZE])
self.assertEqual(stream_slice._remaining_bytes, MAXSIZE - SIZE)