File: test_device.py

package info (click to toggle)
python-demetriek 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 544 kB
  • sloc: python: 1,400; makefile: 3
file content (105 lines) | stat: -rw-r--r-- 3,196 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Asynchronous Python client for LaMetric TIME devices."""

from dataclasses import asdict

import aiohttp
import pytest
from aresponses import ResponsesMockServer
from syrupy.assertion import SnapshotAssertion

from demetriek import (
    Chart,
    Goal,
    GoalData,
    LaMetricDevice,
    Model,
    Notification,
    NotificationIconType,
    NotificationSound,
    Simple,
    Sound,
)
from demetriek.const import (
    NotificationType,
)

from . import load_fixture


@pytest.mark.parametrize(
    "fixture",
    [
        "device.json",
        "device2.json",
        "device3.json",
    ],
)
async def test_get_device(
    aresponses: ResponsesMockServer, fixture: str, snapshot: SnapshotAssertion
) -> None:
    """Test getting device information."""
    aresponses.add(
        "127.0.0.2:4343",
        "/api/v2/device",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture(fixture),
        ),
    )
    async with aiohttp.ClientSession() as session:
        demetriek = LaMetricDevice(host="127.0.0.2", api_key="abc", session=session)
        device = await demetriek.device()

    assert asdict(device) == snapshot


async def test_notify(aresponses: ResponsesMockServer) -> None:
    """Test sending notification serialization."""
    aresponses.add(
        "127.0.0.2:4343",
        "/api/v2/device/notifications",
        "POST",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("notification.json"),
        ),
    )
    async with aiohttp.ClientSession() as session:
        demetriek = LaMetricDevice(host="127.0.0.2", api_key="abc", session=session)
        notification = Notification(
            icon_type=NotificationIconType.ALERT,
            notification_type=NotificationType.EXTERNAL,
            model=Model(
                frames=[
                    Simple(text="Yeah", icon=18815),
                    Goal(
                        icon=7956,
                        data=GoalData(
                            current=65,
                            end=100,
                            start=0,
                            unit="%",
                        ),
                    ),
                    Chart(data=[1, 2, 3, 4, 5, 4, 3, 2, 1]),
                ],
                sound=Sound(sound=NotificationSound.WIN),
            ),
        )
        response = await demetriek.notify(notification=notification)

    # check response
    assert response == 1
    # check on serialized request if aliases are used and null values are removed
    request = await aresponses.history[0].request.json()
    assert request["type"] == "external"
    assert request["icon_type"] == "alert"
    assert "life_time" not in request
    assert request["model"]["sound"]["id"] == "win"
    assert request["model"]["sound"]["category"] == "notifications"
    assert request["model"]["frames"][0]["text"] == "Yeah"
    assert request["model"]["frames"][1]["goalData"]["current"] == 65
    assert request["model"]["frames"][2]["chartData"] == [1, 2, 3, 4, 5, 4, 3, 2, 1]