From 0888398482daf7db0b9ee29a23ca320d8671dd5b Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 3 Aug 2024 21:06:21 +0700 Subject: [PATCH] Create pi-network-integration-tests.py --- .../pi-network-integration-tests.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 blockchain_integration/pi_network/CloudNexus/integration-tests/pi-network-integration-tests.py diff --git a/blockchain_integration/pi_network/CloudNexus/integration-tests/pi-network-integration-tests.py b/blockchain_integration/pi_network/CloudNexus/integration-tests/pi-network-integration-tests.py new file mode 100644 index 000000000..48434ad1a --- /dev/null +++ b/blockchain_integration/pi_network/CloudNexus/integration-tests/pi-network-integration-tests.py @@ -0,0 +1,76 @@ +import unittest +from unittest.mock import patch, MagicMock +from pi_network import PiNetwork, Node, Transaction +from pi_network_client import PiNetworkClient + +class TestPiNetworkIntegration(unittest.TestCase): + def setUp(self): + self.pi_network = PiNetwork() + self.pi_network_client = PiNetworkClient('http://localhost:8080') + + def test_add_node(self): + node = Node('node1', '192.168.1.100') + response = self.pi_network_client.add_node(node) + self.assertEqual(response.status_code, 201) + self.assertIn(node, self.pi_network.nodes) + + def test_remove_node(self): + node = Node('node1', '192.168.1.100') + self.pi_network.add_node(node) + response = self.pi_network_client.remove_node(node) + self.assertEqual(response.status_code, 200) + self.assertNotIn(node, self.pi_network.nodes) + + def test_add_transaction(self): + transaction = Transaction('tx1', 'node1', 'node2', 10) + response = self.pi_network_client.add_transaction(transaction) + self.assertEqual(response.status_code, 201) + self.assertIn(transaction, self.pi_network.transactions) + + def test_remove_transaction(self): + transaction = Transaction('tx1', 'node1', 'node2', 10) + self.pi_network.add_transaction(transaction) + response = self.pi_network_client.remove_transaction(transaction) + self.assertEqual(response.status_code, 200) + self.assertNotIn(transaction, self.pi_network.transactions) + + def test_get_node_by_id(self): + node = Node('node1', '192.168.1.100') + self.pi_network.add_node(node) + response = self.pi_network_client.get_node_by_id('node1') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), node.to_dict()) + + def test_get_transaction_by_id(self): + transaction = Transaction('tx1', 'node1', 'node2', 10) + self.pi_network.add_transaction(transaction) + response = self.pi_network_client.get_transaction_by_id('tx1') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), transaction.to_dict()) + + def test_validate_transaction(self): + transaction = Transaction('tx1', 'node1', 'node2', 10) + self.pi_network.add_node(Node('node1', '192.168.1.100')) + self.pi_network.add_node(Node('node2', '192.168.1.101')) + response = self.pi_network_client.validate_transaction(transaction) + self.assertEqual(response.status_code, 200) + self.assertTrue(response.json()['valid']) + + def test_validate_transaction_invalid_node(self): + transaction = Transaction('tx1', 'node1', 'node3', 10) + self.pi_network.add_node(Node('node1', '192.168.1.100')) + self.pi_network.add_node(Node('node2', '192.168.1.101')) + response = self.pi_network_client.validate_transaction(transaction) + self.assertEqual(response.status_code, 200) + self.assertFalse(response.json()['valid']) + + def test_validate_transaction_invalid_amount(self): + transaction = Transaction('tx1', 'node1', 'node2', -10) + self.pi_network.add_node(Node('node1', '192.168.1.100')) + self.pi_network.add_node(Node('node2', '192.168.1.101')) + response = self.pi_network_client.validate_transaction(transaction) + self.assertEqual(response.status_code, 200) + self.assertFalse(response.json()['valid']) + +if __name__ == '__main__': + unittest.main()