File: test_open_api.py

package info (click to toggle)
python-aioambient 2024.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: python: 755; sh: 41; makefile: 5
file content (67 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Define tests for the REST API."""
import re

import aiohttp
import pytest
from aresponses import ResponsesMockServer

from aioambient import OpenAPI

from .common import TEST_MAC, load_fixture


@pytest.mark.asyncio
async def test_get_device_details(aresponses: ResponsesMockServer) -> None:
    """Test retrieving device details from the open REST API.

    Args:
        aresponses: An aresponses server.
    """
    aresponses.add(
        "lightning.ambientweather.net",
        f"/devices/{TEST_MAC}",
        "get",
        aresponses.Response(
            text=load_fixture("device_details_open_response.json"),
            status=200,
            headers={"Content-Type": "application/json; charset=utf-8"},
        ),
    )

    async with aiohttp.ClientSession() as session:
        api = OpenAPI(session=session)

        device_details = await api.get_device_details(TEST_MAC)
        assert "lastData" in device_details
        assert "dewPoint" in device_details["lastData"]
        assert device_details["lastData"]["dewPoint"] == 67.56184884292183
        assert "feelsLike" in device_details["lastData"]
        assert device_details["lastData"]["feelsLike"] == 85.76260552070016


@pytest.mark.asyncio
async def test_get_devices_by_location(aresponses: ResponsesMockServer) -> None:
    """Test retrieving devices from the open REST API.

    Args:
        aresponses: An aresponses server.
    """
    aresponses.add(
        "lightning.ambientweather.net",
        re.compile(r"/devices.*"),
        "get",
        aresponses.Response(
            text=load_fixture("devices_by_location_open_response.json"),
            status=200,
            headers={"Content-Type": "application/json; charset=utf-8"},
        ),
    )

    async with aiohttp.ClientSession() as session:
        api = OpenAPI(session=session)

        devices = await api.get_devices_by_location(32.5, -97.3, 1.5)
        assert len(devices) == 6

        assert "dewPoint" in devices[0]["lastData"]
        assert "feelsLike" in devices[0]["lastData"]