File: test_pvoutput.py

package info (click to toggle)
python-pvo 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: python: 440; makefile: 3
file content (265 lines) | stat: -rw-r--r-- 8,308 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Asynchronous client for the PVOutput API."""

# pylint: disable=protected-access
import asyncio
import socket
from datetime import date, datetime, time, timezone
from unittest.mock import patch

import aiohttp
import pytest
from aresponses import Response, ResponsesMockServer
from syrupy.assertion import SnapshotAssertion

from pvo import PVOutput
from pvo.exceptions import (
    PVOutputAuthenticationError,
    PVOutputConnectionError,
    PVOutputError,
    PVOutputNoDataError,
)


async def test_request(aresponses: ResponsesMockServer) -> None:
    """Test response is handled correctly."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/test",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text="ok",
        ),
    )
    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        response = await pvoutput._request("test")
        assert response == "ok"
        await pvoutput.close()


async def test_internal_session(aresponses: ResponsesMockServer) -> None:
    """Test response is handled correctly."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/test",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text="ok",
        ),
    )
    async with PVOutput(api_key="fake", system_id=12345) as pvoutput:
        response = await pvoutput._request("test")
        assert response == "ok"


async def test_post_request(aresponses: ResponsesMockServer) -> None:
    """Test post requests are handled correctly."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/test",
        "POST",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text="ok",
        ),
    )
    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        response = await pvoutput._request(
            "test",
            method=aiohttp.hdrs.METH_POST,
            data={},
        )
        assert response == "ok"


async def test_timeout(aresponses: ResponsesMockServer) -> None:
    """Test request timeout from the API."""

    # Faking a timeout by sleeping
    async def response_handler(_: aiohttp.ClientResponse) -> Response:
        """Response handler for this test."""
        await asyncio.sleep(0.2)
        return aresponses.Response(body="Goodmorning!")

    aresponses.add("pvoutput.org", "/service/r2/test", "GET", response_handler)

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(
            api_key="fake",
            system_id=12345,
            session=session,
            request_timeout=0.1,
        )
        with pytest.raises(PVOutputConnectionError):
            assert await pvoutput._request("test")


async def test_http_error400(aresponses: ResponsesMockServer) -> None:
    """Test HTTP 404 response handling."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/test",
        "GET",
        aresponses.Response(text="OMG PUPPIES!", status=404),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        with pytest.raises(PVOutputError):
            assert await pvoutput._request("test")


async def test_http_error401(aresponses: ResponsesMockServer) -> None:
    """Test HTTP 401 response handling."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/test",
        "GET",
        aresponses.Response(text="Access denied!", status=401),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        with pytest.raises(PVOutputAuthenticationError):
            assert await pvoutput._request("test")


async def test_communication_error() -> None:
    """Test communication error handling."""
    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        with (
            patch.object(
                session,
                "request",
                side_effect=socket.gaierror,
            ),
            pytest.raises(PVOutputConnectionError),
        ):
            assert await pvoutput._request("test")


async def test_get_status(
    aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
    """Test get status handling."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/getstatus.jsp",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text="20211222,18:00,3636,0,NaN,NaN,NaN,21.2,220.1",
        ),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        status = await pvoutput.status()

    assert status.reported_date == date(2021, 12, 22)
    assert status.reported_time == time(18, 0)
    assert status.reported_datetime == datetime(
        2021,
        12,
        22,
        18,
        0,
        tzinfo=timezone.utc,
    )
    assert status.energy_consumption is None
    assert status.energy_generation == 3636
    assert status.normalized_output is None
    assert status.power_consumption is None
    assert status.power_generation == 0
    assert status.temperature == 21.2
    assert status.voltage == 220.1

    assert status.to_dict() == snapshot


async def test_get_status_no_data(aresponses: ResponsesMockServer) -> None:
    """Test PVOutput status without data is handled."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/getstatus.jsp",
        "GET",
        aresponses.Response(text="Bad Request!", status=400),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        with pytest.raises(PVOutputNoDataError):
            await pvoutput.status()


async def test_get_system(
    aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
    """Test get system handling."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/getsystem.jsp",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text=(
                "Frenck,5015,CO1,17,295,JA solar JAM-300,1,5000,"
                "SolarEdge SE5000H,S,20.0,Low,20180622,51.1234,6.1234,5;;0"
            ),
        ),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        system = await pvoutput.system()

    assert system.array_tilt == 20.0
    assert system.install_date == date(2018, 6, 22)
    assert system.inverter_brand == "SolarEdge SE5000H"
    assert system.inverter_power == 5000
    assert system.inverters == 1
    assert system.latitude == 51.1234
    assert system.longitude == 6.1234
    assert system.orientation == "S"
    assert system.panel_brand == "JA solar JAM-300"
    assert system.panel_power == 295
    assert system.panels == 17
    assert system.shade == "Low"
    assert system.status_interval == 5
    assert system.system_name == "Frenck"
    assert system.system_size == 5015
    assert system.zipcode == "CO1"

    assert system.to_dict() == snapshot


async def test_get_system_empty_install_date(aresponses: ResponsesMockServer) -> None:
    """Test get system handling with an empty install date."""
    aresponses.add(
        "pvoutput.org",
        "/service/r2/getsystem.jsp",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "text/plain"},
            text=(
                "Frenck,5015,1234,17,295,JA solar JAM-300,1,5000,"
                "SolarEdge SE5000H,S,20.0,Low,,51.1234,6.1234,5;;0"
            ),
        ),
    )

    async with aiohttp.ClientSession() as session:
        pvoutput = PVOutput(api_key="fake", system_id=12345, session=session)
        system = await pvoutput.system()

    assert system.install_date is None