Skip to content

Commit 7ba3303

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate unit/test_utils.py from unittest to pytest (#129)
* Migrate unit/test_utils.py from unittest to pytest * Removed unused import and added yield and tearDown to apply_fixtures function * Made changes to fix CI travis issue
1 parent d4541b6 commit 7ba3303

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

test/unit/test_utils.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
# language governing permissions and limitations under the License.
1414
"""Test suite for aws_encryption_sdk.internal.utils"""
1515
import io
16-
import unittest
1716

1817
import pytest
19-
import six
2018
from mock import MagicMock, patch, sentinel
2119

2220
import aws_encryption_sdk.identifiers
@@ -44,8 +42,9 @@ def test_prep_stream_data_wrap(source):
4442
assert_prepped_stream_identity(test, io.BytesIO)
4543

4644

47-
class TestUtils(unittest.TestCase):
48-
def setUp(self):
45+
class TestUtils(object):
46+
@pytest.fixture(autouse=True)
47+
def apply_fixtures(self):
4948
# Set up mock key provider and keys
5049
self.mock_key_provider_1 = MasterKeyInfo(provider_id="adijoasijfoi", key_info=b"asoiwef8q34")
5150
self.mock_raw_data_key_1_bytes = b"asioufhaw9eruhtg"
@@ -139,49 +138,44 @@ def setUp(self):
139138
)
140139
self.mock_aws_encryption_sdk_instance.decrypt.return_value = VALUES["data_key"]
141140
self.mock_aws_encryption_sdk_instance.encrypt.return_value = VALUES["encrypted_data_key"]
142-
143-
def tearDown(self):
141+
yield
142+
# Run tearDown
144143
self.mock_urandom_patcher.stop()
145144

146145
def test_validate_frame_length_negative_frame_length(self):
147146
"""Validate that the validate_frame_length function
148147
behaves as expected when supplied with a
149148
negative frame length.
150149
"""
151-
with six.assertRaisesRegex(
152-
self,
153-
SerializationError,
154-
"Frame size must be a non-negative multiple of the block size of the crypto algorithm: *",
155-
):
150+
with pytest.raises(SerializationError) as excinfo:
156151
aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=-1, algorithm=self.mock_algorithm)
152+
excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *")
157153

158154
def test_validate_frame_length_invalid_frame_length(self):
159155
"""Validate that the validate_frame_length function
160156
behaves as expected when supplied with an
161157
invalid frame length.
162158
"""
163-
with six.assertRaisesRegex(
164-
self,
165-
SerializationError,
166-
"Frame size must be a non-negative multiple of the block size of the crypto algorithm: *",
167-
):
159+
with pytest.raises(SerializationError) as excinfo:
168160
aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=1, algorithm=self.mock_algorithm)
161+
excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *")
169162

170163
def test_validate_frame_length_too_large(self):
171164
"""Validate that the validate_frame_length function
172165
behaves as expected when supplied with a
173166
frame length which is too large.
174167
"""
175-
with six.assertRaisesRegex(self, SerializationError, "Frame size too large: *"):
168+
with pytest.raises(SerializationError) as excinfo:
176169
aws_encryption_sdk.internal.utils.validate_frame_length(
177170
frame_length=MAX_FRAME_SIZE + 1, algorithm=self.mock_algorithm
178171
)
172+
excinfo.match("Frame size too large: *")
179173

180174
def test_message_id(self):
181175
"""Validate that the message_id function behaves as expected."""
182176
test = aws_encryption_sdk.internal.utils.message_id()
183177
self.mock_urandom.assert_called_once_with(MESSAGE_ID_LENGTH)
184-
self.assertEqual(test, sentinel.random)
178+
assert test == sentinel.random
185179

186180
def test_get_aad_content_string_no_framing(self):
187181
"""Validate that the get_aad_content_string function behaves
@@ -214,8 +208,9 @@ def test_get_aad_content_string_framing_bad_type(self):
214208
"""Validate that the get_aad_content_string function behaves as
215209
expected when called with an unknown content type.
216210
"""
217-
with six.assertRaisesRegex(self, UnknownIdentityError, "Unhandled content type"):
211+
with pytest.raises(UnknownIdentityError) as excinfo:
218212
aws_encryption_sdk.internal.utils.get_aad_content_string(-1, False)
213+
excinfo.match("Unhandled content type")
219214

220215
def test_prepare_data_keys(self):
221216
mock_encryption_dk = DataKey(
@@ -265,9 +260,8 @@ def test_source_data_key_length_check_invalid(self):
265260
mock_algorithm.kdf_input_len = 5
266261
mock_data_key = MagicMock()
267262
mock_data_key.data_key = "1234"
268-
with six.assertRaisesRegex(
269-
self, InvalidDataKeyError, "Invalid Source Data Key length 4 for algorithm required: 5"
270-
):
263+
with pytest.raises(InvalidDataKeyError) as excinfo:
271264
aws_encryption_sdk.internal.utils.source_data_key_length_check(
272265
source_data_key=mock_data_key, algorithm=mock_algorithm
273266
)
267+
excinfo.match("Invalid Source Data Key length 4 for algorithm required: 5")

0 commit comments

Comments
 (0)