Skip to content

Commit c1ed0cb

Browse files
authored
Update unit_test.py
1 parent c49ee0a commit c1ed0cb

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

spectreora_IoT/unit_test.py

+52-45
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
11
import unittest
2-
from unittest.mock import Mock, patch
3-
import main
42

3+
Define a test class that inherits from unittest.TestCase
4+
class TestEcgData(unittest.TestCase):
55

6-
class TestMain(unittest.TestCase):
6+
# Define a setUp method that runs before each test case
7+
def setUp(self):
8+
# Create an instance of the WLAN class and connect to Wi-Fi
9+
self.wlan = wifi_connect()
10+
# Create an instance of the ADC class and pass the pin number
11+
self.adc = ADC(Pin(26))
12+
# Set the url, headers, sampling interval, and send data interval
13+
self.url, self.headers = config.SUPABASE_URL, {
14+
"apikey": config.API_KEY,
15+
"Content-Type": config.TYPE
16+
}
17+
self.sampling_interval, self.send_data_interval = 8, 15 * 1000
18+
# Set the next sample and last send time to the current time
19+
self.next_sample, self.last_send_time = ticks_ms(), ticks_ms()
720

8-
def test_wifi_connect_success(self):
9-
# Test that the wifi_connect function successfully connects to the specified Wi-Fi network
10-
mock_wlan = Mock()
11-
mock_sta_if = Mock(return_value=mock_wlan)
12-
with patch('main.WLAN', mock_sta_if):
13-
mock_wlan.isconnected.return_value = True
14-
mock_wlan.ifconfig.return_value = (
15-
'192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
16-
result = main.wifi_connect()
17-
self.assertEqual(result, mock_wlan)
18-
mock_sta_if.assert_called_once()
19-
mock_wlan.active.assert_called_once_with(True)
20-
mock_wlan.connect.assert_called_once_with(
21-
main.config.SSID, main.config.PASSWORD)
22-
mock_wlan.ifconfig.assert_called_once()o
21+
# Define a tearDown method that runs after each test case
22+
def tearDown(self):
23+
# Disconnect from Wi-Fi and close the WLAN instance
24+
self.wlan.disconnect()
25+
self.wlan.active(False)
26+
self.wlan.close()
27+
# Close the ADC instance
28+
self.adc.close()
2329

24-
def test_read_adc(self):
25-
# Test that the read_adc function collects ECG data correctly
26-
mock_adc = Mock()
27-
mock_pin = Mock(return_value=mock_adc)
28-
with patch('main.Pin', mock_pin):
29-
mock_adc.read_u16.return_value = 500
30-
# Test filling the ecg_data_array
31-
for i in range(3000):
32-
result = main.read_adc(mock_adc)
33-
if i < 2999:
34-
self.assertFalse(result)
35-
else:
36-
self.assertTrue(result)
37-
# Test resetting the ecg_data_array
38-
self.assertEqual(main.ecg_data_array, [None] * 3000)
39-
40-
def test_send_data(self):
41-
# Test that the send_data function sends data correctly
42-
mock_response = Mock()
43-
mock_response.status_code = 200
44-
mock_post = Mock(return_value=mock_response)
45-
with patch('main.urequests.post', mock_post):
46-
result = main.send_data(
47-
Mock(), 'http://example.com', '{}', {'Content-Type': 'application/json'})
48-
self.assertEqual(result, 200)
49-
mock_post.assert_called_once_with(
50-
'http://example.com', data='{}', headers={'Content-Type': 'application/json'})
30+
# Define a test case for reading adc values and sending data
31+
def test_read_adc_and_send_data(self):
32+
# Use a loop to simulate 20 seconds of data collection and transmission
33+
for _ in range(20):
34+
# Get the current time
35+
now = ticks_ms()
36+
# Check if it is time to read adc values
37+
if ticks_diff(self.next_sample, now) <= 0:
38+
# Call the read_adc function and store the return value
39+
is_full = read_adc(self.adc)
40+
# Check if it is time to send data
41+
if is_full and ticks_diff(now, self.last_send_time) >= self.send_data_interval:
42+
# Prepare the payload as a JSON string
43+
payload = ujson.dumps(
44+
{"values": ecg_data_array, "user_id": config.USER_ID})
45+
gc.collect()
46+
# Call the send_data function and store the return value
47+
status_code = send_data(self.wlan, self.url, payload, self.headers)
48+
gc.collect()
49+
# Assert that the status code is 200 (OK)
50+
self.assertEqual(status_code, 200)
51+
# Update the last send time
52+
self.last_send_time = ticks_ms()
53+
# Update the next sample time
54+
self.next_sample = ticks_ms() + self.sampling_interval
55+
Copy
56+
Run the test script if it is executed as the main module
57+
if name ==main’: unittest.main()

0 commit comments

Comments
 (0)