Skip to content

Commit b72ca70

Browse files
refactor(tapo): unit tests refactored, typo fixed
Signed-off-by: Marek Szczypiński <markacy@gmail.com>
1 parent 8221d6c commit b72ca70

3 files changed

Lines changed: 34 additions & 42 deletions

File tree

labgrid/driver/power/tapo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def _power_set(host: str, port: str, index: str, value: bool) -> None:
7373
await device.update()
7474

7575
if device.children:
76-
assert len(device.children) > index, "Trying to access non-existant plug socket on device"
76+
assert len(device.children) > index, "Trying to access non-existent plug socket on device"
7777

7878
target = device if not device.children else device.children[index]
7979
if value:
@@ -98,7 +98,7 @@ async def _power_get(host: str, port: str, index: str) -> bool:
9898
if not device.children:
9999
pwr_state = device.is_on
100100
else:
101-
assert len(device.children) > index, "Trying to access non-existant plug socket on device"
101+
assert len(device.children) > index, "Trying to access non-existent plug socket on device"
102102
pwr_state = device.children[index].is_on
103103
await device.disconnect()
104104
return pwr_state

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ dev = [
8484

8585
# additional dev dependencies
8686
"psutil>=5.8.0",
87-
"pytest-asyncio>=0.25.3",
8887
"pytest-benchmark>=4.0.0",
8988
"pytest-cov>=3.0.0",
9089
"pytest-dependency>=0.5.1",
@@ -227,6 +226,7 @@ include = [
227226
"labgrid/driver/power/pe6216.py",
228227
"labgrid/driver/power/poe_netgear_plus.py",
229228
"labgrid/driver/power/shelly_gen2.py",
229+
"labgrid/driver/power/tapo.py",
230230
"labgrid/driver/rawnetworkinterfacedriver.py",
231231
"labgrid/protocol/**/*.py",
232232
"labgrid/remote/**/*.py",

tests/test_tapo.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from labgrid.driver.power.tapo import _get_credentials, _power_get, _power_set, power_get
6+
from labgrid.driver.power.tapo import _get_credentials, power_get, power_set
77

88

99
@pytest.fixture
@@ -50,89 +50,81 @@ def test_credentials_valid(self, mock_env):
5050
assert creds.username == "test_user"
5151
assert creds.password == "test_pass"
5252

53-
@pytest.mark.asyncio
54-
async def test_power_get_single_plug_turn_on(self, mock_device_single_plug, mock_env):
53+
def test_power_get_single_plug_turn_on(self, mock_device_single_plug, mock_env):
5554
mock_device_single_plug.is_on = True
5655

5756
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
58-
result = await _power_get("192.168.1.100", None, "0")
57+
result = power_get("192.168.1.100", None, "0")
5958
assert result is True
6059

61-
@pytest.mark.asyncio
62-
async def test_power_get_single_plug_turn_off(self, mock_device_single_plug, mock_env):
60+
def test_power_get_single_plug_turn_off(self, mock_device_single_plug, mock_env):
6361
mock_device_single_plug.is_on = False
6462

6563
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
66-
result = await _power_get("192.168.1.100", None, "0")
64+
result = power_get("192.168.1.100", None, "0")
6765
assert result is False
6866

69-
@pytest.mark.asyncio
70-
async def test_power_get_single_plug_should_not_care_for_index(self, mock_device_single_plug, mock_env):
67+
def test_power_get_single_plug_should_not_care_for_index(self, mock_device_single_plug, mock_env):
7168
invalid_index_ignored = "7"
7269
mock_device_single_plug.is_on = True
7370

7471
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
75-
result = await _power_get("192.168.1.100", None, invalid_index_ignored)
72+
result = power_get("192.168.1.100", None, invalid_index_ignored)
7673
assert result is True
7774

78-
@pytest.mark.asyncio
79-
async def test_power_set_single_plug_turn_on(self, mock_device_single_plug, mock_env):
75+
def test_power_set_single_plug_turn_on(self, mock_device_single_plug, mock_env):
8076
mock_device_single_plug.is_on = False
8177
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
82-
await _power_set("192.168.1.100", None, "0", True)
78+
power_set("192.168.1.100", None, "0", True)
8379
mock_device_single_plug.turn_on.assert_called_once()
8480

85-
@pytest.mark.asyncio
86-
async def test_power_set_single_plug_turn_off(self, mock_device_single_plug, mock_env):
81+
def test_power_set_single_plug_turn_off(self, mock_device_single_plug, mock_env):
8782
mock_device_single_plug.is_on = True
8883
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
89-
await _power_set("192.168.1.100", None, "0", False)
84+
power_set("192.168.1.100", None, "0", False)
9085
mock_device_single_plug.turn_off.assert_called_once()
9186

92-
@pytest.mark.asyncio
93-
async def test_power_get_strip_valid_socket(self, mock_device_strip, mock_env):
87+
def test_power_get_strip_valid_socket(self, mock_device_strip, mock_env):
9488
with patch("kasa.Device.connect", return_value=mock_device_strip):
9589
# Test first outlet (on)
96-
result = await _power_get("192.168.1.100", None, "0")
90+
result = power_get("192.168.1.100", None, "0")
9791
assert result is True
9892

9993
# Test second outlet (off)
100-
result = await _power_get("192.168.1.100", None, "1")
94+
result = power_get("192.168.1.100", None, "1")
10195
assert result is False
10296

10397
# Test third outlet (on)
104-
result = await _power_get("192.168.1.100", None, "2")
98+
result = power_get("192.168.1.100", None, "2")
10599
assert result is True
106100

107-
@pytest.mark.asyncio
108-
async def test_power_set_strip_valid_socket(self, mock_device_strip, mock_env):
101+
def test_power_set_strip_valid_socket(self, mock_device_strip, mock_env):
109102
with patch("kasa.Device.connect", return_value=mock_device_strip):
110-
await _power_set("192.168.1.100", None, "0", False)
103+
power_set("192.168.1.100", None, "0", False)
111104
mock_device_strip.children[0].turn_off.assert_called_once()
112105

113-
await _power_set("192.168.1.100", None, "1", True)
106+
power_set("192.168.1.100", None, "1", True)
114107
mock_device_strip.children[1].turn_on.assert_called_once()
115108

116109
def test_power_get_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env):
117110
invalid_socket = "5"
118-
with patch("kasa.Device.connect", return_value=mock_device_strip):
119-
with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"):
120-
power_get("192.168.1.100", None, invalid_socket)
111+
with patch("kasa.Device.connect", return_value=mock_device_strip), \
112+
pytest.raises(AssertionError, match="Trying to access non-existent plug socket"):
113+
power_get("192.168.1.100", None, invalid_socket)
121114

122-
@pytest.mark.asyncio
123-
async def test_power_set_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env):
115+
def test_power_set_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env):
124116
invalid_socket = "5"
125-
with patch("kasa.Device.connect", return_value=mock_device_strip):
126-
with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"):
127-
await _power_set("192.168.1.100", None, invalid_socket, True)
117+
with patch("kasa.Device.connect", return_value=mock_device_strip), \
118+
pytest.raises(AssertionError, match="Trying to access non-existent plug socket"):
119+
power_set("192.168.1.100", None, invalid_socket, True)
128120

129121
def test_port_not_none_strip(self, mock_device_strip):
130-
with patch("kasa.Device.connect", return_value=mock_device_strip):
131-
with pytest.raises(AssertionError):
132-
power_get("192.168.1.100", "8080", "0")
122+
with patch("kasa.Device.connect", return_value=mock_device_strip), \
123+
pytest.raises(AssertionError):
124+
power_get("192.168.1.100", "8080", "0")
133125

134126
def test_port_not_none_single_socket(self, mock_device_single_plug):
135127
mock_device_single_plug.is_on = True
136-
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
137-
with pytest.raises(AssertionError):
138-
power_get("192.168.1.100", "8080", "0")
128+
with patch("kasa.Device.connect", return_value=mock_device_single_plug), \
129+
pytest.raises(AssertionError):
130+
power_get("192.168.1.100", "8080", "0")

0 commit comments

Comments
 (0)