Skip to content

Commit d109a94

Browse files
committed
use pytest.raises
1 parent 6f2819b commit d109a94

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

tests/test_exceptions.py

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
""" Test that exceptions are translated/rendered correctly """
2+
import pytest
3+
24
from ledgerblue.commException import CommException
35

46
from ledgereth.comms import dongle_send
@@ -14,90 +16,73 @@
1416

1517
def test_comms_exception(yield_dongle):
1618
with yield_dongle(exception=CommException("TEST", 0xFFFF, 0x00)) as dongle:
17-
try:
19+
with pytest.raises(LedgerError) as err:
1820
dongle_send(dongle, "GET_CONFIGURATION")
19-
assert False
20-
except Exception as err:
21-
assert isinstance(err, LedgerError), f"Unexpected exception: {type(err)}"
21+
22+
assert "Unexpected error" in str(err.value)
2223

2324

2425
def test_comms_exception_invalid(yield_dongle):
2526
with yield_dongle(exception=CommException("TEST", 0x6A80, 0x00)) as dongle:
26-
try:
27+
with pytest.raises(LedgerInvalid) as err:
2728
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
28-
assert False
29-
except Exception as err:
30-
assert isinstance(err, LedgerInvalid), f"Unexpected exception: {type(err)}"
29+
30+
assert "Invalid data" in str(err.value)
3131

3232

3333
def test_comms_not_found(yield_dongle):
3434
with yield_dongle(exception=CommException("TEST", 0x6F00, 0x00)) as dongle:
35-
try:
35+
with pytest.raises(LedgerNotFound) as err:
3636
dongle_send(dongle, "GET_DEFAULT_ADDRESS_NO_CONFIRM")
37-
assert False
38-
except Exception as err:
39-
assert isinstance(err, LedgerNotFound), f"Unexpected exception: {type(err)}"
37+
38+
assert "Unable to find Ledger" in str(err.value)
4039

4140

4241
def test_comms_exception_locked(yield_dongle):
4342
with yield_dongle(exception=CommException("TEST", 0x6B0C, 0x00)) as dongle:
44-
try:
43+
with pytest.raises(LedgerLocked) as err:
4544
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
46-
assert False
47-
except Exception as err:
48-
assert isinstance(err, LedgerLocked), f"Unexpected exception: {type(err)}"
45+
46+
assert "locked" in str(err.value)
4947

5048

5149
def test_comms_app_not_open(yield_dongle):
5250
with yield_dongle(exception=CommException("TEST", 0x6804, 0x00)) as dongle:
53-
try:
51+
with pytest.raises(LedgerAppNotOpened) as err:
5452
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
55-
assert False
56-
except Exception as err:
57-
assert isinstance(
58-
err, LedgerAppNotOpened
59-
), f"Unexpected exception: {type(err)}"
53+
54+
assert "Ethereum app" in str(err.value)
6055

6156
with yield_dongle(exception=CommException("TEST", 0x6D00, 0x00)) as dongle:
62-
try:
57+
with pytest.raises(LedgerAppNotOpened) as err:
6358
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
64-
assert False
65-
except Exception as err:
66-
assert isinstance(
67-
err, LedgerAppNotOpened
68-
), f"Unexpected exception: {type(err)}"
59+
60+
assert "Ethereum app" in str(err.value)
6961

7062
with yield_dongle(exception=CommException("TEST", 0x6D02, 0x00)) as dongle:
71-
try:
63+
with pytest.raises(LedgerAppNotOpened) as err:
7264
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
73-
assert False
74-
except Exception as err:
75-
assert isinstance(
76-
err, LedgerAppNotOpened
77-
), f"Unexpected exception: {type(err)}"
65+
66+
assert "Ethereum app" in str(err.value)
7867

7968

8069
def test_comms_user_cancel(yield_dongle):
8170
with yield_dongle(exception=CommException("TEST", 0x6982, 0x00)) as dongle:
82-
try:
71+
with pytest.raises(LedgerCancel) as err:
8372
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
84-
assert False
85-
except Exception as err:
86-
assert isinstance(err, LedgerCancel), f"Unexpected exception: {type(err)}"
73+
74+
assert "cancelled" in str(err.value)
8775

8876
with yield_dongle(exception=CommException("TEST", 0x6985, 0x00)) as dongle:
89-
try:
77+
with pytest.raises(LedgerCancel) as err:
9078
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
91-
assert False
92-
except Exception as err:
93-
assert isinstance(err, LedgerCancel), f"Unexpected exception: {type(err)}"
79+
80+
assert "cancelled" in str(err.value)
9481

9582

9683
def test_comms_unknown_error(yield_dongle):
9784
with yield_dongle(exception=CommException("TEST", 0xEEEE, 0x00)) as dongle:
98-
try:
85+
with pytest.raises(LedgerError) as err:
9986
dongle_send(dongle, "SIGN_TX_FIRST_DATA")
100-
assert False
101-
except Exception as err:
102-
assert "UNKNOWN" in str(err)
103-
assert isinstance(err, LedgerError), f"Unexpected exception: {type(err)}"
87+
88+
assert "UNKNOWN" in str(err.value)

0 commit comments

Comments
 (0)