File: test_library.py

package info (click to toggle)
aioruuvigateway 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: python: 225; makefile: 6; sh: 5
file content (53 lines) | stat: -rw-r--r-- 1,561 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
from collections import Counter
from pathlib import Path

import httpx
import pytest

from aioruuvigateway.api import get_gateway_history_data
from aioruuvigateway.excs import InvalidAuth

example_path = Path(__file__).parent / "example.json"


@pytest.mark.asyncio
async def test_library(httpx_mock):
    httpx_mock.add_response(
        url="http://192.168.1.202/history",
        content=example_path.read_bytes(),
        headers={"Content-Type": "application/json"},
    )
    async with httpx.AsyncClient() as client:
        history = await get_gateway_history_data(
            client=client,
            host="192.168.1.202",
            bearer_token="bear, a scary bear",
        )
    assert history.gw_mac_suffix == "EE:FF"
    manufacturers = Counter()
    for tag in history.tags:
        assert tag.datetime.year == 2022
        assert tag.age_seconds in {0, 1, 19}
        ann = tag.parse_announcement()
        print(tag, ann)
        manufacturers.update(ann.manufacturer_data.keys())
    assert manufacturers == {
        0x0499: 2,  # Two Ruuvitags
        0x012D: 1,  # One Sony
        0x004C: 2,  # Two Apples
    }


@pytest.mark.asyncio
async def test_auth(httpx_mock):
    httpx_mock.add_response(
        url="http://192.168.1.202/history",
        status_code=401,
    )
    async with httpx.AsyncClient() as client:
        with pytest.raises(InvalidAuth):
            await get_gateway_history_data(
                client=client,
                host="192.168.1.202",
                bearer_token="not good at all",
            )