From 102e3921e7f8da4b378adcac1ca401b1763f769c Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Mon, 11 Nov 2024 23:39:56 +0100 Subject: [PATCH] Use syrupy for model testing (#830) * Update dependencies: bump Poetry to 1.8.4 and add Syrupy 4.7.2 * Add snapshot tests for Device and SmartBridge models --- poetry.lock | 18 ++++++++++++++++-- pyproject.toml | 5 +++-- tests/__snapshots__/test_models.ambr | 7 +++++++ tests/test_models.py | 24 +++++++++++------------- 4 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 tests/__snapshots__/test_models.ambr diff --git a/poetry.lock b/poetry.lock index 54328fc..2b21154 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "aiohappyeyeballs" @@ -1125,6 +1125,20 @@ files = [ {file = "ruff-0.7.3.tar.gz", hash = "sha256:e1d1ba2e40b6e71a61b063354d04be669ab0d39c352461f3d789cac68b54a313"}, ] +[[package]] +name = "syrupy" +version = "4.7.2" +description = "Pytest Snapshot Test Utility" +optional = false +python-versions = ">=3.8.1" +files = [ + {file = "syrupy-4.7.2-py3-none-any.whl", hash = "sha256:eae7ba6be5aed190237caa93be288e97ca1eec5ca58760e4818972a10c4acc64"}, + {file = "syrupy-4.7.2.tar.gz", hash = "sha256:ea45e099f242de1bb53018c238f408a5bb6c82007bc687aefcbeaa0e1c2e935a"}, +] + +[package.dependencies] +pytest = ">=7.0.0,<9.0.0" + [[package]] name = "tomlkit" version = "0.13.2" @@ -1284,4 +1298,4 @@ propcache = ">=0.2.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "9b95fdffe731753c324fd1eabc30ff3927de227876c8b3b00ca6fc20256e03b7" +content-hash = "57fec9ed0b019a271b166dd51fdf027e1976d984ed2d7894b18fcbed0bf770dd" diff --git a/pyproject.toml b/pyproject.toml index 246f24f..654431e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,9 +36,9 @@ yarl = ">=1.6.0" Changelog = "https://github.com/klaasnicolaas/python-gridnet/releases" [tool.poetry.group.dev.dependencies] -ruff = "0.7.3" aresponses = "3.0.0" codespell = "2.3.0" +covdefaults = "2.3.0" coverage = {version = "7.6.4", extras = ["toml"]} mypy = "1.13.0" pre-commit = "4.0.1" @@ -47,8 +47,9 @@ pylint = "3.3.1" 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" -covdefaults = "2.3.0" [tool.coverage.run] plugins = ["covdefaults"] diff --git a/tests/__snapshots__/test_models.ambr b/tests/__snapshots__/test_models.ambr new file mode 100644 index 0000000..fc1bcd5 --- /dev/null +++ b/tests/__snapshots__/test_models.ambr @@ -0,0 +1,7 @@ +# serializer version: 1 +# name: test_device + Device(n2g_id='84df:0c11:9999:3795', model='SBWF3102', batch='SBP-HMX-210318', firmware='1.6.16', hardware=1, manufacturer='NET2GRID') +# --- +# name: test_smartbridge + SmartBridge(power_flow=338, energy_consumption_total=17762.1, energy_production_total=21214.6) +# --- diff --git a/tests/test_models.py b/tests/test_models.py index f30a2aa..78f05bb 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,13 +1,18 @@ """Test the models.""" from aresponses import ResponsesMockServer +from syrupy.assertion import SnapshotAssertion from gridnet import Device, GridNet, SmartBridge from . import load_fixtures -async def test_device(aresponses: ResponsesMockServer, gridnet_client: GridNet) -> None: +async def test_device( + aresponses: ResponsesMockServer, + snapshot: SnapshotAssertion, + gridnet_client: GridNet, +) -> None: """Test request from the device - Device object.""" aresponses.add( "127.0.0.1", @@ -19,17 +24,13 @@ async def test_device(aresponses: ResponsesMockServer, gridnet_client: GridNet) ), ) device: Device = await gridnet_client.device() - assert device - assert device.n2g_id == "84df:0c11:9999:3795" - assert device.model == "SBWF3102" - assert device.batch == "SBP-HMX-210318" - assert device.firmware == "1.6.16" - assert device.hardware == 1 - assert device.manufacturer == "NET2GRID" + assert device == snapshot async def test_smartbridge( - aresponses: ResponsesMockServer, gridnet_client: GridNet + aresponses: ResponsesMockServer, + snapshot: SnapshotAssertion, + gridnet_client: GridNet, ) -> None: """Test request from the device - SmartBridge object.""" aresponses.add( @@ -42,7 +43,4 @@ async def test_smartbridge( ), ) smartbridge: SmartBridge = await gridnet_client.smartbridge() - assert smartbridge - assert smartbridge.power_flow == 338 - assert smartbridge.energy_consumption_total == 17762.1 - assert smartbridge.energy_production_total == 21214.6 + assert smartbridge == snapshot