|
| 1 | +# Copyright (c) 2016-2026 Memgraph Ltd. [https://memgraph.com] |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import mgclient |
| 16 | +import pytest |
| 17 | + |
| 18 | +from gqlalchemy.exceptions import ( |
| 19 | + GQLAlchemyDatabaseError, |
| 20 | + GQLAlchemyTransientError, |
| 21 | + database_error_handler, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def test_transient_error_is_a_database_error(): |
| 26 | + # Backwards compatible: code catching GQLAlchemyDatabaseError still catches |
| 27 | + # the transient subclass. |
| 28 | + assert issubclass(GQLAlchemyTransientError, GQLAlchemyDatabaseError) |
| 29 | + |
| 30 | + |
| 31 | +def test_transient_error_is_mapped_to_gqlalchemy_transient_error(): |
| 32 | + @database_error_handler |
| 33 | + def boom(): |
| 34 | + raise mgclient.TransientError("instance briefly unreachable during a failover") |
| 35 | + |
| 36 | + with pytest.raises(GQLAlchemyTransientError): |
| 37 | + boom() |
| 38 | + |
| 39 | + |
| 40 | +def test_non_transient_database_error_is_not_transient(): |
| 41 | + @database_error_handler |
| 42 | + def boom(): |
| 43 | + raise mgclient.DatabaseError("syntax error") |
| 44 | + |
| 45 | + with pytest.raises(GQLAlchemyDatabaseError) as exc_info: |
| 46 | + boom() |
| 47 | + assert not isinstance(exc_info.value, GQLAlchemyTransientError) |
| 48 | + |
| 49 | + |
| 50 | +def test_plain_exception_is_mapped_to_database_error(): |
| 51 | + @database_error_handler |
| 52 | + def boom(): |
| 53 | + raise ValueError("not a database error") |
| 54 | + |
| 55 | + with pytest.raises(GQLAlchemyDatabaseError) as exc_info: |
| 56 | + boom() |
| 57 | + assert not isinstance(exc_info.value, GQLAlchemyTransientError) |
0 commit comments