def test_request_block_whenIndexIsLatest_thenRequestsLatestBlockFromNode(self):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = '{"nonce": 12345, "index": 35, "transactions": [], "timestamp": 1234567890, "current_hash": "current_hash", "previous_hash": "previous_hash"}'
with patch.object(FullNode, '__init__', return_value=None) as patched_init, \
patch("crankycoin.node.Block.current_hash", new_callable=PropertyMock) as patched_block_current_hash, \
patch("crankycoin.requests.get", return_value=mock_response) as patched_requests:
patched_block_current_hash.return_value = "current_hash"
node = FullNode("127.0.0.1", "reward_address")
block = node.request_block("127.0.0.2", "30013", "latest")
self.assertIsNotNone(block)
self.assertEqual(block.index, 35)
self.assertEqual(block.transactions, [])
self.assertEqual(block.previous_hash, "previous_hash")
self.assertEqual(block.current_hash, "current_hash")
self.assertEqual(block.timestamp, 1234567890)
self.assertEqual(block.nonce, 12345)
patched_requests.assert_called_once_with('http://127.0.0.2:30013/block/latest')
评论列表
文章目录