Skip to content

Commit 15f3384

Browse files
authored
Create test_node_manager.py
1 parent aec3404 commit 15f3384

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import unittest
2+
from satellite_node_network.node_manager import NodeManager
3+
from satellite_node_network.satellite_node import SatelliteNode
4+
5+
class TestNodeManager(unittest.TestCase):
6+
def setUp(self):
7+
"""Set up a NodeManager instance for testing."""
8+
self.node_manager = NodeManager()
9+
self.node1 = SatelliteNode(node_id="Node1", location="Orbit 1")
10+
self.node2 = SatelliteNode(node_id="Node2", location="Orbit 2")
11+
self.node3 = SatelliteNode(node_id="Node3", location="Orbit 3")
12+
13+
def test_add_node(self):
14+
"""Test adding a node to the manager."""
15+
self.node_manager.add_node(self.node1)
16+
self.assertIn("Node1", self.node_manager.nodes)
17+
self.assertEqual(self.node_manager.nodes["Node1"], self.node1)
18+
19+
def test_add_duplicate_node(self):
20+
"""Test adding a duplicate node to the manager."""
21+
self.node_manager.add_node(self.node1)
22+
self.node_manager.add_node(self.node1) # Attempt to add the same node again
23+
self.assertEqual(len(self.node_manager.nodes), 1) # Should still be 1
24+
25+
def test_remove_node(self):
26+
"""Test removing a node from the manager."""
27+
self.node_manager.add_node(self.node1)
28+
self.node_manager.remove_node("Node1")
29+
self.assertNotIn("Node1", self.node_manager.nodes)
30+
31+
def test_remove_nonexistent_node(self):
32+
"""Test removing a node that does not exist."""
33+
self.node_manager.remove_node("Node1") # Should not raise an error
34+
self.assertEqual(len(self.node_manager.nodes), 0) # Still should be 0
35+
36+
def test_connect_nodes(self):
37+
"""Test connecting two nodes."""
38+
self.node_manager.add_node(self.node1)
39+
self.node_manager.add_node(self.node2)
40+
self.node_manager.connect_nodes("Node1", "Node2")
41+
42+
self.assertIn(self.node2, self.node1.connected_nodes)
43+
self.assertIn(self.node1, self.node2.connected_nodes)
44+
45+
def test_disconnect_nodes(self):
46+
"""Test disconnecting two nodes."""
47+
self.node_manager.add_node(self.node1)
48+
self.node_manager.add_node(self.node2)
49+
self.node_manager.connect_nodes("Node1", "Node2")
50+
self.node_manager.disconnect_nodes("Node1", "Node2")
51+
52+
self.assertNotIn(self.node2, self.node1.connected_nodes)
53+
self.assertNotIn(self.node1, self.node2.connected_nodes)
54+
55+
def test_send_data(self):
56+
"""Test sending data between nodes."""
57+
self.node_manager.add_node(self.node1)
58+
self.node_manager.add_node(self.node2)
59+
60+
with self.assertLogs(level='INFO') as log:
61+
data = {'temperature': 75, 'humidity': 50}
62+
response = self.node_manager.send_data("Node1", "Node2", data)
63+
self.assertIn("Node Node1 sending data to Node Node2: {'temperature': 75, 'humidity': 50}", log.output[0])
64+
self.assertEqual(response, "Data sent from Node1 to Node2: {'temperature': 75, 'humidity': 50}")
65+
66+
def test_send_data_to_nonexistent_node(self):
67+
"""Test sending data to a node that does not exist."""
68+
self.node_manager.add_node(self.node1)
69+
data = {'temperature': 75, 'humidity': 50}
70+
71+
with self.assertLogs(level='ERROR') as log:
72+
response = self.node_manager.send_data("Node1", "Node2", data) # Node2 does not exist
73+
self.assertIn("One or both nodes not found in NodeManager.", log.output[0])
74+
self.assertIsNone(response)
75+
76+
if __name__ == "__main__":
77+
unittest.main()

0 commit comments

Comments
 (0)