-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_stream_slice.py
More file actions
68 lines (58 loc) · 2.25 KB
/
test_stream_slice.py
File metadata and controls
68 lines (58 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import unittest2
class Test_StreamSlice(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.streaming.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 six.moves import http_client
CONTENT = b''
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
with self.assertRaises(http_client.IncompleteRead):
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)