-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_assert_that.py
More file actions
171 lines (136 loc) · 6.04 KB
/
test_assert_that.py
File metadata and controls
171 lines (136 loc) · 6.04 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import appdaemon.plugins.hass.hassapi as hass
import pytest
from appdaemontestframework import automation_fixture
"""
Note:
The Appdaemon test framework was the fruit of a refactor of the tests
suite of one of the Appdaemon projects I was working on at the time.
Because it didn't start as a standalone project itself but was part of a test
suite, it didn't have tests itself.
(lessons learned, now my 'heavy' tests helpers are themselves tested :)
Anyways, what that means is: Most of the base functionality is tested only
through `integration_tests`.
New feature should come with the proper unit tests.
"""
LIGHT = 'light.some_light'
SWITCH = 'switch.some_switch'
TRANSITION_DURATION = 2
class MockAutomation(hass.Hass):
def initialize(self):
pass
def turn_on_light(self, via_helper=False):
if via_helper:
self.turn_on(LIGHT)
else:
self.call_service('light/turn_on', entity_id=LIGHT)
def turn_off_light(self, via_helper=False):
if via_helper:
self.turn_off(LIGHT)
else:
self.call_service('light/turn_off', entity_id=LIGHT)
def turn_on_switch(self, via_helper=False):
if via_helper:
self.turn_on(SWITCH)
else:
self.call_service('switch/turn_on', entity_id=SWITCH)
def turn_off_switch(self, via_helper=False):
if via_helper:
self.turn_off(SWITCH)
else:
self.call_service('switch/turn_off', entity_id=SWITCH)
def turn_on_light_with_transition(self, via_helper=False):
if via_helper:
self.turn_on(LIGHT, transition=TRANSITION_DURATION)
else:
self.call_service(
'light/turn_on',
entity_id=LIGHT,
transition=TRANSITION_DURATION
)
def turn_off_light_with_transition(self, via_helper=False):
if via_helper:
self.turn_off(LIGHT, transition=TRANSITION_DURATION)
else:
self.call_service(
'light/turn_off',
entity_id=LIGHT,
transition=TRANSITION_DURATION
)
def call_invalid_service_name(self):
self.call_service(
'switch.turn_off',
entity_id=SWITCH
)
@automation_fixture(MockAutomation)
def automation():
pass
class TestTurnedOn:
class TestViaService:
def test_was_turned_on(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_on()
automation.turn_on_light()
assert_that(LIGHT).was.turned_on()
assert_that(SWITCH).was_not.turned_on()
automation.turn_on_switch()
assert_that(SWITCH).was.turned_on()
def test_with_kwargs(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_on()
automation.turn_on_light_with_transition()
assert_that(LIGHT).was.turned_on(transition=TRANSITION_DURATION)
class TestViaHelper:
def test_was_turned_on(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_on()
automation.turn_on_light(via_helper=True)
assert_that(LIGHT).was.turned_on()
assert_that(SWITCH).was_not.turned_on()
automation.turn_on_switch(via_helper=True)
assert_that(SWITCH).was.turned_on()
def test_with_kwargs(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_on()
automation.turn_on_light_with_transition(via_helper=True)
assert_that(LIGHT).was.turned_on(transition=TRANSITION_DURATION)
class TestTurnedOff:
class TestViaService:
def test_was_turned_off(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_off()
automation.turn_off_light()
assert_that(LIGHT).was.turned_off()
assert_that(SWITCH).was_not.turned_off()
automation.turn_off_switch()
assert_that(SWITCH).was.turned_off()
def test_with_kwargs(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_off()
automation.turn_off_light_with_transition()
assert_that(LIGHT).was.turned_off(transition=TRANSITION_DURATION)
class TestViaHelper:
def test_was_turned_off(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_off()
automation.turn_off_light(via_helper=True)
assert_that(LIGHT).was.turned_off()
assert_that(SWITCH).was_not.turned_off()
automation.turn_off_switch(via_helper=True)
assert_that(SWITCH).was.turned_off()
def test_with_kwargs(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_off()
automation.turn_off_light_with_transition(via_helper=True)
assert_that(LIGHT).was.turned_off(transition=TRANSITION_DURATION)
class TestServiceNameValidation:
class TestValidServiceName:
def test_valid_service_asserted_and_is_called_does_not_raise(self, assert_that, automation):
automation.turn_off_switch(via_helper=False)
assert_that('switch/turn_off') \
.was.called_with(entity_id=SWITCH)
def test_valid_service_asserted_and_is_not_called_raises_assertion_error(self, assert_that, automation):
with pytest.raises(AssertionError):
assert_that('switch/turn_off') \
.was.called_with(entity_id=SWITCH)
class TestInvalidServiceName:
def test_invalid_service_asserted_and_is_called_raises_value_error(self, assert_that, automation):
automation.call_invalid_service_name()
with pytest.raises(ValueError):
assert_that('switch.turn_off')\
.was.called_with(entity_id=SWITCH)
def test_invalid_service_asserted_and_is_not_called_raises_value_or_assertion_error(self, assert_that, automation):
with pytest.raises((ValueError, AssertionError)):
assert_that('switch.turn_off')\
.was.called_with(entity_id=SWITCH)