summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Gonzalez <roger@rogs.me>2024-03-09 18:06:01 -0300
committerRoger Gonzalez <roger@rogs.me>2024-03-09 18:06:01 -0300
commitda52b296f8540a0eb3696232a9fbcc07ac209ad5 (patch)
tree44b7b87c43e624d4fa039a4ab32ac8b703de75b4
parented6a025bcbaf8933bdd0ad58187f87a5cd2a2815 (diff)
Added unit tests
-rw-r--r--.gitlab-ci.yml36
-rw-r--r--poetry.lock159
-rw-r--r--pyproject.toml5
-rwxr-xr-xsrc/ute_wrapper/ute.py4
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/test_ute.py319
6 files changed, 510 insertions, 13 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 350b74e..064613a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,20 +1,38 @@
image: python:latest
+stages:
+ - lint
+ - test
+ - deploy
+
+before_script:
+ - pip install poetry
+ - poetry install --no-root
+
lint:
+ stage: lint
+ script:
+ - poetry run ruff check
+ - poetry run ruff format --check
+
+test:
stage: test
- before_script:
- - pip install poetry
- - poetry install --no-root
script:
- - poetry run ruff check ./src
- - poetry run ruff format --check ./src
+ - poetry run pytest -v --cov=. --cov-report=xml
+ after_script:
+ - bash <(curl -s https://codecov.io/bash)
+ artifacts:
+ reports:
+ coverage_report:
+ coverage_format: cobertura
+ path: coverage.xml
deploy_to_pypi:
stage: deploy
only:
- - master
- before_script:
- - pip install poetry
- - poetry install --no-root
+ refs:
+ - master
+ changes:
+ - pyproject.toml
script:
- POETRY_PYPI_TOKEN_PYPI=$PYPI_PASSWORD poetry publish --build
diff --git a/poetry.lock b/poetry.lock
index 5de8719..8cb796c 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -166,6 +166,84 @@ files = [
]
[[package]]
+name = "colorama"
+version = "0.4.6"
+description = "Cross-platform colored terminal text."
+optional = false
+python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
+files = [
+ {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
+ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
+]
+
+[[package]]
+name = "coverage"
+version = "7.4.3"
+description = "Code coverage measurement for Python"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "coverage-7.4.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:8580b827d4746d47294c0e0b92854c85a92c2227927433998f0d3320ae8a71b6"},
+ {file = "coverage-7.4.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:718187eeb9849fc6cc23e0d9b092bc2348821c5e1a901c9f8975df0bc785bfd4"},
+ {file = "coverage-7.4.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:767b35c3a246bcb55b8044fd3a43b8cd553dd1f9f2c1eeb87a302b1f8daa0524"},
+ {file = "coverage-7.4.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ae7f19afe0cce50039e2c782bff379c7e347cba335429678450b8fe81c4ef96d"},
+ {file = "coverage-7.4.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba3a8aaed13770e970b3df46980cb068d1c24af1a1968b7818b69af8c4347efb"},
+ {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:ee866acc0861caebb4f2ab79f0b94dbfbdbfadc19f82e6e9c93930f74e11d7a0"},
+ {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:506edb1dd49e13a2d4cac6a5173317b82a23c9d6e8df63efb4f0380de0fbccbc"},
+ {file = "coverage-7.4.3-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:fd6545d97c98a192c5ac995d21c894b581f1fd14cf389be90724d21808b657e2"},
+ {file = "coverage-7.4.3-cp310-cp310-win32.whl", hash = "sha256:f6a09b360d67e589236a44f0c39218a8efba2593b6abdccc300a8862cffc2f94"},
+ {file = "coverage-7.4.3-cp310-cp310-win_amd64.whl", hash = "sha256:18d90523ce7553dd0b7e23cbb28865db23cddfd683a38fb224115f7826de78d0"},
+ {file = "coverage-7.4.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:cbbe5e739d45a52f3200a771c6d2c7acf89eb2524890a4a3aa1a7fa0695d2a47"},
+ {file = "coverage-7.4.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:489763b2d037b164846ebac0cbd368b8a4ca56385c4090807ff9fad817de4113"},
+ {file = "coverage-7.4.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:451f433ad901b3bb00184d83fd83d135fb682d780b38af7944c9faeecb1e0bfe"},
+ {file = "coverage-7.4.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fcc66e222cf4c719fe7722a403888b1f5e1682d1679bd780e2b26c18bb648cdc"},
+ {file = "coverage-7.4.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b3ec74cfef2d985e145baae90d9b1b32f85e1741b04cd967aaf9cfa84c1334f3"},
+ {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:abbbd8093c5229c72d4c2926afaee0e6e3140de69d5dcd918b2921f2f0c8baba"},
+ {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:35eb581efdacf7b7422af677b92170da4ef34500467381e805944a3201df2079"},
+ {file = "coverage-7.4.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:8249b1c7334be8f8c3abcaaa996e1e4927b0e5a23b65f5bf6cfe3180d8ca7840"},
+ {file = "coverage-7.4.3-cp311-cp311-win32.whl", hash = "sha256:cf30900aa1ba595312ae41978b95e256e419d8a823af79ce670835409fc02ad3"},
+ {file = "coverage-7.4.3-cp311-cp311-win_amd64.whl", hash = "sha256:18c7320695c949de11a351742ee001849912fd57e62a706d83dfc1581897fa2e"},
+ {file = "coverage-7.4.3-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:b51bfc348925e92a9bd9b2e48dad13431b57011fd1038f08316e6bf1df107d10"},
+ {file = "coverage-7.4.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:d6cdecaedea1ea9e033d8adf6a0ab11107b49571bbb9737175444cea6eb72328"},
+ {file = "coverage-7.4.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3b2eccb883368f9e972e216c7b4c7c06cabda925b5f06dde0650281cb7666a30"},
+ {file = "coverage-7.4.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6c00cdc8fa4e50e1cc1f941a7f2e3e0f26cb2a1233c9696f26963ff58445bac7"},
+ {file = "coverage-7.4.3-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b9a4a8dd3dcf4cbd3165737358e4d7dfbd9d59902ad11e3b15eebb6393b0446e"},
+ {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:062b0a75d9261e2f9c6d071753f7eef0fc9caf3a2c82d36d76667ba7b6470003"},
+ {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:ebe7c9e67a2d15fa97b77ea6571ce5e1e1f6b0db71d1d5e96f8d2bf134303c1d"},
+ {file = "coverage-7.4.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:c0a120238dd71c68484f02562f6d446d736adcc6ca0993712289b102705a9a3a"},
+ {file = "coverage-7.4.3-cp312-cp312-win32.whl", hash = "sha256:37389611ba54fd6d278fde86eb2c013c8e50232e38f5c68235d09d0a3f8aa352"},
+ {file = "coverage-7.4.3-cp312-cp312-win_amd64.whl", hash = "sha256:d25b937a5d9ffa857d41be042b4238dd61db888533b53bc76dc082cb5a15e914"},
+ {file = "coverage-7.4.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:28ca2098939eabab044ad68850aac8f8db6bf0b29bc7f2887d05889b17346454"},
+ {file = "coverage-7.4.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:280459f0a03cecbe8800786cdc23067a8fc64c0bd51dc614008d9c36e1659d7e"},
+ {file = "coverage-7.4.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6c0cdedd3500e0511eac1517bf560149764b7d8e65cb800d8bf1c63ebf39edd2"},
+ {file = "coverage-7.4.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9a9babb9466fe1da12417a4aed923e90124a534736de6201794a3aea9d98484e"},
+ {file = "coverage-7.4.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dec9de46a33cf2dd87a5254af095a409ea3bf952d85ad339751e7de6d962cde6"},
+ {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:16bae383a9cc5abab9bb05c10a3e5a52e0a788325dc9ba8499e821885928968c"},
+ {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:2c854ce44e1ee31bda4e318af1dbcfc929026d12c5ed030095ad98197eeeaed0"},
+ {file = "coverage-7.4.3-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:ce8c50520f57ec57aa21a63ea4f325c7b657386b3f02ccaedeccf9ebe27686e1"},
+ {file = "coverage-7.4.3-cp38-cp38-win32.whl", hash = "sha256:708a3369dcf055c00ddeeaa2b20f0dd1ce664eeabde6623e516c5228b753654f"},
+ {file = "coverage-7.4.3-cp38-cp38-win_amd64.whl", hash = "sha256:1bf25fbca0c8d121a3e92a2a0555c7e5bc981aee5c3fdaf4bb7809f410f696b9"},
+ {file = "coverage-7.4.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:3b253094dbe1b431d3a4ac2f053b6d7ede2664ac559705a704f621742e034f1f"},
+ {file = "coverage-7.4.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:77fbfc5720cceac9c200054b9fab50cb2a7d79660609200ab83f5db96162d20c"},
+ {file = "coverage-7.4.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6679060424faa9c11808598504c3ab472de4531c571ab2befa32f4971835788e"},
+ {file = "coverage-7.4.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4af154d617c875b52651dd8dd17a31270c495082f3d55f6128e7629658d63765"},
+ {file = "coverage-7.4.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8640f1fde5e1b8e3439fe482cdc2b0bb6c329f4bb161927c28d2e8879c6029ee"},
+ {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:69b9f6f66c0af29642e73a520b6fed25ff9fd69a25975ebe6acb297234eda501"},
+ {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:0842571634f39016a6c03e9d4aba502be652a6e4455fadb73cd3a3a49173e38f"},
+ {file = "coverage-7.4.3-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:a78ed23b08e8ab524551f52953a8a05d61c3a760781762aac49f8de6eede8c45"},
+ {file = "coverage-7.4.3-cp39-cp39-win32.whl", hash = "sha256:c0524de3ff096e15fcbfe8f056fdb4ea0bf497d584454f344d59fce069d3e6e9"},
+ {file = "coverage-7.4.3-cp39-cp39-win_amd64.whl", hash = "sha256:0209a6369ccce576b43bb227dc8322d8ef9e323d089c6f3f26a597b09cb4d2aa"},
+ {file = "coverage-7.4.3-pp38.pp39.pp310-none-any.whl", hash = "sha256:7cbde573904625509a3f37b6fecea974e363460b556a627c60dc2f47e2fffa51"},
+ {file = "coverage-7.4.3.tar.gz", hash = "sha256:276f6077a5c61447a48d133ed13e759c09e62aff0dc84274a68dc18660104d52"},
+]
+
+[package.dependencies]
+tomli = {version = "*", optional = true, markers = "python_full_version <= \"3.11.0a6\" and extra == \"toml\""}
+
+[package.extras]
+toml = ["tomli"]
+
+[[package]]
name = "distlib"
version = "0.3.8"
description = "Distribution utilities"
@@ -262,6 +340,17 @@ perf = ["ipython"]
testing = ["flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-perf (>=0.9.2)", "pytest-ruff"]
[[package]]
+name = "iniconfig"
+version = "2.0.0"
+description = "brain-dead simple config-ini parsing"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
+ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
+]
+
+[[package]]
name = "jedi"
version = "0.19.1"
description = "An autocompletion tool for Python that can be used for text editors."
@@ -310,6 +399,17 @@ files = [
setuptools = "*"
[[package]]
+name = "packaging"
+version = "23.2"
+description = "Core utilities for Python packages"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"},
+ {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"},
+]
+
+[[package]]
name = "parso"
version = "0.8.3"
description = "A Python Parser"
@@ -373,6 +473,63 @@ pyyaml = ">=5.1"
virtualenv = ">=20.10.0"
[[package]]
+name = "pytest"
+version = "8.1.1"
+description = "pytest: simple powerful testing with Python"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "pytest-8.1.1-py3-none-any.whl", hash = "sha256:2a8386cfc11fa9d2c50ee7b2a57e7d898ef90470a7a34c4b949ff59662bb78b7"},
+ {file = "pytest-8.1.1.tar.gz", hash = "sha256:ac978141a75948948817d360297b7aae0fcb9d6ff6bc9ec6d514b85d5a65c044"},
+]
+
+[package.dependencies]
+colorama = {version = "*", markers = "sys_platform == \"win32\""}
+exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
+iniconfig = "*"
+packaging = "*"
+pluggy = ">=1.4,<2.0"
+tomli = {version = ">=1", markers = "python_version < \"3.11\""}
+
+[package.extras]
+testing = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
+
+[[package]]
+name = "pytest-cov"
+version = "4.1.0"
+description = "Pytest plugin for measuring coverage."
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "pytest-cov-4.1.0.tar.gz", hash = "sha256:3904b13dfbfec47f003b8e77fd5b589cd11904a21ddf1ab38a64f204d6a10ef6"},
+ {file = "pytest_cov-4.1.0-py3-none-any.whl", hash = "sha256:6ba70b9e97e69fcc3fb45bfeab2d0a138fb65c4d0d6a41ef33983ad114be8c3a"},
+]
+
+[package.dependencies]
+coverage = {version = ">=5.2.1", extras = ["toml"]}
+pytest = ">=4.6"
+
+[package.extras]
+testing = ["fields", "hunter", "process-tests", "pytest-xdist", "six", "virtualenv"]
+
+[[package]]
+name = "pytest-mock"
+version = "3.12.0"
+description = "Thin-wrapper around the mock package for easier use with pytest"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "pytest-mock-3.12.0.tar.gz", hash = "sha256:31a40f038c22cad32287bb43932054451ff5583ff094bca6f675df2f8bc1a6e9"},
+ {file = "pytest_mock-3.12.0-py3-none-any.whl", hash = "sha256:0972719a7263072da3a21c7f4773069bcc7486027d7e8e1f81d98a47e701bc4f"},
+]
+
+[package.dependencies]
+pytest = ">=5.0"
+
+[package.extras]
+dev = ["pre-commit", "pytest-asyncio", "tox"]
+
+[[package]]
name = "python-lsp-jsonrpc"
version = "1.1.2"
description = "JSON RPC 2.0 server library"
@@ -717,4 +874,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata]
lock-version = "2.0"
python-versions = ">=3.9"
-content-hash = "0a6f86cf51e05d548af0f27608079383cc10b8bf036c7669500f51208cdd0b46"
+content-hash = "3af2371329597b4b40f1b2b56d61cbca6009d76b97f1283b64475aee7261fc35"
diff --git a/pyproject.toml b/pyproject.toml
index 5e6fa3f..419f57f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -23,6 +23,9 @@ requests = ">=2.0.0, <3.0.0"
python-lsp-server = "^1.10.0"
python-lsp-ruff = "^2.2.0"
pre-commit = "^3.6.2"
+pytest = "^8.1.1"
+pytest-mock = "^3.12.0"
+pytest-cov = "^4.1.0"
[build-system]
requires = ["poetry-core"]
@@ -65,7 +68,7 @@ target-version = "py39"
[tool.ruff.lint]
select = ["E", "F", "T20", "PL", "B", "A", "C4", "TID", "ERA", "RET", "W", "C90", "ARG", "Q", "FLY", "SIM", "COM", "D"]
-ignore = ["E402"]
+ignore = ["E402", "PLW2901", "COM812", "PLR2004"]
[tool.ruff.lint.pylint]
max-args = 6
diff --git a/src/ute_wrapper/ute.py b/src/ute_wrapper/ute.py
index 7c84c29..1560a64 100755
--- a/src/ute_wrapper/ute.py
+++ b/src/ute_wrapper/ute.py
@@ -187,9 +187,9 @@ class UTEClient:
response = getattr(requests, method.lower(), self._method_not_supported)(url, headers=headers, json=data)
response.raise_for_status()
return response
- except (requests.RequestException, Exception) as e:
+ except (requests.RequestException, Exception):
if attempt == retries - 1:
- raise e
+ break
time.sleep(delay)
raise Exception("All retries failed.")
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/test_ute.py b/tests/test_ute.py
new file mode 100644
index 0000000..6dacb74
--- /dev/null
+++ b/tests/test_ute.py
@@ -0,0 +1,319 @@
+"""Unit tests for UTEClient class."""
+
+import time
+
+import pytest
+import requests
+from pytest_mock import MockerFixture
+
+from src.ute_wrapper.ute import (
+ InvalidPlanException,
+ InvalidPowerFactorException,
+ MultipleDevicesException,
+ ReadingRequestFailedException,
+ ReadingResponseInvalidException,
+ TariffException,
+ UnsupportedMethodException,
+ 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."""
+ del ute_client.authorization
+ 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("requests.post", 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, "_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, "_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,
+ "_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,
+ "_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, "_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, "_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, "_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