From 8bc15f38ceb6aee9496e93275538e719c87804f2 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Thu, 5 Oct 2023 01:53:05 +0800 Subject: [PATCH] [lightapi/python] task: add unit test for query block size --- .../symbollightapi/connector/NemConnector.py | 1 + .../tests/connector/test_NemConnector.py | 42 +++++++++++++++---- lightapi/python/tests/model/test_Block.py | 5 ++- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lightapi/python/symbollightapi/connector/NemConnector.py b/lightapi/python/symbollightapi/connector/NemConnector.py index f83a8f014..ae79e8028 100644 --- a/lightapi/python/symbollightapi/connector/NemConnector.py +++ b/lightapi/python/symbollightapi/connector/NemConnector.py @@ -1,4 +1,5 @@ import asyncio + from symbolchain.CryptoTypes import PublicKey from symbolchain.nc import TransactionType from symbolchain.nem.Network import Address diff --git a/lightapi/python/tests/connector/test_NemConnector.py b/lightapi/python/tests/connector/test_NemConnector.py index cc2e9d94c..43d495460 100644 --- a/lightapi/python/tests/connector/test_NemConnector.py +++ b/lightapi/python/tests/connector/test_NemConnector.py @@ -465,6 +465,9 @@ async def local_chain_blocks_after(self, request): async def local_block_at(self, request): return await self._process(request, CHAIN_BLOCK_1) + async def block_at_public(self, request): + return await self._process(request, CHAIN_BLOCK_1) + async def _process(self, request, response_body): self.urls.append(str(request.url)) return web.Response(body=json.dumps(response_body), headers={'Content-Type': 'application/json'}) @@ -481,6 +484,7 @@ async def _process(self, request, response_body): app.router.add_get('/account/get/forwarded', mock_server.account_info_forwarded) app.router.add_post('/local/chain/blocks-after', mock_server.local_chain_blocks_after) app.router.add_post('/local/block/at', mock_server.local_block_at) + app.router.add_post('/block/at/public', mock_server.block_at_public) server = event_loop.run_until_complete(aiohttp_client(app)) # pylint: disable=redefined-outer-name @@ -650,7 +654,7 @@ async def test_can_query_account_info_without_public_key(server): # pylint: dis # endregion -# region local chain blocks after +# region POST (get_blocks_after) EXPECTED_BLOCK_2 = Block( 2, @@ -772,7 +776,7 @@ async def test_can_query_account_info_without_public_key(server): # pylint: dis None, 150000000000, 'NBUH72UCGBIB64VYTAAJ7QITJ62BLISFFQOHVP65', - {}, + None, None ), 'edcc8d1c48165f5b771087fbe3c4b4d41f5f8f6c4ce715e050b86fb4e7fdeb64' @@ -784,11 +788,12 @@ async def test_can_query_account_info_without_public_key(server): # pylint: dis ( 'fdf6a9830e9320af79123f467fcb03d6beab735575ff50eab363d812c5581436' '2ad7be0503db2ee70e60ac3408d83cdbcbd941067a6df703e0c21c7bf389f105' - ) + ), + 5548 ) -async def can_query_blocks_after(server): # pylint: disable=redefined-outer-name +async def test_can_query_blocks_after(server): # pylint: disable=redefined-outer-name # Arrange: connector = NemConnector(server.make_url('')) @@ -796,7 +801,11 @@ async def can_query_blocks_after(server): # pylint: disable=redefined-outer-nam blocks = await connector.get_blocks_after(1) # Assert: - assert [f'{server.make_url("")}/local/chain/blocks-after'] == server.mock.urls + assert [ + f'{server.make_url("")}/local/chain/blocks-after', + f'{server.make_url("")}/block/at/public', + f'{server.make_url("")}/block/at/public' + ] == server.mock.urls assert 2 == len(blocks) assert EXPECTED_BLOCK_2 == blocks[0] assert Block( @@ -809,13 +818,14 @@ async def can_query_blocks_after(server): # pylint: disable=redefined-outer-nam ( '919ae66a34119b49812b335827b357f86884ab08b628029fd6e8db3572faeb4f' '323a7bf9488c76ef8faa5b513036bbcce2d949ba3e41086d95a54c0007403c0b' - ) + ), + 5548 ) == blocks[1] # endregion -# region local block at public +# region POST (get_block) async def test_can_query_block_at(server): # pylint: disable=redefined-outer-name # Arrange: @@ -825,7 +835,23 @@ async def test_can_query_block_at(server): # pylint: disable=redefined-outer-na block = await connector.get_block(2) # Assert: - assert [f'{server.make_url("")}/local/block/at'] == server.mock.urls + assert [f'{server.make_url("")}/local/block/at', f'{server.make_url("")}/block/at/public'] == server.mock.urls assert EXPECTED_BLOCK_2 == block # endregion + + +# region POST (get_block_size) + +async def test_can_query_block_size(server): # pylint: disable=redefined-outer-name + # Arrange: + connector = NemConnector(server.make_url('')) + + # Act: + block_size = await connector.get_block_size(2) + + # Assert: + assert [f'{server.make_url("")}/block/at/public'] == server.mock.urls + assert 5548 == block_size + +# endregion diff --git a/lightapi/python/tests/model/test_Block.py b/lightapi/python/tests/model/test_Block.py index 44261245e..9d78570c7 100644 --- a/lightapi/python/tests/model/test_Block.py +++ b/lightapi/python/tests/model/test_Block.py @@ -16,7 +16,8 @@ def _create_default_block(override=None): ( 'a4bbf324a3480f58c2d15bdb15d0232da94db9519d5b727a3ea12c11cc11d368' 'e0037c08e1994bc07adc4f790bcb09c1d727066b0308463e406e175572c4150a' - ) + ), + 888 ) if override: @@ -40,6 +41,7 @@ def test_can_create_block(self): 'a4bbf324a3480f58c2d15bdb15d0232da94db9519d5b727a3ea12c11cc11d368' 'e0037c08e1994bc07adc4f790bcb09c1d727066b0308463e406e175572c4150a' ), block.signature) + self.assertEqual(888, block.size) def test_eq_is_supported(self): # Arrange: @@ -56,3 +58,4 @@ def test_eq_is_supported(self): self.assertNotEqual(block, self._create_default_block(('block_hash', 'invalid hash'))) self.assertNotEqual(block, self._create_default_block(('signer', 'invalid signer'))) self.assertNotEqual(block, self._create_default_block(('signature', 'invalid signature'))) + self.assertNotEqual(block, self._create_default_block(('size', 123)))