|
| 1 | +# tests/test_quantum_consensus/test_qebc_protocol.py |
| 2 | + |
| 3 | +import unittest |
| 4 | +from src.quantum_consensus.qebc_protocol import QEBCProtocol |
| 5 | + |
| 6 | +class TestQEBCProtocol(unittest.TestCase): |
| 7 | + def setUp(self): |
| 8 | + """Set up the QEBC Protocol for testing.""" |
| 9 | + self.node_id = 0 |
| 10 | + self.num_nodes = 5 |
| 11 | + self.qebc = QEBCProtocol(node_id=self.node_id, num_nodes=self.num_nodes) |
| 12 | + self.qebc.create_entangled_pairs() # Create entangled pairs for the test |
| 13 | + |
| 14 | + def test_create_entangled_pairs(self): |
| 15 | + """Test that entangled pairs are created correctly.""" |
| 16 | + self.assertEqual(len(self.qebc.entangled_pairs), self.num_nodes - 1) |
| 17 | + for pair in self.qebc.entangled_pairs: |
| 18 | + self.assertEqual(pair[0], self.node_id) # Check that the first element is the node_id |
| 19 | + self.assertIn(pair[1], range(self.num_nodes)) # Check that the second element is a valid node ID |
| 20 | + |
| 21 | + def test_propose_value_consensus_reached(self): |
| 22 | + """Test proposing a value and reaching consensus.""" |
| 23 | + proposed_value = "Test Value" |
| 24 | + consensus_result = self.qebc.reach_consensus(proposed_value) |
| 25 | + self.assertTrue(consensus_result) # Expecting consensus to be reached |
| 26 | + |
| 27 | + def test_propose_value_consensus_not_reached(self): |
| 28 | + """Test proposing a value and not reaching consensus.""" |
| 29 | + # Override the voting mechanism to simulate a failure in consensus |
| 30 | + original_vote = self.qebc._vote |
| 31 | + self.qebc._vote = lambda proposed_value: False # Simulate all votes against |
| 32 | + |
| 33 | + proposed_value = "Test Value" |
| 34 | + consensus_result = self.qebc.reach_consensus(proposed_value) |
| 35 | + self.assertFalse(consensus_result) # Expecting consensus not to be reached |
| 36 | + |
| 37 | + # Restore the original voting mechanism |
| 38 | + self.qebc._vote = original_vote |
| 39 | + |
| 40 | + def test_vote(self): |
| 41 | + """Test the voting mechanism.""" |
| 42 | + proposed_value = "Test Value" |
| 43 | + vote_result = self.qebc._vote(proposed_value) |
| 44 | + self.assertIn(vote_result, [True, False]) # Vote should be either True or False |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + unittest.main() |
0 commit comments