| 1 |
"""Tests for the error hierarchy.""" |
| 2 |
|
| 3 |
import warnings |
| 4 |
|
| 5 |
import osmapi |
| 6 |
import pytest |
| 7 |
|
| 8 |
|
| 9 |
def test_payload_str_decodes_bytes(): |
| 10 |
error = osmapi.ApiError(409, "Conflict", b"The changeset was closed") |
| 11 |
|
| 12 |
assert error.payload_str == "The changeset was closed" |
| 13 |
|
| 14 |
|
| 15 |
def test_payload_str_passes_through_text(): |
| 16 |
error = osmapi.ApiError(409, "Conflict", "already a string") |
| 17 |
|
| 18 |
assert error.payload_str == "already a string" |
| 19 |
|
| 20 |
|
| 21 |
def test_payload_str_replaces_undecodable_bytes(): |
| 22 |
error = osmapi.ApiError(500, "Server Error", b"caf\xff") |
| 23 |
|
| 24 |
assert error.payload_str == "caf�" |
| 25 |
|
| 26 |
|
| 27 |
def test_payload_str_of_empty_payload(): |
| 28 |
error = osmapi.ApiError(0, "Connection error", "") |
| 29 |
|
| 30 |
assert error.payload_str == "" |
| 31 |
|
| 32 |
|
| 33 |
def test_api_error_str(): |
| 34 |
error = osmapi.ApiError(404, "Not Found", b"no such element") |
| 35 |
|
| 36 |
assert str(error) == "Request failed: 404 - Not Found - b'no such element'" |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize( |
| 40 |
"error_class", |
| 41 |
[ |
| 42 |
osmapi.UnauthorizedApiError, |
| 43 |
osmapi.ElementNotFoundApiError, |
| 44 |
osmapi.ElementDeletedApiError, |
| 45 |
osmapi.ChangesetClosedApiError, |
| 46 |
osmapi.VersionMismatchApiError, |
| 47 |
osmapi.PreconditionFailedApiError, |
| 48 |
osmapi.NoteAlreadyClosedApiError, |
| 49 |
osmapi.AlreadySubscribedApiError, |
| 50 |
osmapi.NotSubscribedApiError, |
| 51 |
osmapi.TimeoutApiError, |
| 52 |
osmapi.ConnectionApiError, |
| 53 |
osmapi.ResponseEmptyApiError, |
| 54 |
], |
| 55 |
) |
| 56 |
def test_api_errors_are_catchable_as_api_error(error_class): |
| 57 |
"""Every typed API error can be caught as ApiError and as OsmApiError.""" |
| 58 |
error = error_class(500, "reason", b"payload") |
| 59 |
|
| 60 |
assert isinstance(error, osmapi.ApiError) |
| 61 |
assert isinstance(error, osmapi.OsmApiError) |
| 62 |
assert error.payload_str == "payload" |
| 63 |
|
| 64 |
|
| 65 |
################################################## |
| 66 |
# Deprecated error names (to be removed in 7.0) # |
| 67 |
################################################## |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
"module", [osmapi, osmapi.errors], ids=["osmapi", "osmapi.errors"] |
| 72 |
) |
| 73 |
def test_deprecated_error_name_resolves_to_new_class(module): |
| 74 |
"""The old name is the new class, so existing `except` clauses still work.""" |
| 75 |
with pytest.deprecated_call(match="renamed to AuthenticationMissingError"): |
| 76 |
error_class = module.UsernamePasswordMissingError |
| 77 |
|
| 78 |
assert error_class is osmapi.AuthenticationMissingError |
| 79 |
|
| 80 |
|
| 81 |
def test_deprecated_error_name_catches_new_error(): |
| 82 |
with warnings.catch_warnings(): |
| 83 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 84 |
|
| 85 |
with pytest.raises(osmapi.UsernamePasswordMissingError): |
| 86 |
raise osmapi.AuthenticationMissingError("no credentials") |
| 87 |
|
| 88 |
|
| 89 |
def test_deprecated_error_name_from_import_warns(): |
| 90 |
with pytest.deprecated_call(match="removed in osmapi 7.0"): |
| 91 |
from osmapi.errors import UsernamePasswordMissingError |
| 92 |
|
| 93 |
assert UsernamePasswordMissingError is osmapi.AuthenticationMissingError |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.parametrize( |
| 97 |
"module", [osmapi, osmapi.errors], ids=["osmapi", "osmapi.errors"] |
| 98 |
) |
| 99 |
def test_unknown_attribute_still_raises_attribute_error(module): |
| 100 |
"""The deprecation hook must not swallow typos.""" |
| 101 |
with pytest.raises(AttributeError, match="NoSuchError"): |
| 102 |
module.NoSuchError |