summaryrefslogtreecommitdiff
path: root/tests/test_ute.py
blob: 9e33b3865726787aeed34685ed2e900344a2dacd (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Unit tests for UTEClient class."""

import time

import pytest
import requests
from pytest_mock import MockerFixture

from src.ute_wrapper.exceptions import (
    InvalidPlanException,
    InvalidPowerFactorException,
    MultipleDevicesException,
    ReadingRequestFailedException,
    ReadingResponseInvalidException,
    TariffException,
    UnsupportedMethodException,
)
from src.ute_wrapper.ute import UTEClient


@pytest.fixture
def ute_client(mocker: MockerFixture):
    """Return a UTEClient instance with mocked methods."""
    mocker.patch.object(UTEClient, "_login", return_value="mocked_token")
    mocker.patch.object(
        UTEClient,
        "get_devices_list",
        return_value=[{"name": "Device 1", "accountServicePointId": "device_id_1"}],
    )
    mocker.patch.object(UTEClient, "get_account", return_value={"meterInfo": {"tariffType": "TRIPLE"}})
    return UTEClient("test@example.com", "1234567890")


def test_make_request_success(ute_client, mocker: MockerFixture):
    """Test the _make_request method with a successful request."""
    mocked_response = mocker.Mock(status_code=200, json=lambda: {"success": True})
    mocker.patch("src.ute_wrapper.ute.requests.get", return_value=mocked_response)
    response = ute_client._make_request("GET", "http://example.com")
    assert response.status_code == requests.codes.ok
    assert response.json()["success"] is True


def test_make_request_no_authorization(ute_client, mocker: MockerFixture):
    """Test the _make_request method when there is no authorization token."""
    ute_client.authorization = None
    mocked_response = mocker.Mock(status_code=requests.codes.ok, json=lambda: {"success": True})
    mocker.patch("requests.get", return_value=mocked_response)
    response = ute_client._make_request("GET", "http://example.com")
    assert response.status_code == requests.codes.ok
    assert response.json()["success"] is True


def test_make_request_all_retries_failed(ute_client, mocker: MockerFixture):
    """Test the _make_request method when all retries fail."""
    mocked_response = mocker.Mock()
    mocked_response.raise_for_status.side_effect = requests.RequestException
    mocker.patch("requests.get", return_value=mocked_response)
    mocker.patch("time.sleep")
    total_call_count = 3
    with pytest.raises(Exception, match="All retries failed."):
        ute_client._make_request("GET", "http://example.com", retries=total_call_count)
    assert mocked_response.raise_for_status.call_count == total_call_count
    assert time.sleep.call_count == total_call_count - 1


def test_method_not_supported(ute_client):
    """Test the _method_not_supported method."""
    with pytest.raises(UnsupportedMethodException):
        ute_client._method_not_supported()


def test_login(ute_client, mocker: MockerFixture):
    """Test the _login method."""
    mocked_response = mocker.Mock(text="mocked_token")
    mocker.patch.object(ute_client, "_make_request", return_value=mocked_response)
    token = ute_client._login()
    assert token == "mocked_token"


def test_validate_power_factor(ute_client):
    """Test the _validate_power_factor method."""
    with pytest.raises(InvalidPowerFactorException):
        ute_client.power_factor = 1.5
        ute_client._validate_power_factor()


def test_initialize_device_id(ute_client, mocker: MockerFixture):
    """Test the _initialize_device_id method."""
    ute_client.device_id = ""
    mocker.patch.object(ute_client, "_select_device_id", return_value="selected_device_id")
    ute_client._initialize_device_id()
    assert ute_client.device_id == "selected_device_id"


def test_initialize_average_cost_per_kwh(ute_client, mocker: MockerFixture):
    """Test the _initialize_average_cost_per_kwh method."""
    ute_client.average_cost_per_kwh = 0.0
    mocker.patch.object(ute_client, "_determine_average_cost", return_value=5.0)
    ute_client._initialize_average_cost_per_kwh()
    assert ute_client.average_cost_per_kwh == 5.0


def test_select_device_id_multiple_devices(ute_client, mocker: MockerFixture):
    """Test the _select_device_id method when there are multiple devices."""
    mocker.patch.object(
        ute_client,
        "get_devices_list",
        return_value=[
            {"name": "Device 1", "accountServicePointId": "device_id_1"},
            {"name": "Device 2", "accountServicePointId": "device_id_2"},
        ],
    )
    with pytest.raises(MultipleDevicesException):
        ute_client._select_device_id()


def test_determine_average_cost_invalid_tariff(ute_client, mocker: MockerFixture):
    """Test the _determine_average_cost method with an invalid tariff."""
    mocker.patch.object(ute_client, "get_account", return_value={"meterInfo": {"tariffType": "INVALID"}})
    with pytest.raises(InvalidPlanException):
        ute_client._determine_average_cost()


def test_determine_average_cost_invalid_tariff_key(ute_client, mocker: MockerFixture):
    """Test the _determine_average_cost method with an invalid tariff key."""
    mocker.patch.object(ute_client, "get_account", side_effect=KeyError)
    with pytest.raises(TariffException, match="Tariff type not standard. Explicit definition required."):
        ute_client._determine_average_cost()


@pytest.mark.parametrize(
    "date_start, date_end",
    [
        ("2023-05-01", "2023-05-02"),
        (None, None),
    ],
)
def test_get_historic_consumption(ute_client, mocker: MockerFixture, date_start, date_end):
    """Test the get_historic_consumption method."""
    mocked_response = {
        "data": [
            {"magnitudeVO": "IMPORT_ACTIVE_ENERGY", "date": "2023-05-01T00:00:00+00:00", "value": "10.0"},
            {"magnitudeVO": "IMPORT_ACTIVE_ENERGY", "date": "2023-05-02T00:00:00+00:00", "value": "20.0"},
        ],
    }
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(ute_client, "_make_request", return_value=mocker.Mock(json=lambda: mocked_response))
    result = ute_client.get_historic_consumption(date_start, date_end)
    assert result["total"]["sum_in_kwh"] == 30.0
    assert result["total"]["aproximated_cost_in_uyu"] == 136.579
    assert len(result["dates"]) == 2


def test_get_current_usage_info_success(ute_client, mocker: MockerFixture):
    """Test the get_current_usage_info method with a successful request."""
    readings = [
        {"tipoLecturaMGMI": "I1", "valor": "10"},
        {"tipoLecturaMGMI": "V1", "valor": "220"},
    ]
    mocked_reading_request = mocker.Mock(status_code=requests.codes.ok)
    mocked_reading_response = mocker.Mock(json=lambda: {"success": True, "data": {"readings": readings}})
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(ute_client, "_make_request", side_effect=[mocked_reading_request, mocked_reading_response])

    result = ute_client.get_current_usage_info()

    assert result["success"] is True
    assert result["data"]["readings"] == readings
    assert result["data"]["power_in_watts"] == 2200.0
    assert result["data"]["using_power_factor"] is True


def test_get_current_usage_info_failed_request(ute_client, mocker: MockerFixture):
    """Test the get_current_usage_info method with a failed request."""
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(
        ute_client,
        "_make_request",
        side_effect=[mocker.Mock(status_code=400), mocker.Mock(json=lambda: {"success": False})],
    )
    with pytest.raises(ReadingRequestFailedException):
        ute_client.get_current_usage_info()


def test_get_current_usage_info_invalid_response(ute_client, mocker: MockerFixture):
    """Test the get_current_usage_info method with an invalid response."""
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(
        ute_client,
        "_make_request",
        side_effect=[
            mocker.Mock(status_code=requests.codes.ok),
            mocker.Mock(json=lambda: {"success": True, "data": {}}),
        ],
    )
    with pytest.raises(ReadingResponseInvalidException):
        ute_client.get_current_usage_info()


def test_get_average_price_invalid_plan(ute_client):
    """Test the get_average_price method with an invalid plan."""
    with pytest.raises(InvalidPlanException):
        ute_client.get_average_price("invalid_plan")


@pytest.mark.parametrize(
    "plan, expected_price",
    [
        ("triple", 4.5526324),
        ("doble", 5.523887),
    ],
)
def test_get_average_price(ute_client, plan, expected_price):
    """Test the get_average_price method."""
    result = ute_client.get_average_price(plan)
    assert result == expected_price


def test_get_peak(ute_client, mocker: MockerFixture):
    """Test the get_peak method."""
    expected_peak_data = {
        "deviceId": "test_device_id",
        "peak": {
            "value": 123.45,
            "date": "2023-06-01T10:00:00Z",
        },
    }

    mocked_response = mocker.Mock(json=lambda: {"data": expected_peak_data})
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(ute_client, "_make_request", return_value=mocked_response)

    result = ute_client.get_peak()

    assert result == expected_peak_data
    ute_client._make_request.assert_called_once_with(
        "GET",
        f"https://rocme.ute.com.uy/api/v1/accounts/{ute_client.device_id}/peak",
    )


def test_get_network_status(ute_client, mocker: MockerFixture):
    """Test the get_network_status method."""
    expected_network_status = [
        {
            "departmentId": 1,
            "clientsWithoutService": 100,
            "percentageClientsWithoutService": 0.5,
        },
        {
            "departmentId": 2,
            "clientsWithoutService": requests.codes.ok,
            "percentageClientsWithoutService": 1.0,
        },
    ]

    mocked_response = mocker.Mock(json=lambda: {"data": {"summary": expected_network_status}})
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(ute_client, "_make_request", return_value=mocked_response)

    result = ute_client.get_network_status()

    assert result == expected_network_status
    ute_client._make_request.assert_called_once_with(
        "GET",
        "https://rocme.ute.com.uy/api/v1/info/network/status",
    )


def test_get_renewable_sources(ute_client, mocker: MockerFixture):
    """Test the get_renewable_sources method."""
    expected_renewable_sources = "50.0%"

    mocked_response = mocker.Mock(json=lambda: {"data": {"renewableSources": expected_renewable_sources}})
    mocker.patch.object(ute_client, "_login")
    mocker.patch.object(ute_client, "_make_request", return_value=mocked_response)

    result = ute_client.get_renewable_sources()

    assert result == expected_renewable_sources
    ute_client._make_request.assert_called_once_with(
        "GET",
        "https://rocme.ute.com.uy/api/v1/info/demand/global",
    )


@pytest.mark.parametrize(
    "readings, expected_power",
    [
        (
            [
                {"tipoLecturaMGMI": "I1", "valor": "10"},
                {"tipoLecturaMGMI": "V1", "valor": "220"},
            ],
            2200.0,
        ),
        (
            [
                {"tipoLecturaMGMI": "I1", "valor": "10"},
                {"tipoLecturaMGMI": "I2", "valor": "10"},
                {"tipoLecturaMGMI": "V1", "valor": "220"},
                {"tipoLecturaMGMI": "V2", "valor": "220"},
            ],
            2200.0,
        ),
        (
            [
                {"tipoLecturaMGMI": "I1", "valor": "10"},
                {"tipoLecturaMGMI": "I2", "valor": "10"},
                {"tipoLecturaMGMI": "I3", "valor": "10"},
                {"tipoLecturaMGMI": "V1", "valor": "220"},
                {"tipoLecturaMGMI": "V2", "valor": "220"},
                {"tipoLecturaMGMI": "V3", "valor": "220"},
            ],
            3810.4,
        ),
        (
            [],
            0.0,
        ),
    ],
)
def test_convert_powers_to_power_in_watts(ute_client, readings, expected_power):
    """Test the _convert_powers_to_power_in_watts method."""
    result = ute_client._convert_powers_to_power_in_watts(readings)
    assert result == expected_power