def test_json(self): self.blockchain.add_block( self.blockchain.generate_new_block('new-block')) self.assertEqual( convert_dumps( {'blocks': [b.dict() for b in self.blockchain.blocks]}), self.blockchain.json())
async def test_query_all(self): ws = await self.client.ws_connect('/ws') ws.send_str(convert_dumps({'type': MessageTypes.QUERY_ALL})) async for msg in ws: data = convert_loads(msg.data)['data'] self.assertEqual(data, self.server.blockchain.dict()) return ws.close()
def test_json(self): self.assertEqual( convert_dumps({ 'index': 1, 'previous_hash': '12', 'timestamp': 1465154705, 'data': 'test-block', 'hash': '12345' }), self.block.json())
async def test_blockchain_received__append(self): other_blockchain = Blockchain() other_blockchain.add_block( other_blockchain.generate_new_block('new-block')) ws = await self.client.ws_connect('/ws') ws.send_str( convert_dumps({ 'type': MessageTypes.RESPONSE_BLOCKCHAIN, 'data': other_blockchain.dict() })) async for msg in ws: self.assertEqual(other_blockchain.latest_block, self.server.blockchain.latest_block) return ws.close()
async def handle_query_latest(self, ws): ws.send_str( convert_dumps({ 'type': MessageTypes.RESPONSE_BLOCKCHAIN, 'data': self.blockchain.latest_block.dict() }))
def get_query_all_msg(self) -> str: return convert_dumps({'type': MessageTypes.QUERY_ALL})
def get_response_latest_msg(self) -> str: return convert_dumps({ 'type': MessageTypes.RESPONSE_BLOCKCHAIN, 'data': [self.blockchain.latest_block.dict()] })