Skip to content

Commit

Permalink
adding tests for links order and min_height/max_height filters
Browse files Browse the repository at this point in the history
  • Loading branch information
soad003 committed Apr 5, 2024
1 parent f9d0e70 commit 83b14b5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gsrest/db/cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def build_select_address_txs_statement(network: str, node_type: NodeType,
ordering = "ASC" if ascending else "DESC"
# Ordering statement
ordering_statement = ("ORDER BY " +
(" currency DESC," if eth_like else "") +
(f" currency {ordering}," if eth_like else "") +
f" {tx_id_col} {ordering}")

return f"{query} {ordering_statement} LIMIT {limit}"
Expand Down Expand Up @@ -874,6 +874,7 @@ async def list_block_txs_ids(self, currency, height):
"WHERE block_id_group=%s and block_id=%s")
result = await self.execute_async(
currency, 'raw', query, [height_group, int(height)])

if one(result) is None:
return None
return [tx.tx_id for tx in result.one()['txs']]
Expand Down
102 changes: 102 additions & 0 deletions gsrest/test/addresses_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from gsrest.util.values import make_values
import gsrest.test.tags_service as ts
import copy
import itertools

address = Address(
currency="btc",
Expand Down Expand Up @@ -1148,3 +1149,104 @@ async def list_address_links(test_case):
page=result['next_page'],
pagesize=1)
test_case.assertEqual(Links(links=[]).to_dict(), result)

# test order parameter
path = ('/{currency}/addresses/{address}/links?'
'neighbor={neighbor}&order={order}')
result = await test_case.request(path,
currency='eth',
address=eth_address.address,
neighbor='0x123456',
order="desc")

test_case.assertEqual(['af6e0004', 'af6e0003'],
[x['tx_hash'] for x in result['links']])
test_case.assertEqual(None, result.get('next_page', None))

result = await test_case.request(path,
currency='eth',
address=eth_address.address,
neighbor='0x123456',
order="asc")

test_case.assertEqual(list(reversed(['af6e0004', 'af6e0003'])),
[x['tx_hash'] for x in result['links']])
test_case.assertEqual(None, result.get('next_page', None))

for mh, ex, exv in [
(
2,
2,
['af6e0004', 'af6e0003'], # noqa: E131
),
(
3,
1,
['af6e0004'], # noqa: E131
),
(
4,
0,
[], # noqa: E131
)
]:
result = await test_case.request(path + '&min_height={min_height}',
currency='eth',
address=eth_address.address,
neighbor='0x123456',
min_height=mh,
order="desc")

test_case.assertEqual(ex, len(result["links"]))
test_case.assertEqual(exv, [x['tx_hash'] for x in result['links']])

rel = itertools.permutations(["A", "B", "C", "D", "E"], r=2)
er = {("A", "E"): 1}
queries = [(x, y, er.get((x, y), 0)) for x, y in rel]

for o in ["desc", "asc"]:
for a, b, n in queries:
result = await test_case.request(path,
currency='btc',
address=f"address{a}",
neighbor=f"address{b}",
order=o)
test_case.assertEqual(n, len(result["links"]))

for o in ["desc", "asc"]:
for a, b, n in queries:
result = await test_case.request(path + "&min_height=2",
currency='btc',
address=f"address{a}",
neighbor=f"address{b}",
order=o)
test_case.assertEqual(n, len(result["links"]))

for o in ["desc", "asc"]:
for a, b, n in queries:
result = await test_case.request(path + "&max_height=3",
currency='btc',
address=f"address{a}",
neighbor=f"address{b}",
order=o)
test_case.assertEqual(n, len(result["links"]))

er = {}
queries = [(x, y, er.get((x, y), 0)) for x, y in rel]
for o in ["desc", "asc"]:
for a, b, n in queries:
result = await test_case.request(path + "&min_height=3",
currency='btc',
address=f"address{a}",
neighbor=f"address{b}",
order=o)
test_case.assertEqual(n, len(result["links"]))

for o in ["desc", "asc"]:
for a, b, n in queries:
result = await test_case.request(path + "&max_height=2",
currency='btc',
address=f"address{a}",
neighbor=f"address{b}",
order=o)
test_case.assertEqual(n, len(result["links"]))
14 changes: 14 additions & 0 deletions gsrest/test/entities_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,17 @@ async def list_entity_links(test_case):
neighbor=107925001)
txs = Links(links=[tx2_eth, tx22_eth])
test_case.assertEqualWithList(txs.to_dict(), result, 'links', 'tx_hash')

result = await test_case.request(path + "&order=asc",
currency='eth',
entity=107925000,
neighbor=107925001)
test_case.assertEqual(['af6e0003', 'af6e0004'],
[x["tx_hash"] for x in result["links"]])

result = await test_case.request(path + "&order=desc",
currency='eth',
entity=107925000,
neighbor=107925001)
test_case.assertEqual(['af6e0004', 'af6e0003'],
[x["tx_hash"] for x in result["links"]])
1 change: 1 addition & 0 deletions tests/cassandra/data/resttest_btc_raw.block_transactions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{"block_id_group": 0, "block_id": 1, "txs": [{"tx_id": 25001, "no_inputs": 0, "no_outputs": 1, "total_input": 0, "total_output": 5000000000}]}
{"block_id_group": 0, "block_id": 2, "txs": [{"tx_id": 25002, "no_inputs": 0, "no_outputs": 1, "total_input": 0, "total_output": 5000000000}]}
{"block_id_group": 0, "block_id": 3, "txs": [{"tx_id": 270320193, "no_inputs": 2, "no_outputs": 2, "total_input": 75147389, "total_output": 75137389}]}

0 comments on commit 83b14b5

Please sign in to comment.