cic-internal-integration/apps/cic-eth/tests/unit/db/test_nonce_db.py

78 lines
1.9 KiB
Python
Raw Permalink Normal View History

2021-07-15 00:02:59 +02:00
# extended imports
2021-02-01 18:12:51 +01:00
import pytest
2021-03-06 18:55:51 +01:00
import uuid
2021-07-15 00:02:59 +02:00
import unittest
2021-02-01 18:12:51 +01:00
# local imports
2021-03-06 18:55:51 +01:00
from cic_eth.db.models.nonce import (
Nonce,
NonceReservation,
)
from cic_eth.error import (
InitializationError,
IntegrityError,
)
def test_nonce_init(
init_database,
eth_empty_accounts,
):
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
init_database.commit()
with pytest.raises(InitializationError):
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
2021-02-01 18:12:51 +01:00
def test_nonce_increment(
init_database,
eth_empty_accounts,
database_engine,
):
nonce = Nonce.next(eth_empty_accounts[0], 3)
assert nonce == 3
nonce = Nonce.next(eth_empty_accounts[0], 3)
assert nonce == 4
2021-03-06 18:55:51 +01:00
def test_nonce_reserve(
init_database,
eth_empty_accounts,
):
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
init_database.commit()
uu = uuid.uuid4()
nonce = NonceReservation.next(eth_empty_accounts[0], str(uu), session=init_database)
init_database.commit()
assert nonce == (str(uu), 42)
2021-03-06 18:55:51 +01:00
q = init_database.query(Nonce)
q = q.filter(Nonce.address_hex==eth_empty_accounts[0])
o = q.first()
assert o.nonce == 43
2021-07-15 00:02:59 +02:00
nonce = NonceReservation.release(eth_empty_accounts[0], str(uu), session=init_database)
2021-03-06 18:55:51 +01:00
init_database.commit()
assert nonce == (str(uu), 42)
2021-03-06 18:55:51 +01:00
q = init_database.query(NonceReservation)
q = q.filter(NonceReservation.key==str(uu))
o = q.first()
assert o == None
def test_nonce_reserve_integrity(
init_database,
eth_empty_accounts,
):
uu = uuid.uuid4()
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
with pytest.raises(IntegrityError):
NonceReservation.release(eth_empty_accounts[0], str(uu))