Skip to content

Commit e232c32

Browse files
committed
convert TestEncryptionStream to pytest
1 parent 82af575 commit e232c32

File tree

1 file changed

+64
-33
lines changed

1 file changed

+64
-33
lines changed

test/unit/test_streaming_client_encryption_stream.py

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Unit test suite for aws_encryption_sdk.streaming_client._EncryptionStream"""
14+
import copy
1415
import io
1516
import unittest
1617

@@ -45,20 +46,23 @@ def _read_bytes(self, b):
4546
return self.config.mock_read_bytes
4647

4748

48-
class TestEncryptionStream(unittest.TestCase):
49-
def setUp(self):
50-
self.mock_source_stream = MagicMock()
51-
self.mock_source_stream.__class__ = io.IOBase
52-
self.mock_source_stream.tell.side_effect = (10, 500)
49+
class TestEncryptionStream(object):
5350

54-
self.mock_key_provider = MagicMock()
55-
self.mock_key_provider.__class__ = MasterKeyProvider
51+
def _mock_key_provider(self):
52+
mock_key_provider = MagicMock()
53+
mock_key_provider.__class__ = MasterKeyProvider
54+
return mock_key_provider
5655

57-
self.mock_line_length = MagicMock()
58-
self.mock_line_length.__class__ = int
56+
def _mock_source_stream(self):
57+
mock_source_stream = MagicMock()
58+
mock_source_stream.__class__ = io.IOBase
59+
mock_source_stream.tell.side_effect = (10, 500)
60+
return mock_source_stream
5961

60-
self.mock_source_length = MagicMock()
61-
self.mock_source_length.__class__ = int
62+
@pytest.fixture(autouse=True)
63+
def apply_fixtures(self):
64+
self.mock_key_provider = self._mock_key_provider()
65+
self.mock_source_stream = self._mock_source_stream()
6266

6367
def test_read_bytes_enforcement(self):
6468
class TestStream(_EncryptionStream):
@@ -67,19 +71,23 @@ class TestStream(_EncryptionStream):
6771
def _prep_message(self):
6872
pass
6973

70-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestStream"):
74+
with pytest.raises(TypeError) as excinfo:
7175
TestStream()
7276

77+
excinfo.match("Can't instantiate abstract class TestStream")
78+
7379
def test_prep_message_enforcement(self):
7480
class TestStream(_EncryptionStream):
7581
_config_class = MockClientConfig
7682

7783
def _read_bytes(self):
7884
pass
7985

80-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestStream"):
86+
with pytest.raises(TypeError) as excinfo:
8187
TestStream()
8288

89+
excinfo.match("Can't instantiate abstract class TestStream")
90+
8391
def test_config_class_enforcement(self):
8492
class TestStream(_EncryptionStream):
8593
def _read_bytes(self):
@@ -88,29 +96,32 @@ def _read_bytes(self):
8896
def _prep_message(self):
8997
pass
9098

91-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestStream"):
99+
with pytest.raises(TypeError) as excinfo:
92100
TestStream()
93101

102+
excinfo.match("Can't instantiate abstract class TestStream")
103+
94104
def test_new_with_params(self):
105+
mock_int_sentinel = MagicMock(__class__=int)
95106
mock_stream = MockEncryptionStream(
96107
source=self.mock_source_stream,
97108
key_provider=self.mock_key_provider,
98109
mock_read_bytes=sentinel.read_bytes,
99-
line_length=self.mock_line_length,
100-
source_length=self.mock_source_length,
110+
line_length=io.DEFAULT_BUFFER_SIZE,
111+
source_length=mock_int_sentinel,
101112
)
102113
assert mock_stream.config == MockClientConfig(
103114
source=self.mock_source_stream,
104115
key_provider=self.mock_key_provider,
105116
mock_read_bytes=sentinel.read_bytes,
106-
line_length=self.mock_line_length,
107-
source_length=self.mock_source_length,
117+
line_length=io.DEFAULT_BUFFER_SIZE,
118+
source_length=mock_int_sentinel,
108119
)
109120
assert mock_stream.bytes_read == 0
110121
assert mock_stream.output_buffer == b""
111122
assert not mock_stream._message_prepped
112123
assert mock_stream.source_stream is self.mock_source_stream
113-
assert mock_stream._stream_length is self.mock_source_length
124+
assert mock_stream._stream_length is mock_int_sentinel
114125
assert mock_stream.line_length == io.DEFAULT_BUFFER_SIZE
115126

116127
def test_new_with_config(self):
@@ -154,7 +165,8 @@ class CustomUnknownError(Exception):
154165
mock_stream = MockEncryptionStream(
155166
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
156167
)
157-
with self.assertRaises(CustomUnknownError):
168+
169+
with pytest.raises(CustomUnknownError):
158170
mock_stream.__exit__(None, None, None)
159171

160172
def test_stream_length(self):
@@ -173,9 +185,12 @@ def test_stream_length_unsupported(self):
173185
mock_stream = MockEncryptionStream(
174186
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
175187
)
176-
with six.assertRaisesRegex(self, aws_encryption_sdk.exceptions.NotSupportedError, "Unexpected exception!"):
188+
189+
with pytest.raises(aws_encryption_sdk.exceptions.NotSupportedError) as excinfo:
177190
mock_stream.stream_length # pylint: disable=pointless-statement
178191

192+
excinfo.match("Unexpected exception!")
193+
179194
def test_header_property(self):
180195
mock_prep_message = MagicMock()
181196
mock_stream = MockEncryptionStream(
@@ -205,21 +220,26 @@ def test_read_closed(self):
205220
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
206221
)
207222
mock_stream.close()
208-
with six.assertRaisesRegex(self, ValueError, "I/O operation on closed file"):
223+
224+
with pytest.raises(ValueError) as excinfo:
209225
mock_stream.read()
210226

211-
def test_read_b(self):
227+
excinfo.match("I/O operation on closed file")
228+
229+
@pytest.mark.parametrize('bytes_to_read', range(1, 11))
230+
def test_read_b(self, bytes_to_read):
212231
mock_stream = MockEncryptionStream(
213232
source=io.BytesIO(VALUES["data_128"]),
214233
key_provider=self.mock_key_provider,
215234
mock_read_bytes=sentinel.read_bytes,
216235
)
236+
data = b"1234567890"
217237
mock_stream._read_bytes = MagicMock()
218-
mock_stream.output_buffer = b"1234567890"
219-
test = mock_stream.read(5)
220-
mock_stream._read_bytes.assert_called_once_with(5)
221-
assert test == b"12345"
222-
assert mock_stream.output_buffer == b"67890"
238+
mock_stream.output_buffer = copy.copy(data)
239+
test = mock_stream.read(bytes_to_read)
240+
mock_stream._read_bytes.assert_called_once_with(bytes_to_read)
241+
assert test == data[:bytes_to_read]
242+
assert mock_stream.output_buffer == data[bytes_to_read:]
223243

224244
def test_read_all(self):
225245
mock_stream = MockEncryptionStream(
@@ -262,23 +282,32 @@ def test_writelines(self):
262282
mock_stream = MockEncryptionStream(
263283
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
264284
)
265-
with six.assertRaisesRegex(self, NotImplementedError, "writelines is not available for this object"):
285+
286+
with pytest.raises(NotImplementedError) as excinfo:
266287
mock_stream.writelines(None)
267288

289+
excinfo.match("writelines is not available for this object")
290+
268291
def test_write(self):
269292
mock_stream = MockEncryptionStream(
270293
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
271294
)
272-
with six.assertRaisesRegex(self, NotImplementedError, "write is not available for this object"):
295+
296+
with pytest.raises(NotImplementedError) as excinfo:
273297
mock_stream.write(None)
274298

299+
excinfo.match("write is not available for this object")
300+
275301
def test_seek(self):
276302
mock_stream = MockEncryptionStream(
277303
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
278304
)
279-
with six.assertRaisesRegex(self, NotImplementedError, "seek is not available for this object"):
305+
306+
with pytest.raises(NotImplementedError) as excinfo:
280307
mock_stream.seek(None)
281308

309+
excinfo.match("seek is not available for this object")
310+
282311
def test_readline(self):
283312
test_line = "TEST_LINE_AAAA"
284313
test_line_length = len(test_line)
@@ -319,7 +348,8 @@ def test_next_stream_closed(self):
319348
source=self.mock_source_stream, key_provider=self.mock_key_provider, mock_read_bytes=sentinel.read_bytes
320349
)
321350
mock_stream.close()
322-
with self.assertRaises(StopIteration):
351+
352+
with pytest.raises(StopIteration):
323353
mock_stream.next()
324354

325355
def test_next_source_stream_closed_and_buffer_empty(self):
@@ -328,7 +358,8 @@ def test_next_source_stream_closed_and_buffer_empty(self):
328358
)
329359
self.mock_source_stream.closed = True
330360
mock_stream.output_buffer = b""
331-
with self.assertRaises(StopIteration):
361+
362+
with pytest.raises(StopIteration):
332363
mock_stream.next()
333364

334365
@patch("aws_encryption_sdk.streaming_client._EncryptionStream.closed", new_callable=PropertyMock)

0 commit comments

Comments
 (0)