| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Unit tests for the asynceapi.errors module.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import httpx |
| 9 |
import pytest |
| 10 |
|
| 11 |
from asynceapi.errors import EapiAuthenticationError, EapiCommandError, EapiTransportError |
| 12 |
|
| 13 |
|
| 14 |
def test_eapi_authentication_error_host_and_message() -> None: |
| 15 |
"""Test that EapiAuthenticationError stores host and includes it with HTTP 401 context in the message.""" |
| 16 |
exc = EapiAuthenticationError("192.0.2.1") |
| 17 |
assert exc.host == "192.0.2.1" |
| 18 |
message = str(exc) |
| 19 |
assert "192.0.2.1" in message |
| 20 |
assert "HTTP 401" in message |
| 21 |
|
| 22 |
|
| 23 |
def test_eapi_authentication_error_is_runtime_error() -> None: |
| 24 |
"""Test that EapiAuthenticationError is a subclass of RuntimeError.""" |
| 25 |
assert isinstance(EapiAuthenticationError("192.0.2.1"), RuntimeError) |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
("failed", "errors", "errmsg", "passed", "not_exec"), |
| 30 |
[ |
| 31 |
( |
| 32 |
"show bad-command", |
| 33 |
["Invalid input (at token 1: 'bad-command')"], |
| 34 |
"CLI command 2 of 3 'show bad-command' failed: invalid command", |
| 35 |
[{"hostname": "switch1"}], |
| 36 |
["show clock"], |
| 37 |
), |
| 38 |
( |
| 39 |
"show version", |
| 40 |
["err1", "err2", "err3"], |
| 41 |
"CLI command 1 of 1 failed", |
| 42 |
[], |
| 43 |
[], |
| 44 |
), |
| 45 |
], |
| 46 |
ids=["mid_sequence_failure", "first_cmd_fails_multiple_errors"], |
| 47 |
) |
| 48 |
def test_eapi_command_error_attributes_and_str( |
| 49 |
failed: str, |
| 50 |
errors: list[str], |
| 51 |
errmsg: str, |
| 52 |
passed: list, |
| 53 |
not_exec: list, |
| 54 |
) -> None: |
| 55 |
"""Test that EapiCommandError stores all constructor attributes and str() returns errmsg.""" |
| 56 |
exc = EapiCommandError(failed=failed, errors=errors, errmsg=errmsg, passed=passed, not_exec=not_exec) |
| 57 |
assert exc.failed == failed |
| 58 |
assert exc.errors == errors |
| 59 |
assert exc.errmsg == errmsg |
| 60 |
assert exc.passed == passed |
| 61 |
assert exc.not_exec == not_exec |
| 62 |
assert str(exc) == errmsg |
| 63 |
|
| 64 |
|
| 65 |
def test_eapi_command_error_is_runtime_error() -> None: |
| 66 |
"""Test that EapiCommandError is a subclass of RuntimeError.""" |
| 67 |
exc = EapiCommandError(failed="cmd", errors=["err"], errmsg="msg", passed=[], not_exec=[]) |
| 68 |
assert isinstance(exc, RuntimeError) |
| 69 |
|
| 70 |
|
| 71 |
def test_eapi_transport_error_is_alias_for_httpx_http_status_error() -> None: |
| 72 |
"""Test that EapiTransportError is the same class as httpx.HTTPStatusError.""" |
| 73 |
assert EapiTransportError is httpx.HTTPStatusError |