File: test_misc.py

package info (click to toggle)
python-homematicip 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,796 kB
  • sloc: python: 15,164; makefile: 17; sh: 4
file content (57 lines) | stat: -rw-r--r-- 2,216 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
import pytest

from homematicip.base.enums import *
from homematicip.base.helpers import bytes2str, detect_encoding
from homematicip.base.homematicip_object import HomeMaticIPObject
from homematicip.EventHook import EventHook


def event_hook_handler2(mustBe2):
    assert mustBe2 == 2


def event_hook_handler3(mustBe3):
    assert mustBe3 == 3


def test_event_hook():
    eh = EventHook()
    eh += event_hook_handler2
    eh.fire(2)
    eh += event_hook_handler3
    eh -= event_hook_handler2
    eh.fire(3)


def test_detect_encoding():
    testString = "This is my special string to test the encoding"
    assert detect_encoding(testString.encode("utf-8")) == "utf-8"
    assert detect_encoding(testString.encode("utf-8-sig")) == "utf-8-sig"
    assert detect_encoding(testString.encode("utf-16")) == "utf-16"
    assert detect_encoding(testString.encode("utf-32")) == "utf-32"
    assert detect_encoding(testString.encode("utf-16-be")) == "utf-16-be"
    assert detect_encoding(testString.encode("utf-32-be")) == "utf-32-be"
    assert detect_encoding(testString.encode("utf-16-le")) == "utf-16-le"
    assert detect_encoding(testString.encode("utf-32-le")) == "utf-32-le"


def test_bytes2str():
    testString = "This is my special string to test the encoding"
    assert bytes2str(testString.encode("utf-8")) == testString
    assert bytes2str(testString.encode("utf-8-sig")) == testString
    assert bytes2str(testString.encode("utf-16")) == testString
    assert bytes2str(testString.encode("utf-32")) == testString
    assert bytes2str(testString.encode("utf-16-be")) == testString
    assert bytes2str(testString.encode("utf-32-be")) == testString
    assert bytes2str(testString.encode("utf-16-le")) == testString
    assert bytes2str(testString.encode("utf-32-le")) == testString
    assert bytes2str(testString) == testString
    with pytest.raises(TypeError):
        assert bytes2str(44) == testString


def test_auto_name_enum():
    assert DeviceType.from_str("PUSH_BUTTON") == DeviceType.PUSH_BUTTON
    assert DeviceType.from_str(None) is None
    assert DeviceType.from_str("I_DONT_EXIST", DeviceType.DEVICE) == DeviceType.DEVICE
    assert DeviceType.from_str("I_DONT_EXIST_EITHER") is None