|
| 1 | +import os |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from labgrid.driver.power.tapo import _get_credentials, _power_get, _power_set, power_get |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mock_device_strip(): |
| 11 | + device = AsyncMock() |
| 12 | + device.children = [ |
| 13 | + AsyncMock(is_on=True), |
| 14 | + AsyncMock(is_on=False), |
| 15 | + AsyncMock(is_on=True) |
| 16 | + ] |
| 17 | + return device |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def mock_device_single_plug(): |
| 22 | + device = AsyncMock() |
| 23 | + device.children = [] |
| 24 | + return device |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def mock_env(): |
| 29 | + os.environ['KASA_USERNAME'] = 'test_user' |
| 30 | + os.environ['KASA_PASSWORD'] = 'test_pass' |
| 31 | + yield |
| 32 | + del os.environ['KASA_USERNAME'] |
| 33 | + del os.environ['KASA_PASSWORD'] |
| 34 | + |
| 35 | + |
| 36 | +class TestTapoPowerDriver: |
| 37 | + def test_get_credentials_should_raise_value_error_when_credentials_missing(self): |
| 38 | + # Save existing environment variables |
| 39 | + saved_username = os.environ.pop('KASA_USERNAME', None) |
| 40 | + saved_password = os.environ.pop('KASA_PASSWORD', None) |
| 41 | + |
| 42 | + try: |
| 43 | + with pytest.raises(EnvironmentError, match="KASA_USERNAME or KASA_PASSWORD environment variable not set"): |
| 44 | + _get_credentials() |
| 45 | + finally: |
| 46 | + # Restore environment variables if they existed |
| 47 | + if saved_username is not None: |
| 48 | + os.environ['KASA_USERNAME'] = saved_username |
| 49 | + if saved_password is not None: |
| 50 | + os.environ['KASA_PASSWORD'] = saved_password |
| 51 | + |
| 52 | + def test_credentials_valid(self, mock_env): |
| 53 | + creds = _get_credentials() |
| 54 | + assert creds.username == 'test_user' |
| 55 | + assert creds.password == 'test_pass' |
| 56 | + |
| 57 | + @pytest.mark.asyncio |
| 58 | + async def test_power_get_single_plug_turn_on(self, mock_device_single_plug, mock_env): |
| 59 | + mock_device_single_plug.is_on = True |
| 60 | + |
| 61 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 62 | + result = await _power_get('192.168.1.100', None, "0") |
| 63 | + assert result is True |
| 64 | + |
| 65 | + @pytest.mark.asyncio |
| 66 | + async def test_power_get_single_plug_turn_off(self, mock_device_single_plug, mock_env): |
| 67 | + mock_device_single_plug.is_on = False |
| 68 | + |
| 69 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 70 | + result = await _power_get('192.168.1.100', None, "0") |
| 71 | + assert result is False |
| 72 | + |
| 73 | + @pytest.mark.asyncio |
| 74 | + async def test_power_get_single_plug_should_not_care_for_index(self, mock_device_single_plug, mock_env): |
| 75 | + invalid_index_ignored = "7" |
| 76 | + mock_device_single_plug.is_on = True |
| 77 | + |
| 78 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 79 | + result = await _power_get('192.168.1.100', None, invalid_index_ignored) |
| 80 | + assert result is True |
| 81 | + |
| 82 | + @pytest.mark.asyncio |
| 83 | + async def test_power_set_single_plug_turn_on(self, mock_device_single_plug, mock_env): |
| 84 | + mock_device_single_plug.is_on = False |
| 85 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 86 | + await _power_set('192.168.1.100', None, "0", True) |
| 87 | + mock_device_single_plug.turn_on.assert_called_once() |
| 88 | + |
| 89 | + @pytest.mark.asyncio |
| 90 | + async def test_power_set_single_plug_turn_off(self, mock_device_single_plug, mock_env): |
| 91 | + mock_device_single_plug.is_on = True |
| 92 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 93 | + await _power_set('192.168.1.100', None, "0", False) |
| 94 | + mock_device_single_plug.turn_off.assert_called_once() |
| 95 | + |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_power_get_strip_valid_socket(self, mock_device_strip, mock_env): |
| 98 | + with patch('kasa.Device.connect', return_value=mock_device_strip): |
| 99 | + # Test first outlet (on) |
| 100 | + result = await _power_get('192.168.1.100', None, "0") |
| 101 | + assert result is True |
| 102 | + |
| 103 | + # Test second outlet (off) |
| 104 | + result = await _power_get('192.168.1.100', None, "1") |
| 105 | + assert result is False |
| 106 | + |
| 107 | + # Test third outlet (on) |
| 108 | + result = await _power_get('192.168.1.100', None, "2") |
| 109 | + assert result is True |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_power_set_strip_valid_socket(self, mock_device_strip, mock_env): |
| 113 | + with patch('kasa.Device.connect', return_value=mock_device_strip): |
| 114 | + await _power_set('192.168.1.100', None, "0", False) |
| 115 | + mock_device_strip.children[0].turn_off.assert_called_once() |
| 116 | + |
| 117 | + await _power_set('192.168.1.100', None, "1", True) |
| 118 | + mock_device_strip.children[1].turn_on.assert_called_once() |
| 119 | + |
| 120 | + def test_power_get_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env): |
| 121 | + invalid_socket = "5" |
| 122 | + with patch('kasa.Device.connect', return_value=mock_device_strip): |
| 123 | + with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"): |
| 124 | + power_get('192.168.1.100', None, invalid_socket) |
| 125 | + |
| 126 | + @pytest.mark.asyncio |
| 127 | + async def test_power_set_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env): |
| 128 | + invalid_socket = "5" |
| 129 | + with patch('kasa.Device.connect', return_value=mock_device_strip): |
| 130 | + with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"): |
| 131 | + await _power_set('192.168.1.100', None, invalid_socket, True) |
| 132 | + |
| 133 | + def test_port_not_none_strip(self, mock_device_strip): |
| 134 | + with patch('kasa.Device.connect', return_value=mock_device_strip): |
| 135 | + with pytest.raises(AssertionError): |
| 136 | + power_get('192.168.1.100', '8080', "0") |
| 137 | + |
| 138 | + def test_port_not_none_single_socket(self, mock_device_single_plug): |
| 139 | + mock_device_single_plug.is_on = True |
| 140 | + with patch('kasa.Device.connect', return_value=mock_device_single_plug): |
| 141 | + with pytest.raises(AssertionError): |
| 142 | + power_get('192.168.1.100', '8080', "0") |
0 commit comments