nomy-data-models 0.35.6

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

import datetime
import uuid
from decimal import Decimal

import pytest
from sqlalchemy.exc import IntegrityError

from nomy_data_models.models.market_price import MarketPrice


@pytest.fixture
def market_price():
    """Fixture providing a MarketPrice instance."""
    price = MarketPrice()
    price.token_symbol = "BTC"
    price.price_usd = Decimal("50000.0")
    price.source = "coinmarketcap"
    return price


class TestMarketPrice:
    """Test cases for MarketPrice."""

    def test_market_price_creation(self):
        """Test creating a market price instance."""
        # Create a new market price instance
        price = MarketPrice()
        price.token_symbol = "ETH"
        price.price_usd = Decimal("3000.0")
        price.source = "coingecko"

        # Verify the instance was created correctly
        assert price.token_symbol == "ETH", "Token symbol should be 'ETH'"
        assert price.price_usd == Decimal("3000.0"), "Price should be 3000.0"
        assert price.source == "coingecko", "Source should be 'coingecko'"

    def test_market_price_repr(self, market_price):
        """Test the string representation of a market price."""
        repr_str = repr(market_price)
        assert "MarketPrice" in repr_str
        assert "token_symbol=BTC" in repr_str

    def test_market_price_db_integration(self, session):
        """Test database integration with the market price model."""
        # Create a new market price instance
        price = MarketPrice()
        price.token_symbol = "ETH"
        price.price_usd = Decimal("3000.0")
        price.source = "coingecko"
        price.market_cap_usd = Decimal("500000000.0")
        price.volume_24h_usd = Decimal("25000000.0")

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

        # Verify the price was saved to the database
        retrieved = session.query(MarketPrice).filter_by(token_symbol="ETH").first()
        assert retrieved is not None, "MarketPrice should be retrievable from database"
        assert retrieved.token_symbol == "ETH", "Retrieved token symbol should match"
        assert retrieved.price_usd == Decimal("3000.0"), "Retrieved price should match"
        assert retrieved.source == "coingecko", "Retrieved source should match"
        assert retrieved.market_cap_usd == Decimal(
            "500000000.0"
        ), "Retrieved market cap should match"
        assert retrieved.volume_24h_usd == Decimal(
            "25000000.0"
        ), "Retrieved volume should match"

    def test_market_price_unique_constraint(self, session):
        """Test that the unique constraint on token_symbol works."""
        # Create and save a market price
        price1 = MarketPrice()
        price1.token_symbol = "SOL"
        price1.price_usd = Decimal("100.0")
        price1.source = "coinmarketcap"

        session.add(price1)
        session.commit()

        # Try to create another price with the same token_symbol
        price2 = MarketPrice()
        price2.token_symbol = "SOL"
        price2.price_usd = Decimal("101.0")
        price2.source = "coingecko"

        session.add(price2)
        with pytest.raises(Exception):
            session.commit()

    def test_market_price_update(self, session):
        """Test updating a market price."""
        # Create and save a market price
        price = MarketPrice()
        price.token_symbol = "AVAX"
        price.price_usd = Decimal("20.0")
        price.source = "coinmarketcap"

        session.add(price)
        session.commit()

        # Update the price
        price.price_usd = Decimal("25.0")
        session.commit()

        # Verify the update
        retrieved = session.query(MarketPrice).filter_by(token_symbol="AVAX").first()
        assert retrieved.price_usd == Decimal("25.0"), "Price should be updated to 25.0"