-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_basic_functionality.py
More file actions
226 lines (179 loc) · 6.71 KB
/
test_basic_functionality.py
File metadata and controls
226 lines (179 loc) · 6.71 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
"""Basic functionality tests for Smart Irrigation without full HA setup."""
import asyncio
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, Mock
# Add the repository root to Python path
repo_root = Path(__file__).parent
if str(repo_root) not in sys.path:
sys.path.insert(0, str(repo_root))
# Mock problematic HA modules early
sys.modules["homeassistant.helpers"] = MagicMock()
sys.modules["homeassistant.helpers.device_registry"] = MagicMock()
sys.modules["homeassistant.helpers.entity_registry"] = MagicMock()
sys.modules["homeassistant.components.frontend"] = MagicMock()
sys.modules["homeassistant.components.websocket_api"] = MagicMock()
sys.modules["homeassistant.components.http"] = MagicMock()
sys.modules["homeassistant.components.logger"] = MagicMock()
sys.modules["homeassistant.components.system_log"] = MagicMock()
async def test_basic_imports():
"""Test that basic module imports work."""
try:
from custom_components.smart_irrigation import const
print("✓ Constants import successful")
from custom_components.smart_irrigation.helpers import (
CannotConnect,
InvalidAuth,
altitude_to_pressure,
convert_between_temperature,
)
print("✓ Helper functions import successful")
from custom_components.smart_irrigation.store import SmartIrrigationStore
print("✓ Store import successful")
from custom_components.smart_irrigation.performance import performance_timer
print("✓ Performance utilities import successful")
return True
except Exception as e:
print(f"✗ Import failed: {e}")
return False
async def test_helper_functions():
"""Test basic helper functions."""
try:
from custom_components.smart_irrigation.helpers import (
CannotConnect,
InvalidAuth,
altitude_to_pressure,
convert_between_temperature,
)
# Test pressure conversion
pressure = altitude_to_pressure(100, 1013.25)
assert isinstance(pressure, float)
assert pressure < 1013.25
print("✓ Pressure conversion working")
# Test temperature conversion
temp_f = convert_between_temperature(20, "°C", "°F")
assert abs(temp_f - 68.0) < 0.1
print("✓ Temperature conversion working")
# Test exceptions
try:
raise CannotConnect("Test connection error")
except CannotConnect:
print("✓ CannotConnect exception working")
try:
raise InvalidAuth("Test auth error")
except InvalidAuth:
print("✓ InvalidAuth exception working")
return True
except Exception as e:
print(f"✗ Helper functions test failed: {e}")
return False
async def test_performance_timer():
"""Test performance monitoring functionality."""
try:
from custom_components.smart_irrigation.performance import performance_timer
@performance_timer("test_operation")
def sync_function():
return "success"
@performance_timer("test_async_operation")
async def async_function():
await asyncio.sleep(0.01)
return "async_success"
# Test sync function
result = sync_function()
assert result == "success"
print("✓ Sync performance timer working")
# Test async function
result = await async_function()
assert result == "async_success"
print("✓ Async performance timer working")
return True
except Exception as e:
print(f"✗ Performance timer test failed: {e}")
return False
async def test_exception_classes():
"""Test custom exception classes."""
try:
from custom_components.smart_irrigation.helpers import (
CannotConnect,
InvalidAuth,
)
# Test CannotConnect
exc = CannotConnect("Test message")
assert str(exc) == "Test message"
assert exc.__class__.__name__ == "CannotConnect"
print("✓ CannotConnect exception class working")
# Test InvalidAuth
exc = InvalidAuth("Auth failed")
assert str(exc) == "Auth failed"
assert exc.__class__.__name__ == "InvalidAuth"
print("✓ InvalidAuth exception class working")
return True
except Exception as e:
print(f"✗ Exception classes test failed: {e}")
return False
async def test_mock_store_basic():
"""Test basic store functionality with mocks."""
try:
# Create a minimal mock store
mock_store = Mock()
mock_store.async_get_config = AsyncMock(return_value={})
mock_store.async_get_all_zones = AsyncMock(return_value=[])
mock_store.get_config = Mock(
return_value={
"auto_update_enabled": False,
"auto_calc_enabled": False,
"use_weather_service": False,
}
)
# Test async methods
config = await mock_store.async_get_config()
assert isinstance(config, dict)
print("✓ Mock store async_get_config working")
zones = await mock_store.async_get_all_zones()
assert isinstance(zones, list)
print("✓ Mock store async_get_all_zones working")
# Test sync methods
sync_config = mock_store.get_config()
assert isinstance(sync_config, dict)
assert "auto_update_enabled" in sync_config
print("✓ Mock store get_config working")
return True
except Exception as e:
print(f"✗ Mock store test failed: {e}")
return False
async def main():
"""Run all basic functionality tests."""
print("Running Smart Irrigation Basic Functionality Tests")
print("=" * 50)
tests = [
test_basic_imports,
test_helper_functions,
test_performance_timer,
test_exception_classes,
test_mock_store_basic,
]
passed = 0
total = len(tests)
for test in tests:
print(f"\nRunning {test.__name__}...")
try:
success = await test()
if success:
passed += 1
print(f"✓ {test.__name__} PASSED")
else:
print(f"✗ {test.__name__} FAILED")
except Exception as e:
print(f"✗ {test.__name__} FAILED with exception: {e}")
print("\n" + "=" * 50)
print(f"Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All basic functionality tests passed!")
return 0
else:
print("❌ Some tests failed")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)