Skip to content

Commit

Permalink
[lightapi/python] task: add unit test for query block size
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLaw committed Oct 4, 2023
1 parent d09f425 commit 8bc15f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions lightapi/python/symbollightapi/connector/NemConnector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio

from symbolchain.CryptoTypes import PublicKey
from symbolchain.nc import TransactionType
from symbolchain.nem.Network import Address
Expand Down
42 changes: 34 additions & 8 deletions lightapi/python/tests/connector/test_NemConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -772,7 +776,7 @@ async def test_can_query_account_info_without_public_key(server): # pylint: dis
None,
150000000000,
'NBUH72UCGBIB64VYTAAJ7QITJ62BLISFFQOHVP65',
{},
None,
None
),
'edcc8d1c48165f5b771087fbe3c4b4d41f5f8f6c4ce715e050b86fb4e7fdeb64'
Expand All @@ -784,19 +788,24 @@ 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(''))

# Act:
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(
Expand All @@ -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:
Expand All @@ -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
5 changes: 4 additions & 1 deletion lightapi/python/tests/model/test_Block.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def _create_default_block(override=None):
(
'a4bbf324a3480f58c2d15bdb15d0232da94db9519d5b727a3ea12c11cc11d368'
'e0037c08e1994bc07adc4f790bcb09c1d727066b0308463e406e175572c4150a'
)
),
888
)

if override:
Expand All @@ -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:
Expand All @@ -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)))

0 comments on commit 8bc15f3

Please sign in to comment.