Skip to content

Commit

Permalink
Create test_pi_node.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 0683c6e commit 7cfca64
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions projects/piguardian/test/test_pi_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from unittest.mock import patch, MagicMock
from pi_node import PiNode

class TestPiNode(unittest.TestCase):
def setUp(self):
self.pi_node = PiNode()

def test_init(self):
self.assertIsNotNone(self.pi_node)

@patch('pi_node.requests')
def test_send_data(self, mock_requests):
mock_response = MagicMock()
mock_response.status_code = 200
mock_requests.post.return_value = mock_response

data = {'temperature': 25, 'humidity': 60}
self.pi_node.send_data(data)

mock_requests.post.assert_called_once_with('https://example.com/api/data', json=data)

@patch('pi_node.time')
def test_read_sensor(self, mock_time):
mock_time.sleep.return_value = None
mock_sensor_data = {'temperature': 25, 'humidity': 60}

with patch('pi_node.DHTSensor') as mock_dht_sensor:
mock_dht_sensor.return_value.read.return_value = mock_sensor_data
data = self.pi_node.read_sensor()
self.assertEqual(data, mock_sensor_data)

def test_calculate_pi(self):
data = {'temperature': 25, 'humidity': 60}
pi_value = self.pi_node.calculate_pi(data)
self.assertAlmostEqual(pi_value, 3.14159, places=5)

if __name__ == '__main__':
unittest.main()

0 comments on commit 7cfca64

Please sign in to comment.