Skip to content

Commit

Permalink
Add extra tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Sep 3, 2021
1 parent 2c730a0 commit a1239ef
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 59 deletions.
2 changes: 1 addition & 1 deletion omnikinverter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_values(position):
energy_value = float(data_list[position]) / 10
return energy_value

return data_list[position]
return data_list[position].replace(" ", "")
except AttributeError as exception:
raise OmnikInverterWrongSourceError(
"Your inverter has no data source from a javascript file."
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/status_devicearray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var myDeviceArray=new Array();
myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,1500,850,232,52002,,1,";
;
File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Test the models."""
import aiohttp
import pytest

from omnikinverter import Inverter, OmnikInverter

from . import load_fixtures


@pytest.mark.asyncio
async def test_js_webdata(aresponses):
"""Test request from a JS Webdata source."""
aresponses.add(
"example.com",
"/js/status.js",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/x-javascript"},
text=load_fixtures("status_webdata.js"),
),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware_main == "NL2-V9.8-5931"
assert inverter.firmware_slave == "V5.3-00157"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1010
assert inverter.solar_energy_today == 4.88
assert inverter.solar_energy_total == 10531.9


@pytest.mark.asyncio
async def test_js_devicearray(aresponses):
"""Test request from a JS DeviceArray source."""
aresponses.add(
"example.com",
"/js/status.js",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/x-javascript"},
text=load_fixtures("status_devicearray.js"),
),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware_main == "V4.08Build215"
assert inverter.firmware_slave == "V4.12Build246"
assert inverter.model == "Omnik1500tl"
assert inverter.solar_current_power == 850
assert inverter.solar_energy_today == 2.32
assert inverter.solar_energy_total == 5200.2


@pytest.mark.asyncio
async def test_json(aresponses):
"""Test request from a JSON source."""
aresponses.add(
"example.com",
"/status.json",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("status.json"),
),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware_slave == "V1.40Build52927"
assert inverter.firmware_main == "V1.25Build23261"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1225
assert inverter.solar_energy_today == 10.90
assert inverter.solar_energy_total == 8674.0
99 changes: 41 additions & 58 deletions tests/test_omnik.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,19 @@
"""Test for retrieving information from the Omnik Inverter device."""
"""Basic tests for the Omnik Inverter device."""
import asyncio

import aiohttp
import pytest

from omnikinverter import (
Inverter,
OmnikInverter,
OmnikInverterConnectionError,
OmnikInverterWrongSourceError,
)
from omnikinverter.exceptions import OmnikInverterError

from . import load_fixtures


@pytest.mark.asyncio
async def test_js_input(aresponses):
"""Test request from a JS input."""
aresponses.add(
"example.com",
"/js/status.js",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/x-javascript"},
text=load_fixtures("status.js"),
),
)
async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware_main == "NL2-V9.8-5931"
assert inverter.firmware_slave == "V5.3-00157"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1010
assert inverter.solar_energy_today == 4.88
assert inverter.solar_energy_total == 10531.9


@pytest.mark.asyncio
async def test_json_input(aresponses):
"""Test request from a JSON input."""
aresponses.add(
"example.com",
"/status.json",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("status.json"),
),
)
async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware_slave == "V1.40Build52927"
assert inverter.firmware_main == "V1.25Build23261"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1225
assert inverter.solar_energy_today == 10.90
assert inverter.solar_energy_total == 8674.0


@pytest.mark.asyncio
async def test_wrong_source(aresponses):
"""Test on wrong data source error raise."""
Expand All @@ -76,11 +24,12 @@ async def test_wrong_source(aresponses):
aresponses.Response(
status=200,
headers={"Content-Type": "application/x-javascript"},
text=load_fixtures("wrong_status.js"),
text=load_fixtures("status_wrong.js"),
),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
omnik = OmnikInverter(host="example.com", session=session)
with pytest.raises(OmnikInverterWrongSourceError):
assert await omnik.inverter()

Expand All @@ -91,11 +40,45 @@ async def test_timeout(aresponses):
# Faking a timeout by sleeping
async def response_handler(_):
await asyncio.sleep(0.2)
return aresponses.Response(body="Goodmorning!", text=load_fixtures("status.js"))
return aresponses.Response(
body="Goodmorning!", text=load_fixtures("status_webdata.js")
)

aresponses.add("example.com", "/js/status.js", "GET", response_handler)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
omnik = OmnikInverter(host="example.com", session=session)
with pytest.raises(OmnikInverterConnectionError):
assert await omnik.inverter()


@pytest.mark.asyncio
async def test_http_error404(aresponses):
"""Test HTTP 404 response handling."""
aresponses.add(
"example.com",
"/omnik/test",
"GET",
aresponses.Response(text="Give me energy!", status=404),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", session=session)
with pytest.raises(OmnikInverterError):
assert await omnik.request("test")


@pytest.mark.asyncio
async def test_unexpected_response(aresponses):
"""Test unexpected response handling."""
aresponses.add(
"example.com",
"/omnik/test",
"GET",
aresponses.Response(text="Give me energy!", status=200),
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", session=session)
with pytest.raises(OmnikInverterError):
assert await omnik.request("test")

0 comments on commit a1239ef

Please sign in to comment.