import uuid
from datetime import datetime
import pytest
from nomy_data_models.models.wallet_state import DataState, SyncState, WalletState
@pytest.fixture
def empty_wallet_state():
wallet_state = WalletState()
wallet_state.id = uuid.uuid4()
wallet_state.wallet_address = "0x1234567890123456789012345678901234567890"
wallet_state.chain_id = 1
wallet_state.raw_data_state = DataState.EMPTY
wallet_state.enriched_data_state = DataState.EMPTY
wallet_state.sync_state = SyncState.PENDING
return wallet_state
@pytest.fixture
def partial_wallet_state():
wallet_state = WalletState()
wallet_state.id = uuid.uuid4()
wallet_state.wallet_address = "0x1234567890123456789012345678901234567890"
wallet_state.chain_id = 1
wallet_state.raw_data_state = DataState.PARTIAL
wallet_state.enriched_data_state = DataState.PARTIAL
wallet_state.sync_state = SyncState.SYNCED
wallet_state.raw_first_block = 1000000
wallet_state.raw_last_block = 2000000
wallet_state.enriched_first_block = 1000000
wallet_state.enriched_last_block = 1500000
wallet_state.last_sync_at = datetime.now()
return wallet_state
class TestDataState:
def test_data_state_values(self):
assert DataState.EMPTY.value == "empty", "EMPTY value should be 'empty'"
assert DataState.PARTIAL.value == "partial", "PARTIAL value should be 'partial'"
assert (
DataState.COMPLETE.value == "complete"
), "COMPLETE value should be 'complete'"
assert DataState.ERROR.value == "error", "ERROR value should be 'error'"
def test_data_state_comparison(self):
assert DataState.EMPTY == DataState.EMPTY, "EMPTY should equal itself"
assert DataState.EMPTY != DataState.PARTIAL, "EMPTY should not equal PARTIAL"
assert DataState.PARTIAL == DataState.PARTIAL, "PARTIAL should equal itself"
assert DataState.COMPLETE == DataState.COMPLETE, "COMPLETE should equal itself"
assert DataState.ERROR == DataState.ERROR, "ERROR should equal itself"
class TestSyncState:
def test_sync_state_values(self):
assert SyncState.PENDING.value == "pending", "PENDING value should be 'pending'"
assert SyncState.SYNCING.value == "syncing", "SYNCING value should be 'syncing'"
assert SyncState.SYNCED.value == "synced", "SYNCED value should be 'synced'"
assert SyncState.FAILED.value == "failed", "FAILED value should be 'failed'"
def test_sync_state_comparison(self):
assert SyncState.PENDING == SyncState.PENDING, "PENDING should equal itself"
assert (
SyncState.PENDING != SyncState.SYNCING
), "PENDING should not equal SYNCING"
assert SyncState.SYNCING == SyncState.SYNCING, "SYNCING should equal itself"
assert SyncState.SYNCED == SyncState.SYNCED, "SYNCED should equal itself"
assert SyncState.FAILED == SyncState.FAILED, "FAILED should equal itself"
class TestWalletState:
def test_wallet_state_creation(self, empty_wallet_state):
assert empty_wallet_state.id is not None, "ID should be set"
assert (
empty_wallet_state.wallet_address
== "0x1234567890123456789012345678901234567890"
), "Wallet address should match"
assert empty_wallet_state.chain_id == 1, "Chain ID should be 1"
assert (
empty_wallet_state.raw_data_state == DataState.EMPTY
), "Raw data state should be EMPTY"
assert (
empty_wallet_state.enriched_data_state == DataState.EMPTY
), "Enriched data state should be EMPTY"
assert (
empty_wallet_state.sync_state == SyncState.PENDING
), "Sync state should be PENDING"
def test_wallet_state_with_block_data(self, partial_wallet_state):
assert partial_wallet_state.id is not None, "ID should be set"
assert (
partial_wallet_state.wallet_address
== "0x1234567890123456789012345678901234567890"
), "Wallet address should match"
assert partial_wallet_state.chain_id == 1, "Chain ID should be 1"
assert (
partial_wallet_state.raw_data_state == DataState.PARTIAL
), "Raw data state should be PARTIAL"
assert (
partial_wallet_state.enriched_data_state == DataState.PARTIAL
), "Enriched data state should be PARTIAL"
assert (
partial_wallet_state.sync_state == SyncState.SYNCED
), "Sync state should be SYNCED"
assert (
partial_wallet_state.raw_first_block == 1000000
), "Raw first block should be 1000000"
assert (
partial_wallet_state.raw_last_block == 2000000
), "Raw last block should be 2000000"
assert (
partial_wallet_state.enriched_first_block == 1000000
), "Enriched first block should be 1000000"
assert (
partial_wallet_state.enriched_last_block == 1500000
), "Enriched last block should be 1500000"
assert (
partial_wallet_state.last_sync_at is not None
), "Last sync timestamp should be set"
def test_wallet_state_repr(self):
wallet_state = WalletState()
wallet_state.wallet_address = "0x1234567890123456789012345678901234567890"
wallet_state.chain_id = 1
wallet_state.sync_state = SyncState.SYNCING
expected_repr = (
"<WalletState(wallet=0x123456..., " "chain_id=1, " "sync_state=syncing)>"
)
assert (
str(wallet_state) == expected_repr
), "String representation should match expected format"
def test_wallet_state_db_integration(self, session):
wallet_state = WalletState()
wallet_state.wallet_address = "0x1234567890123456789012345678901234567890"
wallet_state.chain_id = 1
wallet_state.raw_data_state = DataState.PARTIAL
wallet_state.enriched_data_state = DataState.EMPTY
wallet_state.sync_state = SyncState.SYNCING
wallet_state.raw_first_block = 1000000
wallet_state.raw_last_block = 2000000
wallet_state.last_sync_at = datetime.now()
session.add(wallet_state)
session.commit()
retrieved = (
session.query(WalletState)
.filter_by(
wallet_address="0x1234567890123456789012345678901234567890", chain_id=1
)
.first()
)
assert retrieved is not None, "Wallet state should be retrievable from database"
assert retrieved.id == wallet_state.id, "Retrieved wallet state ID should match"
assert (
retrieved.wallet_address == "0x1234567890123456789012345678901234567890"
), "Retrieved wallet address should match"
assert retrieved.chain_id == 1, "Retrieved chain ID should match"
assert (
retrieved.raw_data_state == DataState.PARTIAL
), "Retrieved raw data state should match"
assert (
retrieved.enriched_data_state == DataState.EMPTY
), "Retrieved enriched data state should match"
assert (
retrieved.sync_state == SyncState.SYNCING
), "Retrieved sync state should match"
assert (
retrieved.raw_first_block == 1000000
), "Retrieved raw first block should match"
assert (
retrieved.raw_last_block == 2000000
), "Retrieved raw last block should match"
assert (
retrieved.last_sync_at is not None
), "Retrieved last sync timestamp should be set"
if __name__ == "__main__":
pytest.main(["-v", __file__])