File: test_utils.py

package info (click to toggle)
pybalboa 1.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: python: 1,783; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (2)
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
"""Tests module."""

import asyncio

from pybalboa.utils import (
    byte_parser,
    calculate_checksum,
    cancel_task,
    default,
    to_celsius,
)


def test_byte_parser() -> None:
    """Test byte_parser."""
    byte = int("0b01010101", 2)
    assert byte_parser(byte) == [1, 0] * 4
    assert byte_parser(byte, offset=1, count=3) == [0, 1, 0]
    assert byte_parser(byte, count=2, bits=2) == [1, 1]
    assert byte_parser(byte, offset=1, count=2, bits=2) == [2, 2]
    assert byte_parser(byte, count=3, bits=3) == [5, 2, 1]


def test_calculate_checksum() -> None:
    """Test calculate_checksum."""
    value = bytes.fromhex("1DFFAF13000064082D0000010000040000000000000000006400000006")
    assert calculate_checksum(value[:-1]) == value[-1]
    value = bytes.fromhex("050ABF0477")
    assert calculate_checksum(value[:-1]) == value[-1]


async def test_cancel_task() -> None:
    """Test cancel_task."""

    async def _long_wait() -> None:
        await asyncio.sleep(1000)

    task = asyncio.ensure_future(test_cancel_task())
    assert not task.done()
    await cancel_task(task)
    assert task.cancelled()


def test_default() -> None:
    """Test default."""
    assert default(12, 24) == 12
    assert default(None, 24) == 24


def test_to_celsius() -> None:
    """Test to_celsius."""
    assert to_celsius(32) == 0
    assert to_celsius(104) == 40
    assert to_celsius(80) == 26.5