|
| 1 | +import unittest |
| 2 | +from satellite_node_network.edge_computing import EdgeComputing |
| 3 | + |
| 4 | +class TestEdgeComputing(unittest.TestCase): |
| 5 | + def setUp(self): |
| 6 | + """Set up an EdgeComputing instance for testing.""" |
| 7 | + self.edge_computing = EdgeComputing(node_id="Node1") |
| 8 | + |
| 9 | + def test_process_data(self): |
| 10 | + """Test processing raw data.""" |
| 11 | + raw_data = { |
| 12 | + 'temperature': 75, |
| 13 | + 'humidity': 50, |
| 14 | + 'status': 'active' |
| 15 | + } |
| 16 | + processed_data = self.edge_computing.process_data(raw_data) |
| 17 | + |
| 18 | + # Check that numeric values are normalized |
| 19 | + self.assertIn('temperature', processed_data) |
| 20 | + self.assertIn('humidity', processed_data) |
| 21 | + self.assertEqual(processed_data['status'], 'active') # Non-numeric data should remain unchanged |
| 22 | + |
| 23 | + # Check normalization logic |
| 24 | + self.assertGreaterEqual(processed_data['temperature'], 0) |
| 25 | + self.assertLessEqual(processed_data['temperature'], 1) |
| 26 | + self.assertGreaterEqual(processed_data['humidity'], 0) |
| 27 | + self.assertLessEqual(processed_data['humidity'], 1) |
| 28 | + |
| 29 | + def test_normalize(self): |
| 30 | + """Test normalization of values.""" |
| 31 | + value = 75 |
| 32 | + normalized_value = self.edge_computing.normalize(value) |
| 33 | + self.assertEqual(normalized_value, 0.75) # Assuming normalization is based on a range of 0 to 100 |
| 34 | + |
| 35 | + value = 0 |
| 36 | + normalized_value = self.edge_computing.normalize(value) |
| 37 | + self.assertEqual(normalized_value, 0.0) |
| 38 | + |
| 39 | + value = 100 |
| 40 | + normalized_value = self.edge_computing.normalize(value) |
| 41 | + self.assertEqual(normalized_value, 1.0) |
| 42 | + |
| 43 | + value = 150 # Out of range |
| 44 | + normalized_value = self.edge_computing.normalize(value) |
| 45 | + self.assertEqual(normalized_value, 1.0) # Clamped to 1 |
| 46 | + |
| 47 | + def test_store_data(self): |
| 48 | + """Test storing processed data.""" |
| 49 | + processed_data = { |
| 50 | + 'temperature': 0.75, |
| 51 | + 'humidity': 0.50 |
| 52 | + } |
| 53 | + # Here we would implement the actual storage logic, but for now, we just log it |
| 54 | + with self.assertLogs(level='INFO') as log: |
| 55 | + self.edge_computing.store_data(processed_data) |
| 56 | + self.assertIn("Node Node1 storing processed data: {'temperature': 0.75, 'humidity': 0.50}", log.output[0]) |
| 57 | + |
| 58 | + def test_communicate_with_node(self): |
| 59 | + """Test communication with another node.""" |
| 60 | + # Simulate another node for testing |
| 61 | + class MockNode: |
| 62 | + def __init__(self, node_id): |
| 63 | + self.node_id = node_id |
| 64 | + |
| 65 | + other_node = MockNode(node_id="Node2") |
| 66 | + data = {'temperature': 0.75, 'humidity': 0.50} |
| 67 | + |
| 68 | + with self.assertLogs(level='INFO') as log: |
| 69 | + self.edge_computing.communicate_with_node(other_node, data) |
| 70 | + self.assertIn("Node Node1 sending data to Node Node2: {'temperature': 0.75, 'humidity': 0.50}", log.output[0]) |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments