Skip to content

Commit 6a27b68

Browse files
authored
Use pytest.raises with match everywhere (#137)
* Use `pytest.raises` with match everywhere * Fix lint --------- Co-authored-by: Simon Beal <[email protected]>
1 parent 4d7ddc8 commit 6a27b68

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

s3torchconnector/tst/unit/test_s3dataset_common.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ def test_s3dataset_base_parse_s3_uri_success(uri, expected_bucket, expected_key)
5353
],
5454
)
5555
def test_s3dataset_base_parse_s3_uri_fail(uri, error_msg):
56-
with pytest.raises(ValueError) as error:
56+
with pytest.raises(ValueError, match=f"^{error_msg}$"):
5757
parse_s3_uri(uri)
58-
assert str(error.value) == error_msg
5958

6059

6160
@pytest.mark.parametrize(
@@ -79,13 +78,12 @@ def test_get_objects_from_prefix(prefix: str, keys: Sequence[str], expected_coun
7978

8079
def test_list_objects_for_bucket_invalid():
8180
mock_client = _create_mock_client_with_dummy_objects(TEST_BUCKET, [])
82-
with pytest.raises(S3Exception) as error:
81+
with pytest.raises(S3Exception, match="Service error: The bucket does not exist"):
8382
objects = get_objects_from_prefix(
8483
"s3://DIFFERENT_BUCKET",
8584
mock_client,
8685
)
8786
next(iter(objects))
88-
assert str(error.value) == "Service error: The bucket does not exist"
8987

9088

9189
@pytest.mark.parametrize(
@@ -117,9 +115,8 @@ def test_get_objects_from_uris_success(
117115
)
118116
def test_get_objects_from_uris_fail(uri, error_msg):
119117
mock_client = _create_mock_client_with_dummy_objects(TEST_BUCKET, [])
120-
with pytest.raises(ValueError) as error:
121-
objects = get_objects_from_uris(uri, mock_client)
122-
assert str(error.value) == error_msg
118+
with pytest.raises(ValueError, match=f"^{error_msg}$"):
119+
get_objects_from_uris(uri, mock_client)
123120

124121

125122
def _create_mock_client_with_dummy_objects(

s3torchconnector/tst/unit/test_s3reader.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ def test_s3reader_creation(object_info, get_stream):
7373
[(None, None), (None, ""), (None, TEST_KEY), ("", TEST_KEY)],
7474
)
7575
def test_s3reader_invalid_creation(bucket, key):
76-
with pytest.raises(ValueError) as error:
76+
with pytest.raises(ValueError, match="Bucket should be specified"):
7777
S3Reader(bucket, key)
78-
assert str(error.value) == "Bucket should be specified"
7978

8079

8180
@pytest.mark.parametrize(

s3torchconnectorclient/python/tst/integration/test_mountpoint_s3_integration.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,22 @@ def test_get_object_with_unpickled_client(sample_directory):
5454

5555
def test_get_object_invalid_bucket(sample_directory):
5656
client = MountpointS3Client(sample_directory.region, TEST_USER_AGENT_PREFIX)
57-
with pytest.raises(S3Exception) as error:
57+
with pytest.raises(S3Exception, match="Service error: The bucket does not exist"):
5858
next(
5959
client.get_object(
6060
f"{sample_directory.bucket}-{uuid.uuid4()}", sample_directory.prefix
6161
)
6262
)
63-
assert str(error.value) == "Service error: The bucket does not exist"
6463

6564

6665
def test_get_object_invalid_prefix(sample_directory):
6766
client = MountpointS3Client(sample_directory.region, TEST_USER_AGENT_PREFIX)
68-
with pytest.raises(S3Exception) as error:
67+
with pytest.raises(S3Exception, match="Service error: The key does not exist"):
6968
next(
7069
client.get_object(
7170
sample_directory.bucket, f"{sample_directory.prefix}-{uuid.uuid4()}"
7271
)
7372
)
74-
assert str(error.value) == "Service error: The key does not exist"
7573

7674

7775
def test_list_objects(image_directory):

s3torchconnectorclient/python/tst/unit/test_mountpoint_s3_client.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,24 @@ def test_get_object_bad_bucket():
6666
mock_client = MockMountpointS3Client(REGION, MOCK_BUCKET)
6767
client = mock_client.create_mocked_client()
6868

69-
try:
69+
with pytest.raises(S3Exception, match="Service error: The bucket does not exist"):
7070
client.get_object("does_not_exist", "foo")
71-
except S3Exception as e:
72-
assert str(e) == "Service error: The bucket does not exist"
7371

7472

7573
def test_get_object_none_bucket():
7674
mock_client = MockMountpointS3Client(REGION, MOCK_BUCKET)
7775
client = mock_client.create_mocked_client()
7876

79-
try:
77+
with pytest.raises(TypeError):
8078
client.get_object(None, "foo")
81-
except TypeError:
82-
pass
83-
else:
84-
raise AssertionError("Should raise TypeError")
8579

8680

8781
def test_get_object_bad_object():
8882
mock_client = MockMountpointS3Client(REGION, MOCK_BUCKET)
8983
client = mock_client.create_mocked_client()
9084

91-
try:
85+
with pytest.raises(S3Exception, match="Service error: The key does not exist"):
9286
client.get_object(MOCK_BUCKET, "does_not_exist")
93-
except S3Exception as e:
94-
assert str(e) == "Service error: The key does not exist"
9587

9688

9789
def test_get_object_iterates_once():
@@ -121,12 +113,8 @@ def test_get_object_throws_stop_iteration():
121113
pass
122114

123115
for _ in range(10):
124-
try:
116+
with pytest.raises(StopIteration):
125117
next(stream)
126-
except StopIteration:
127-
pass
128-
else:
129-
raise AssertionError("Should always throw StopIteration after stream ends")
130118

131119

132120
@pytest.mark.parametrize(
@@ -224,9 +212,8 @@ def test_put_object_no_multiple_close():
224212

225213
put_stream.write(b"")
226214
put_stream.close()
227-
with pytest.raises(S3Exception) as e:
215+
with pytest.raises(S3Exception, match="Cannot close object more than once"):
228216
put_stream.close()
229-
assert str(e.value) == "Cannot close object more than once"
230217

231218

232219
def test_put_object_no_write_after_close():
@@ -238,9 +225,8 @@ def test_put_object_no_write_after_close():
238225

239226
put_stream.write(b"")
240227
put_stream.close()
241-
with pytest.raises(S3Exception) as e:
228+
with pytest.raises(S3Exception, match="Cannot write to closed object"):
242229
put_stream.write(b"")
243-
assert str(e.value) == "Cannot write to closed object"
244230

245231

246232
def test_put_object_with_storage_class():

0 commit comments

Comments
 (0)