File: test_exceptions.py

package info (click to toggle)
python-p1monitor 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: python: 571; makefile: 8; sh: 5
file content (76 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Exceptions tests for the P1Monitor device."""

# pylint: disable=protected-access
import pytest
from aresponses import ResponsesMockServer

from p1monitor import P1Monitor
from p1monitor.exceptions import P1MonitorConnectionError, P1MonitorError


@pytest.mark.parametrize("status", [401, 403])
async def test_http_error401(
    aresponses: ResponsesMockServer,
    p1monitor_client: P1Monitor,
    status: int,
) -> None:
    """Test HTTP 401 response handling."""
    aresponses.add(
        "192.168.1.2",
        "/api/v1/smartmeter",
        "GET",
        aresponses.Response(text="Give me energy!", status=status),
    )
    with pytest.raises(P1MonitorConnectionError):
        assert await p1monitor_client._request("test")


async def test_http_error404(
    aresponses: ResponsesMockServer,
    p1monitor_client: P1Monitor,
) -> None:
    """Test HTTP 404 response handling."""
    aresponses.add(
        "192.168.1.2",
        "/api/v1/smartmeter",
        "GET",
        aresponses.Response(text="Give me energy!", status=404),
    )
    with pytest.raises(P1MonitorError):
        assert await p1monitor_client._request("test")


async def test_http_error500(
    aresponses: ResponsesMockServer,
    p1monitor_client: P1Monitor,
) -> None:
    """Test HTTP 500 response handling."""
    aresponses.add(
        "192.168.1.2",
        "/api/v1/smartmeter",
        "GET",
        aresponses.Response(
            body=b'{"status":"nok"}',
            status=500,
        ),
    )
    with pytest.raises(P1MonitorError):
        assert await p1monitor_client._request("test")


async def test_no_success(
    aresponses: ResponsesMockServer,
    p1monitor_client: P1Monitor,
) -> None:
    """Test a message without a success message throws."""
    aresponses.add(
        "192.168.1.2",
        "/api/v1/smartmeter",
        "GET",
        aresponses.Response(
            status=200,
            text='{"message": "no success"}',
        ),
    )
    with pytest.raises(P1MonitorError):
        assert await p1monitor_client._request("test")