Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use syrupy for model testing #848

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pytest = "8.3.3"
pytest-asyncio = "0.24.0"
pytest-cov = "6.0.0"
ruff = "0.7.3"
syrupy = "4.7.2"
yamllint = "1.35.1"

[tool.coverage.run]
Expand Down
34 changes: 34 additions & 0 deletions tests/__snapshots__/test_models.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# serializer version: 1
# name: test_device_html
Device(signal_quality=None, firmware='ME_08_0102_2.03', ip_address='192.168.0.106')
# ---
# name: test_device_js_devicearray
Device(signal_quality=39, firmware='H4.01.51MW.2.01W1.0.64(2018-01-251-D)', ip_address=None)
# ---
# name: test_device_js_webdata
Device(signal_quality=96, firmware='H4.01.38Y1.0.09W1.0.08', ip_address='192.168.0.10')
# ---
# name: test_device_json
Device(signal_quality=None, firmware='ME-111001-V1.0.6(2015-10-16)', ip_address='192.168.0.10')
# ---
# name: test_device_without_session
Device(signal_quality=96, firmware='MW_08_512_0501_1.82', ip_address='192.168.178.3')
# ---
# name: test_inverter_html
Inverter(serial_number='12345678910', model='Omnik2500tl', solar_rated_power=2500, solar_current_power=219, solar_energy_today=0.23, solar_energy_total=6454.5, alarm_code='F13', firmware='V5.07Build245', firmware_slave=None, inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
# name: test_inverter_js_devicearray
Inverter(serial_number='12345678910', model='Omnik1500tl', solar_rated_power=None, solar_current_power=850, solar_energy_today=2.32, solar_energy_total=5200.2, alarm_code=None, firmware='V4.08Build215', firmware_slave='V4.12Build246', inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
# name: test_inverter_js_devicearray_sofar2200tl
Inverter(serial_number='1234567890', model='SOFAR2200TL', solar_rated_power=2000, solar_current_power=400, solar_energy_today=5.67, solar_energy_total=12307.0, alarm_code=None, firmware='V450', firmware_slave=None, inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
# name: test_inverter_js_webdata
Inverter(serial_number='12345678910', model='omnik4000tl2', solar_rated_power=4000, solar_current_power=140, solar_energy_today=0.3, solar_energy_total=15363.7, alarm_code='F13', firmware='NL2-V9.8-5931', firmware_slave='V5.3-00157', inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
# name: test_inverter_json
Inverter(serial_number=None, model='omnik2000tl2', solar_rated_power=2000, solar_current_power=1225, solar_energy_today=10.9, solar_energy_total=8674.0, alarm_code='F23', firmware='V1.25Build23261', firmware_slave='V1.40Build52927', inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
# name: test_inverter_without_session
Inverter(serial_number='1234567890ABCDE', model='0079', solar_rated_power=None, solar_current_power=5850, solar_energy_today=9.8, solar_energy_total=44.0, alarm_code=None, firmware='001F', firmware_slave='002F', inverter_active=None, solar_hours_total=None, temperature=None, dc_input_voltage=None, dc_input_current=None, ac_output_voltage=None, ac_output_current=None, ac_output_frequency=None, ac_output_power=None)
# ---
151 changes: 53 additions & 98 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from aiohttp import ClientSession
from aresponses import ResponsesMockServer
from syrupy.assertion import SnapshotAssertion

from omnikinverter import Device, Inverter, OmnikInverter
from omnikinverter.exceptions import (
Expand All @@ -13,7 +14,9 @@
from . import load_fixtures


async def test_inverter_js_webdata(aresponses: ResponsesMockServer) -> None:
async def test_inverter_js_webdata(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from an Inverter - JS Webdata source."""
aresponses.add(
"example.com",
Expand All @@ -29,19 +32,12 @@ async def test_inverter_js_webdata(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", session=session)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware == "NL2-V9.8-5931"
assert inverter.firmware_slave == "V5.3-00157"
assert inverter.model == "omnik4000tl2"
assert inverter.alarm_code == "F13"
assert inverter.solar_rated_power == 4000
assert inverter.solar_current_power == 140
assert inverter.solar_energy_today == 0.3
assert inverter.solar_energy_total == 15363.7


async def test_device_js_webdata(aresponses: ResponsesMockServer) -> None:
assert inverter == snapshot


async def test_device_js_webdata(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from a Device - JS Webdata source."""
aresponses.add(
"example.com",
Expand All @@ -57,13 +53,12 @@ async def test_device_js_webdata(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", session=session)
device: Device = await client.device()
assert device
assert device.signal_quality == 96
assert device.firmware == "H4.01.38Y1.0.09W1.0.08"
assert device.ip_address == "192.168.0.10"
assert device == snapshot


async def test_inverter_html(aresponses: ResponsesMockServer) -> None:
async def test_inverter_html(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from an Inverter - HTML source."""
aresponses.add(
"example.com",
Expand All @@ -85,19 +80,12 @@ async def test_inverter_html(aresponses: ResponsesMockServer) -> None:
session=session,
)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware == "V5.07Build245"
assert inverter.firmware_slave is None
assert inverter.model == "Omnik2500tl"
assert inverter.alarm_code == "F13"
assert inverter.solar_rated_power == 2500
assert inverter.solar_current_power == 219
assert inverter.solar_energy_today == 0.23
assert inverter.solar_energy_total == 6454.5


async def test_device_html(aresponses: ResponsesMockServer) -> None:
assert inverter == snapshot


async def test_device_html(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from a Device - HTML source."""
aresponses.add(
"example.com",
Expand All @@ -119,13 +107,12 @@ async def test_device_html(aresponses: ResponsesMockServer) -> None:
session=session,
)
device: Device = await client.device()
assert device
assert device.signal_quality is None
assert device.firmware == "ME_08_0102_2.03"
assert device.ip_address == "192.168.0.106"
assert device == snapshot


async def test_inverter_without_session(aresponses: ResponsesMockServer) -> None:
async def test_inverter_without_session(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from an Inverter - HTML source and without session."""
aresponses.add(
"example.com",
Expand All @@ -145,19 +132,12 @@ async def test_inverter_without_session(aresponses: ResponsesMockServer) -> None
password="supercool", # noqa: S106
)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number == "1234567890ABCDE"
assert inverter.firmware == "001F"
assert inverter.firmware_slave == "002F"
assert inverter.model == "0079"
assert inverter.alarm_code is None
assert inverter.solar_rated_power is None
assert inverter.solar_current_power == 5850
assert inverter.solar_energy_today == 9.80
assert inverter.solar_energy_total == 44.0


async def test_device_without_session(aresponses: ResponsesMockServer) -> None:
assert inverter == snapshot


async def test_device_without_session(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from a Device - HTML source and without session."""
aresponses.add(
"example.com",
Expand All @@ -177,13 +157,12 @@ async def test_device_without_session(aresponses: ResponsesMockServer) -> None:
password="supercool", # noqa: S106
)
device: Device = await client.device()
assert device
assert device.signal_quality == 96
assert device.firmware == "MW_08_512_0501_1.82"
assert device.ip_address == "192.168.178.3"
assert device == snapshot


async def test_inverter_js_devicearray(aresponses: ResponsesMockServer) -> None:
async def test_inverter_js_devicearray(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from an Inverter - JS DeviceArray source."""
aresponses.add(
"example.com",
Expand All @@ -199,19 +178,12 @@ async def test_inverter_js_devicearray(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", session=session)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.firmware == "V4.08Build215"
assert inverter.firmware_slave == "V4.12Build246"
assert inverter.model == "Omnik1500tl"
assert inverter.solar_rated_power is None
assert inverter.solar_current_power == 850
assert inverter.solar_energy_today == 2.32
assert inverter.solar_energy_total == 5200.2
assert inverter == snapshot


async def test_inverter_js_devicearray_sofar2200tl(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
) -> None:
"""Test request from an SOFAR 2200TL Inverter - JS DeviceArray source."""
aresponses.add(
Expand All @@ -228,18 +200,12 @@ async def test_inverter_js_devicearray_sofar2200tl(
async with ClientSession() as session:
client = OmnikInverter(host="example.com", session=session)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number == "1234567890"
assert inverter.firmware == "V450"
assert inverter.firmware_slave is None
assert inverter.model == "SOFAR2200TL"
assert inverter.solar_rated_power == 2000
assert inverter.solar_current_power == 400
assert inverter.solar_energy_today == 5.67
assert inverter.solar_energy_total == 12307.0


async def test_device_js_devicearray(aresponses: ResponsesMockServer) -> None:
assert inverter == snapshot


async def test_device_js_devicearray(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from a Device - JS DeviceArray source."""
aresponses.add(
"example.com",
Expand All @@ -255,13 +221,12 @@ async def test_device_js_devicearray(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", session=session)
device: Device = await client.device()
assert device
assert device.signal_quality == 39
assert device.firmware == "H4.01.51MW.2.01W1.0.64(2018-01-251-D)"
assert device.ip_address is None
assert device == snapshot


async def test_inverter_json(aresponses: ResponsesMockServer) -> None:
async def test_inverter_json(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from an Inverter - JSON source."""
aresponses.add(
"example.com",
Expand All @@ -277,19 +242,12 @@ async def test_inverter_json(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", source_type="json", session=session)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number is None
assert inverter.firmware == "V1.25Build23261"
assert inverter.firmware_slave == "V1.40Build52927"
assert inverter.model == "omnik2000tl2"
assert inverter.alarm_code == "F23"
assert inverter.solar_rated_power == 2000
assert inverter.solar_current_power == 1225
assert inverter.solar_energy_today == 10.90
assert inverter.solar_energy_total == 8674.0


async def test_device_json(aresponses: ResponsesMockServer) -> None:
assert inverter == snapshot


async def test_device_json(
aresponses: ResponsesMockServer, snapshot: SnapshotAssertion
) -> None:
"""Test request from a Device - JSON source."""
aresponses.add(
"example.com",
Expand All @@ -305,10 +263,7 @@ async def test_device_json(aresponses: ResponsesMockServer) -> None:
async with ClientSession() as session:
client = OmnikInverter(host="example.com", source_type="json", session=session)
device: Device = await client.device()
assert device
assert device.signal_quality is None
assert device.firmware == "ME-111001-V1.0.6(2015-10-16)"
assert device.ip_address == "192.168.0.10"
assert device == snapshot


async def test_wrong_values(aresponses: ResponsesMockServer) -> None:
Expand Down