-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtest_loader.py
75 lines (56 loc) · 2.31 KB
/
test_loader.py
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
"""Tests for pyhap.loader."""
import pytest
from pyhap import CHARACTERISTICS_FILE, SERVICES_FILE
from pyhap.characteristic import Characteristic
from pyhap.loader import Loader, get_loader
from pyhap.service import Service
def test_loader_char():
"""Test if method returns a Characteristic object."""
loader = Loader()
with pytest.raises(KeyError):
loader.get_char("Not a char")
char_name = loader.get_char("Name")
assert char_name is not None
assert isinstance(char_name, Characteristic)
def test_loader_get_char_error():
"""Test if errors are thrown for invalid dictionary entries."""
loader = Loader.from_dict(char_dict={"Char": None})
assert loader.char_types == {"Char": None}
assert loader.serv_types == {}
json_dicts = (
{"Format": "int", "Permissions": "read"},
{"Format": "int", "UUID": "123456"},
{"Permissions": "read", "UUID": "123456"},
)
for case in json_dicts:
loader.char_types["Char"] = case
with pytest.raises(KeyError):
loader.get_char("Char")
def test_loader_service():
"""Test if method returns a Service object."""
loader = Loader()
with pytest.raises(KeyError):
loader.get_service("Not a service")
serv_acc_info = loader.get_service("AccessoryInformation")
assert serv_acc_info is not None
assert isinstance(serv_acc_info, Service)
def test_loader_service_error():
"""Test if errors are thrown for invalid dictionary entries."""
loader = Loader.from_dict(serv_dict={"Service": None})
assert loader.char_types == {}
assert loader.serv_types == {"Service": None}
json_dicts = ({"RequiredCharacteristics": ["Char 1", "Char 2"]}, {"UUID": "123456"})
for case in json_dicts:
loader.serv_types["Service"] = case
with pytest.raises(KeyError):
loader.get_service("Service")
def test_get_loader():
"""Test if method returns the preloaded loader object."""
loader = get_loader()
assert isinstance(loader, Loader)
assert loader.char_types is not ({} or None)
assert loader.serv_types is not ({} or None)
loader2 = Loader(path_char=CHARACTERISTICS_FILE, path_service=SERVICES_FILE)
assert loader.char_types == loader2.char_types
assert loader.serv_types == loader2.serv_types
assert get_loader() == loader