Add test for all list query combinations

This commit is contained in:
nolash 2021-07-25 19:41:37 +02:00
parent 5415b6adad
commit 36b73c7f7a
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 119 additions and 64 deletions

View File

@ -117,17 +117,17 @@ class DataCache(Cache):
if limit == 0: if limit == 0:
limit = DEFAULT_LIMIT limit = DEFAULT_LIMIT
rows = list_transactions_mined_with_data(self.session, offset, limit, block_offset, block_limit, oldest=oldest) rows = list_transactions_mined_with_data(self.session, offset, limit, block_offset, block_limit, oldest=oldest)
return self.__process_rows(rows) return self.__process_rows(rows, oldest)
def load_transactions_account_with_data(self, address, offset, limit, block_offset=None, block_limit=None, oldest=False): def load_transactions_account_with_data(self, address, offset, limit, block_offset=None, block_limit=None, oldest=False):
if limit == 0: if limit == 0:
limit = DEFAULT_LIMIT limit = DEFAULT_LIMIT
rows = list_transactions_account_mined_with_data(self.session, address, offset, limit, block_offset, block_limit, oldest=oldest) rows = list_transactions_account_mined_with_data(self.session, address, offset, limit, block_offset, block_limit, oldest=oldest)
return self.__process_rows(rows) return self.__process_rows(rows, oldest)
def __process_rows(self, rows): def __process_rows(self, rows, oldest):
tx_cache = [] tx_cache = []
highest_block = -1; highest_block = -1;
lowest_block = -1; lowest_block = -1;
@ -135,7 +135,12 @@ class DataCache(Cache):
for r in rows: for r in rows:
if highest_block == -1: if highest_block == -1:
highest_block = r['block_number'] highest_block = r['block_number']
lowest_block = r['block_number'] lowest_block = r['block_number']
else:
if oldest:
highest_block = r['block_number']
else:
lowest_block = r['block_number']
tx_type = 'unknown' tx_type = 'unknown'
if r['value'] != None: if r['value'] != None:

View File

@ -172,7 +172,7 @@ def list_transactions_account_mined_with_data(
else: else:
s = "SELECT tx_hash, block_number, date_block, sender, recipient, from_value, to_value, source_token, destination_token, success, domain, value FROM tx LEFT JOIN tag_tx_link ON tx.id = tag_tx_link.tx_id LEFT JOIN tag ON tag_tx_link.tag_id = tag.id WHERE block_number >= {} AND (sender = '{}' OR recipient = '{}') ORDER BY block_number {}, tx_index {} LIMIT {} OFFSET {}".format(block_offset, address, address, order_by, order_by, limit, offset) s = "SELECT tx_hash, block_number, date_block, sender, recipient, from_value, to_value, source_token, destination_token, success, domain, value FROM tx LEFT JOIN tag_tx_link ON tx.id = tag_tx_link.tx_id LEFT JOIN tag ON tag_tx_link.tag_id = tag.id WHERE block_number >= {} AND (sender = '{}' OR recipient = '{}') ORDER BY block_number {}, tx_index {} LIMIT {} OFFSET {}".format(block_offset, address, address, order_by, order_by, limit, offset)
else: else:
s = "SELECT tx_hash, block_number, date_block, sender, recipient, from_value, to_value, source_token, destination_token, success, domain, value FROM tx LEFT JOIN tag_tx_link ON tx.id = tag_tx_link.tx_id LEFT JOIN tag ON tag_tx_link.tag_id = tag.id WHERE sender = '{}' OR recipient = '{}' ORDER BY block_number , tx_index {} LIMIT {} OFFSET {}".format(address, address, order_by, order_by, limit, offset) s = "SELECT tx_hash, block_number, date_block, sender, recipient, from_value, to_value, source_token, destination_token, success, domain, value FROM tx LEFT JOIN tag_tx_link ON tx.id = tag_tx_link.tx_id LEFT JOIN tag ON tag_tx_link.tag_id = tag.id WHERE sender = '{}' OR recipient = '{}' ORDER BY block_number {}, tx_index {} LIMIT {} OFFSET {}".format(address, address, order_by, order_by, limit, offset)
r = session.execute(s) r = session.execute(s)
return r return r

View File

@ -127,7 +127,8 @@ def more_txs(
session.commit() session.commit()
return txs + [tx_hash] return [tx_hash] + txs
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def tag_txs( def tag_txs(

View File

@ -44,7 +44,6 @@ def test_cache_data(
session = init_database session = init_database
c = DataCache(session) c = DataCache(session)
#b = c.load_transactions_with_data(0, 100, block_offset=410000, block_limit=420000, oldest=True)
b = c.load_transactions_with_data(0, 3) #410000, 420000) #, 100, block_offset=410000, block_limit=420000, oldest=True) b = c.load_transactions_with_data(0, 3) #410000, 420000) #, 100, block_offset=410000, block_limit=420000, oldest=True)
assert len(b[2]) == 2 assert len(b[2]) == 2
@ -96,6 +95,10 @@ def test_cache_ranges(
assert b[0] == oldest assert b[0] == oldest
assert b[1] == mid assert b[1] == mid
b = c.load_transactions(0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'], oldest=True)
assert b[0] == oldest
assert b[1] == mid
# now check when supplying account # now check when supplying account
b = c.load_transactions_account(list_actors['alice'], 0, 100) b = c.load_transactions_account(list_actors['alice'], 0, 100)
assert b[0] == oldest assert b[0] == oldest
@ -132,7 +135,7 @@ def test_cache_ranges_data(
list_defaults, list_defaults,
list_actors, list_actors,
list_tokens, list_tokens,
txs, more_txs,
): ):
session = init_database session = init_database
@ -142,62 +145,108 @@ def test_cache_ranges_data(
newest = list_defaults['block'] + 2 newest = list_defaults['block'] + 2
c = DataCache(session) c = DataCache(session)
b = c.load_transactions_with_data(0, 100) b = c.load_transactions_with_data(0, 100)
assert len(b) == 3 assert b[0] == oldest
assert b[1] == newest
assert len(b[2]) == 3
assert b[2][0]['tx_hash'] == more_txs[0]
assert b[2][2]['tx_hash'] == more_txs[2]
b = c.load_transactions_with_data(1, 2)
assert b[0] == oldest
assert b[1] == mid
assert len(b[2]) == 2
assert b[2][0]['tx_hash'] == more_txs[1]
assert b[2][1]['tx_hash'] == more_txs[2]
# b = c.load_transactions(1, 2) b = c.load_transactions_with_data(0, 2)
# assert b[0] == oldest assert b[0] == mid
# assert b[1] == mid assert b[1] == newest
# assert len(b[2]) == 2
# b = c.load_transactions(0, 2) assert b[2][0]['tx_hash'] == more_txs[0]
# assert b[0] == mid assert b[2][1]['tx_hash'] == more_txs[1]
# assert b[1] == newest
# b = c.load_transactions_with_data(0, 1)
# b = c.load_transactions(0, 1) assert b[0] == newest
# assert b[0] == newest assert b[1] == newest
# assert b[1] == newest assert len(b[2]) == 1
# assert b[2][0]['tx_hash'] == more_txs[0]
# b = c.load_transactions(0, 100, oldest=True)
# assert b[0] == oldest b = c.load_transactions_with_data(0, 100, oldest=True)
# assert b[1] == newest assert b[0] == oldest
# assert b[1] == newest
# b = c.load_transactions(0, 100, block_offset=list_defaults['block']) assert len(b[2]) == 3
# assert b[0] == mid assert b[2][0]['tx_hash'] == more_txs[2]
# assert b[1] == newest assert b[2][1]['tx_hash'] == more_txs[1]
# assert b[2][2]['tx_hash'] == more_txs[0]
# b = c.load_transactions(0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'])
# assert b[0] == oldest b = c.load_transactions_with_data(0, 100, block_offset=list_defaults['block'])
# assert b[1] == mid assert b[0] == mid
# assert b[1] == newest
# # now check when supplying account assert len(b[2]) == 2
# b = c.load_transactions_account(list_actors['alice'], 0, 100) assert b[2][0]['tx_hash'] == more_txs[0]
# assert b[0] == oldest assert b[2][1]['tx_hash'] == more_txs[1]
# assert b[1] == newest
# b = c.load_transactions_with_data(0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'])
# b = c.load_transactions_account(list_actors['bob'], 0, 100) assert b[0] == oldest
# assert b[0] == mid assert b[1] == mid
# assert b[1] == mid assert len(b[2]) == 2
# assert b[2][0]['tx_hash'] == more_txs[1]
# b = c.load_transactions_account(list_actors['diane'], 0, 100) assert b[2][1]['tx_hash'] == more_txs[2]
# assert b[0] == oldest
# assert b[1] == newest b = c.load_transactions_with_data(0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'], oldest=True)
# assert b[0] == oldest
# # add block filter to the mix assert b[1] == mid
# b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) assert len(b[2]) == 2
# assert b[0] == mid assert b[2][0]['tx_hash'] == more_txs[2]
# assert b[1] == newest assert b[2][1]['tx_hash'] == more_txs[1]
#
# b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) # now check when supplying account
# assert b[0] == mid b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100)
# assert b[1] == newest assert b[0] == oldest
# assert b[1] == newest
# b = c.load_transactions_account(list_actors['bob'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) assert len(b[2]) == 3
# assert b[0] == mid assert b[2][0]['tx_hash'] == more_txs[0]
# assert b[1] == mid assert b[2][1]['tx_hash'] == more_txs[1]
# assert b[2][2]['tx_hash'] == more_txs[2]
# b = c.load_transactions_account(list_actors['diane'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'])
# assert b[0] == oldest b = c.load_transactions_account_with_data(list_actors['bob'], 0, 100)
# assert b[1] == oldest assert b[0] == mid
# assert b[1] == mid
# assert len(b[2]) == 1
assert b[2][0]['tx_hash'] == more_txs[1]
b = c.load_transactions_account_with_data(list_actors['diane'], 0, 100)
assert b[0] == oldest
assert b[1] == newest
assert len(b[2]) == 2
assert b[2][0]['tx_hash'] == more_txs[0]
assert b[2][1]['tx_hash'] == more_txs[2]
# add block filter to the mix
b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100, block_offset=list_defaults['block'])
assert b[0] == mid
assert b[1] == newest
assert len(b[2]) == 2
assert b[2][0]['tx_hash'] == more_txs[0]
assert b[2][1]['tx_hash'] == more_txs[1]
b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100, block_offset=list_defaults['block'])
assert b[0] == mid
assert b[1] == newest
assert len(b[2]) == 2
assert b[2][0]['tx_hash'] == more_txs[0]
assert b[2][1]['tx_hash'] == more_txs[1]
b = c.load_transactions_account_with_data(list_actors['bob'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'])
assert b[0] == mid
assert b[1] == mid
assert len(b[2]) == 1
assert b[2][0]['tx_hash'] == more_txs[1]
b = c.load_transactions_account_with_data(list_actors['diane'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block'])
assert b[0] == oldest
assert b[1] == oldest
assert len(b[2]) == 1
assert b[2][0]['tx_hash'] == more_txs[2]