nomy-data-models 0.33.0

Data model definitions for Nomy wallet analysis data processing
Documentation
"""Tests for the wallet_state module."""

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():
    """Fixture providing a WalletState instance with empty data states.

    Returns:
        WalletState: A WalletState instance with empty data states
    """
    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():
    """Fixture providing a WalletState instance with partial data states and block data.

    Returns:
        WalletState: A WalletState instance with partial data states and block data
    """
    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:
    """Test cases for DataState enum."""

    def test_data_state_values(self):
        """Test that the DataState enum has the expected values."""
        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):
        """Test that the DataState enum values can be compared."""
        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:
    """Test cases for SyncState enum."""

    def test_sync_state_values(self):
        """Test that the SyncState enum has the expected values."""
        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):
        """Test that the SyncState enum values can be compared."""
        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:
    """Test cases for WalletState model."""

    def test_wallet_state_creation(self, empty_wallet_state):
        """Test creating a WalletState instance with empty data states.

        Args:
            empty_wallet_state: Fixture providing a WalletState instance with empty data states
        """
        # Verify the instance was created correctly
        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):
        """Test creating a WalletState instance with partial data states and block data.

        Args:
            partial_wallet_state: Fixture providing a WalletState instance with partial data states
        """
        # Verify the instance was created correctly
        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):
        """Test the string representation of a WalletState."""
        # Create a WalletState instance
        wallet_state = WalletState()

        # Set required attributes
        wallet_state.wallet_address = "0x1234567890123456789012345678901234567890"
        wallet_state.chain_id = 1
        wallet_state.sync_state = SyncState.SYNCING

        # Verify the string representation
        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):
        """Test database integration with the WalletState model.

        Args:
            session: SQLAlchemy session fixture
        """
        # Create a new WalletState instance
        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()

        # Add to session and commit
        session.add(wallet_state)
        session.commit()

        # Verify the wallet state was saved to the database
        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__])