Translate StatusEnum to flags instead of number ranges
This commit is contained in:
20
apps/cic-eth/tests/unit/db/test_enum.py
Normal file
20
apps/cic-eth/tests/unit/db/test_enum.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from cic_eth.db.enum import (
|
||||
StatusEnum,
|
||||
StatusBits,
|
||||
status_str,
|
||||
)
|
||||
|
||||
|
||||
def test_status_str():
|
||||
|
||||
# String representation for a status in StatusEnum
|
||||
s = status_str(StatusEnum.REVERTED)
|
||||
assert s == 'REVERTED'
|
||||
|
||||
# String representation for a status not in StatusEnum
|
||||
s = status_str(StatusBits.LOCAL_ERROR | StatusBits.NODE_ERROR)
|
||||
assert s == 'LOCAL_ERROR,NODE_ERROR*'
|
||||
|
||||
# String representation for a status in StatusEnum, but bits only representation bit set
|
||||
s = status_str(StatusEnum.REVERTED, bits_only=True)
|
||||
assert s == 'IN_NETWORK,NETWORK_ERROR,FINAL'
|
||||
@@ -9,7 +9,11 @@ import pytest
|
||||
from cic_eth.db.models.base import SessionBase
|
||||
from cic_eth.db.models.otx import OtxStateLog
|
||||
from cic_eth.db.models.otx import Otx
|
||||
from cic_eth.db.enum import StatusEnum
|
||||
from cic_eth.db.enum import (
|
||||
StatusEnum,
|
||||
StatusBits,
|
||||
is_alive,
|
||||
)
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
@@ -70,15 +74,24 @@ def test_state_log(
|
||||
otx = Otx.add(0, address, tx_hash, signed_tx, session=init_database)
|
||||
|
||||
otx.waitforgas(session=init_database)
|
||||
init_database.commit()
|
||||
|
||||
otx.readysend(session=init_database)
|
||||
init_database.commit()
|
||||
|
||||
otx.sent(session=init_database)
|
||||
init_database.commit()
|
||||
|
||||
otx.success(1024, session=init_database)
|
||||
init_database.commit()
|
||||
|
||||
q = init_database.query(OtxStateLog)
|
||||
q = q.filter(OtxStateLog.otx_id==otx.id)
|
||||
q = q.order_by(OtxStateLog.date.asc())
|
||||
logs = q.all()
|
||||
|
||||
|
||||
assert logs[0].status == StatusEnum.PENDING
|
||||
assert logs[1].status == StatusEnum.WAITFORGAS
|
||||
assert logs[2].status == StatusEnum.SENT
|
||||
assert logs[3].status == StatusEnum.SUCCESS
|
||||
assert logs[2].status & StatusBits.QUEUED
|
||||
assert logs[3].status & StatusBits.IN_NETWORK
|
||||
assert not is_alive(logs[4].status)
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
import pytest
|
||||
|
||||
# local imports
|
||||
from cic_eth.db import Otx
|
||||
from cic_eth.db.error import TxStateChangeError
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
# Check that invalid transitions throw exceptions
|
||||
# sent
|
||||
def test_db_queue_states(
|
||||
init_database,
|
||||
):
|
||||
|
||||
session = init_database
|
||||
|
||||
# these values are completely arbitary
|
||||
tx_hash = '0xF182DFA3AD48723E7E222FE7B4C2C44C23CD4D7FF413E8999DFA15ECE53F'
|
||||
address = '0x38C5559D6EDDDA1F705D3AB1A664CA1B397EB119'
|
||||
signed_tx = '0xA5866A5383249AE843546BDA46235A1CA1614F538FB486140693C2EF1956FC53213F6AEF0F99F44D7103871AF3A12B126DCF9BFB7AF11143FAB3ECE2B452EE35D1320C4C7C6F999C8DF4EB09E729715B573F6672ED852547F552C4AE99D17DCD14C810'
|
||||
o = Otx(
|
||||
nonce=42,
|
||||
address=address[2:],
|
||||
tx_hash=tx_hash[2:],
|
||||
signed_tx=signed_tx[2:],
|
||||
)
|
||||
session.add(o)
|
||||
session.commit()
|
||||
|
||||
o.sent(session=session)
|
||||
session.commit()
|
||||
|
||||
# send after sent is ok
|
||||
o.sent(session=session)
|
||||
session.commit()
|
||||
|
||||
o.sendfail(session=session)
|
||||
session.commit()
|
||||
|
||||
with pytest.raises(TxStateChangeError):
|
||||
o.sendfail(session=session)
|
||||
|
||||
o.sent(session=session)
|
||||
session.commit()
|
||||
|
||||
o.minefail(1234, session=session)
|
||||
session.commit()
|
||||
|
||||
with pytest.raises(TxStateChangeError):
|
||||
o.sent(session=session)
|
||||
97
apps/cic-eth/tests/unit/db/test_status.py
Normal file
97
apps/cic-eth/tests/unit/db/test_status.py
Normal file
@@ -0,0 +1,97 @@
|
||||
# standard imports
|
||||
import os
|
||||
|
||||
# third-party imports
|
||||
import pytest
|
||||
|
||||
# local imports
|
||||
from cic_eth.db.models.otx import Otx
|
||||
from cic_eth.db.enum import (
|
||||
StatusEnum,
|
||||
StatusBits,
|
||||
is_alive,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def otx(
|
||||
init_database,
|
||||
):
|
||||
|
||||
bogus_hash = '0x' + os.urandom(32).hex()
|
||||
bogus_address = '0x' + os.urandom(20).hex()
|
||||
bogus_tx_raw = '0x' + os.urandom(128).hex()
|
||||
return Otx(0, bogus_address, bogus_hash, bogus_tx_raw)
|
||||
|
||||
|
||||
def test_status_chain_gas(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.waitforgas(init_database)
|
||||
otx.readysend(init_database)
|
||||
otx.sent(init_database)
|
||||
otx.success(1024, init_database)
|
||||
assert not is_alive(otx.status)
|
||||
|
||||
|
||||
def test_status_chain_straight_success(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.readysend(init_database)
|
||||
otx.sent(init_database)
|
||||
otx.success(1024, init_database)
|
||||
assert not is_alive(otx.status)
|
||||
|
||||
|
||||
def test_status_chain_straight_revert(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.readysend(init_database)
|
||||
otx.sent(init_database)
|
||||
otx.minefail(1024, init_database)
|
||||
assert not is_alive(otx.status)
|
||||
|
||||
|
||||
def test_status_chain_nodeerror(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.readysend(init_database)
|
||||
otx.sendfail(init_database)
|
||||
otx.retry(init_database)
|
||||
otx.sent(init_database)
|
||||
otx.success(1024, init_database)
|
||||
assert not is_alive(otx.status)
|
||||
|
||||
|
||||
|
||||
def test_status_chain_nodeerror_multiple(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.readysend(init_database)
|
||||
otx.sendfail(init_database)
|
||||
otx.retry(init_database)
|
||||
otx.sendfail(init_database)
|
||||
otx.retry(init_database)
|
||||
otx.sent(init_database)
|
||||
otx.success(1024, init_database)
|
||||
assert not is_alive(otx.status)
|
||||
|
||||
|
||||
def test_status_chain_nodeerror(
|
||||
init_database,
|
||||
otx,
|
||||
):
|
||||
|
||||
otx.readysend(init_database)
|
||||
otx.reject(init_database)
|
||||
assert not is_alive(otx.status)
|
||||
Reference in New Issue
Block a user