11"""Test the interaction with the Glances API."""
2+ from typing import Any
3+
24import pytest
5+ from pytest_httpx import HTTPXMock
6+
37from glances_api import Glances
48from glances_api .exceptions import GlancesApiNoDataAvailable
5- from pytest_httpx import HTTPXMock
69
710PLUGINS_LIST_RESPONSE = [
811 "alert" ,
1720 "fs" ,
1821]
1922
20- RESPONSE = {
23+ RESPONSE : dict [ str , Any ] = {
2124 "cpu" : {
2225 "total" : 10.6 ,
2326 "user" : 7.6 ,
241244 "uptime" : "3 days, 10:25:20" ,
242245}
243246
244- HA_SENSOR_DATA = {
247+ HA_SENSOR_DATA : dict [ str , Any ] = {
245248 "fs" : {
246249 "/ssl" : {"disk_use" : 30.7 , "disk_use_percent" : 6.7 , "disk_free" : 426.5 },
247250 "/media" : {"disk_use" : 30.7 , "disk_use_percent" : 6.7 , "disk_free" : 426.5 },
267270
268271
269272@pytest .mark .asyncio
270- async def test_non_existing_endpoint (httpx_mock : HTTPXMock ):
273+ async def test_non_existing_endpoint (httpx_mock : HTTPXMock ) -> None :
271274 """Test a non-exisiting endpoint."""
272275 httpx_mock .add_response (status_code = 400 )
273276
@@ -279,7 +282,7 @@ async def test_non_existing_endpoint(httpx_mock: HTTPXMock):
279282
280283
281284@pytest .mark .asyncio
282- async def test_plugins_list (httpx_mock : HTTPXMock ):
285+ async def test_plugins_list (httpx_mock : HTTPXMock ) -> None :
283286 """Test the plugins list response."""
284287 httpx_mock .add_response (json = PLUGINS_LIST_RESPONSE )
285288
@@ -290,19 +293,19 @@ async def test_plugins_list(httpx_mock: HTTPXMock):
290293
291294
292295@pytest .mark .asyncio
293- async def test_exisiting_endpoint (httpx_mock : HTTPXMock ):
296+ async def test_exisiting_endpoint (httpx_mock : HTTPXMock ) -> None :
294297 """Test the a valid endpoint."""
295298 httpx_mock .add_response (json = RESPONSE )
296299
297300 client = Glances ()
298301 await client .get_metrics ("cpu" )
299-
302+ assert client . values
300303 assert client .values ["total" ] == 10.6
301304 assert client .values ["system" ] == 2.1
302305
303306
304307@pytest .mark .asyncio
305- async def test_ha_sensor_data (httpx_mock : HTTPXMock ):
308+ async def test_ha_sensor_data (httpx_mock : HTTPXMock ) -> None :
306309 """Test the return value for ha sensors."""
307310 httpx_mock .add_response (json = RESPONSE )
308311
@@ -315,21 +318,21 @@ async def test_ha_sensor_data(httpx_mock: HTTPXMock):
315318@pytest .mark .asyncio
316319async def test_ha_sensor_data_with_incomplete_container_information (
317320 httpx_mock : HTTPXMock ,
318- ):
319- """Test the return value for ha sensors when container memory and cpu data is not exposed by glances ."""
320- TEST_RESPONSE = RESPONSE
321- del TEST_RESPONSE ["containers" ]["containers" ][0 ]["memory" ]["usage" ]
322- del TEST_RESPONSE ["containers" ]["containers" ][0 ]["cpu" ]["total" ]
323- del TEST_RESPONSE ["containers" ]["containers" ][1 ]["memory" ]["usage" ]
324- del TEST_RESPONSE ["containers" ]["containers" ][1 ]["cpu" ]["total" ]
321+ ) -> None :
322+ """Test the return value for ha sensors when some data is not exposed."""
323+ response = RESPONSE . copy ()
324+ del response ["containers" ]["containers" ][0 ]["memory" ]["usage" ]
325+ del response ["containers" ]["containers" ][0 ]["cpu" ]["total" ]
326+ del response ["containers" ]["containers" ][1 ]["memory" ]["usage" ]
327+ del response ["containers" ]["containers" ][1 ]["cpu" ]["total" ]
325328
326- TEST_HA_SENSOR_DATA = HA_SENSOR_DATA
327- TEST_HA_SENSOR_DATA ["docker" ]["docker_memory_use" ] = 0
328- TEST_HA_SENSOR_DATA ["docker" ]["docker_cpu_use" ] = 0
329+ ha_sensor_data = HA_SENSOR_DATA . copy ()
330+ ha_sensor_data ["docker" ]["docker_memory_use" ] = 0
331+ ha_sensor_data ["docker" ]["docker_cpu_use" ] = 0
329332
330- httpx_mock .add_response (json = TEST_RESPONSE )
333+ httpx_mock .add_response (json = response )
331334
332335 client = Glances ()
333336 result = await client .get_ha_sensor_data ()
334337
335- assert result == TEST_HA_SENSOR_DATA
338+ assert result == ha_sensor_data
0 commit comments