From dc33612174383266415a006bd61d3828320502bd Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Wed, 8 May 2024 18:42:09 +0200 Subject: [PATCH 1/2] convert TestTranslateErrors to pytest --- test/unit/b2http/test_b2http.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/unit/b2http/test_b2http.py b/test/unit/b2http/test_b2http.py index 789d0426..739a4bf4 100644 --- a/test/unit/b2http/test_b2http.py +++ b/test/unit/b2http/test_b2http.py @@ -38,24 +38,24 @@ from ..test_base import TestBase -class TestTranslateErrors(TestBase): +class TestTranslateErrors: def test_ok(self): response = MagicMock() response.status_code = 200 actual = B2Http._translate_errors(lambda: response) - self.assertIs(response, actual) + assert response == actual def test_partial_content(self): response = MagicMock() response.status_code = 206 actual = B2Http._translate_errors(lambda: response) - self.assertIs(response, actual) + assert response == actual def test_b2_error(self): response = MagicMock() response.status_code = 503 response.content = b'{"status": 503, "code": "server_busy", "message": "busy"}' - with self.assertRaises(ServiceError): + with pytest.raises(ServiceError): B2Http._translate_errors(lambda: response) def test_broken_pipe(self): @@ -66,7 +66,7 @@ def fcn(): ) ) - with self.assertRaises(BrokenPipe): + with pytest.raises(BrokenPipe): B2Http._translate_errors(fcn) def test_unknown_host(self): @@ -77,14 +77,14 @@ def fcn(): ) ) - with self.assertRaises(UnknownHost): + with pytest.raises(UnknownHost): B2Http._translate_errors(fcn) def test_connection_error(self): def fcn(): raise requests.ConnectionError('a message') - with self.assertRaises(B2ConnectionError): + with pytest.raises(B2ConnectionError): B2Http._translate_errors(fcn) def test_connection_reset(self): @@ -94,14 +94,14 @@ class SysCallError(Exception): def fcn(): raise SysCallError('(104, ECONNRESET)') - with self.assertRaises(ConnectionReset): + with pytest.raises(ConnectionReset): B2Http._translate_errors(fcn) def test_unknown_error(self): def fcn(): raise Exception('a message') - with self.assertRaises(UnknownError): + with pytest.raises(UnknownError): B2Http._translate_errors(fcn) def test_too_many_requests(self): @@ -109,7 +109,7 @@ def test_too_many_requests(self): response.status_code = 429 response.headers = {'retry-after': 1} response.content = b'{"status": 429, "code": "Too Many requests", "message": "retry after some time"}' - with self.assertRaises(TooManyRequests): + with pytest.raises(TooManyRequests): B2Http._translate_errors(lambda: response) def test_invalid_json(self): @@ -129,7 +129,7 @@ def test_potential_s3_endpoint_passed_as_realm(self): response.content = b'' response.url = 'https://s3.us-west-000.backblazeb2.com' - with self.assertRaises(PotentialS3EndpointPassedAsRealm): + with pytest.raises(PotentialS3EndpointPassedAsRealm): B2Http._translate_errors(lambda: response) From 2b20921a5144b313a754552720f91e4496a4d421 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Wed, 8 May 2024 18:46:36 +0200 Subject: [PATCH 2/2] ensure b2sdk.v2 emits v2 BucketIdNotFound exc --- b2sdk/v2/b2http.py | 2 +- changelog.d/437.fixed.md | 1 + test/unit/b2http/test_b2http.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelog.d/437.fixed.md diff --git a/b2sdk/v2/b2http.py b/b2sdk/v2/b2http.py index 152457c8..e6a26e6a 100644 --- a/b2sdk/v2/b2http.py +++ b/b2sdk/v2/b2http.py @@ -17,7 +17,7 @@ # Overridden to retain old-style BadRequest exception in case of a bad bucket id class B2Http(v3.B2Http): @classmethod - def _translate_error(cls, fcn, post_params=None): + def _translate_errors(cls, fcn, post_params=None): try: return super()._translate_errors(fcn, post_params) except v3BucketIdNotFound as e: diff --git a/changelog.d/437.fixed.md b/changelog.d/437.fixed.md new file mode 100644 index 00000000..89941029 --- /dev/null +++ b/changelog.d/437.fixed.md @@ -0,0 +1 @@ +Ensure `b2sdk.v2.b2http` emits `b2sdk.v2.BucketIdNotFound` exception instead of `b2sdk._v3.BucketIdNotFound`. diff --git a/test/unit/b2http/test_b2http.py b/test/unit/b2http/test_b2http.py index 739a4bf4..9d5bff5f 100644 --- a/test/unit/b2http/test_b2http.py +++ b/test/unit/b2http/test_b2http.py @@ -132,6 +132,16 @@ def test_potential_s3_endpoint_passed_as_realm(self): with pytest.raises(PotentialS3EndpointPassedAsRealm): B2Http._translate_errors(lambda: response) + @pytest.mark.apiver(to_ver=2) + def test_bucket_id_not_found(self): + from b2sdk.v2.exception import BucketIdNotFound, v3BucketIdNotFound + + def fcn(): + raise v3BucketIdNotFound('bucket_id') + + with pytest.raises(BucketIdNotFound): + B2Http._translate_errors(fcn) + def test_b2_error__nginx_html(): """