Spaces:
Sleeping
Sleeping
Download tests/test_data/test_models.py from MikeZhang09/marketwise: direct link, hf CLI and curl.
- Browser
- Download file 6.03 kB
-
https://huggingface.co/spaces/MikeZhang09/marketwise/resolve/main/tests/test_data/test_models.py
- Command line
-
hf download hf://spaces/MikeZhang09/marketwise/tests/test_data/test_models.py
-
curl -L -o test_models.py https://huggingface.co/spaces/MikeZhang09/marketwise/resolve/main/tests/test_data/test_models.py
6.03 kB
| """ | |
| Unit tests for MarketData model validation and constraints. | |
| """ | |
| import pytest | |
| from datetime import datetime | |
| from decimal import Decimal | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.exc import IntegrityError | |
| from src.data.models import Base, MarketData | |
| def engine(): | |
| """Create in-memory SQLite engine for testing.""" | |
| engine = create_engine("sqlite:///:memory:") | |
| Base.metadata.create_all(engine) | |
| return engine | |
| def session(engine): | |
| """Create database session for testing.""" | |
| Session = sessionmaker(bind=engine) | |
| session = Session() | |
| yield session | |
| session.close() | |
| def sample_market_data(): | |
| """Sample market data for testing.""" | |
| return { | |
| 'symbol': '^GSPC', | |
| 'timestamp': datetime(2024, 1, 15, 16, 0, 0), | |
| 'open_price': Decimal('4500.50'), | |
| 'high_price': Decimal('4520.75'), | |
| 'low_price': Decimal('4495.25'), | |
| 'close_price': Decimal('4510.00'), | |
| 'volume': Decimal('1000000') | |
| } | |
| class TestMarketDataModel: | |
| """Test cases for MarketData model.""" | |
| def test_create_market_data(self, session, sample_market_data): | |
| """Test creating a valid MarketData instance.""" | |
| market_data = MarketData(**sample_market_data) | |
| session.add(market_data) | |
| session.commit() | |
| assert market_data.id is not None | |
| assert market_data.symbol == '^GSPC' | |
| assert market_data.close_price == Decimal('4510.00') | |
| def test_symbol_validation_empty(self): | |
| """Test symbol validation with empty string.""" | |
| with pytest.raises(ValueError, match="Symbol must be 1-10 characters"): | |
| MarketData(symbol="") | |
| def test_symbol_validation_too_long(self): | |
| """Test symbol validation with too long string.""" | |
| with pytest.raises(ValueError, match="Symbol must be 1-10 characters"): | |
| MarketData(symbol="VERYLONGSYMBOL") | |
| def test_symbol_uppercase_conversion(self, session): | |
| """Test that symbol is converted to uppercase.""" | |
| market_data = MarketData( | |
| symbol='gspc', | |
| timestamp=datetime.now(), | |
| open_price=100, | |
| high_price=105, | |
| low_price=95, | |
| close_price=102, | |
| volume=1000 | |
| ) | |
| session.add(market_data) | |
| session.commit() | |
| assert market_data.symbol == 'GSPC' | |
| def test_price_validation_negative(self): | |
| """Test price validation with negative values.""" | |
| with pytest.raises(ValueError, match="open_price must be positive"): | |
| MarketData( | |
| symbol='TEST', | |
| timestamp=datetime.now(), | |
| open_price=-100, | |
| high_price=105, | |
| low_price=95, | |
| close_price=102, | |
| volume=1000 | |
| ) | |
| def test_price_validation_zero(self): | |
| """Test price validation with zero values.""" | |
| with pytest.raises(ValueError, match="close_price must be positive"): | |
| MarketData( | |
| symbol='TEST', | |
| timestamp=datetime.now(), | |
| open_price=100, | |
| high_price=105, | |
| low_price=95, | |
| close_price=0, | |
| volume=1000 | |
| ) | |
| def test_price_validation_none(self): | |
| """Test price validation with None values.""" | |
| with pytest.raises(ValueError, match="high_price cannot be None"): | |
| MarketData( | |
| symbol='TEST', | |
| timestamp=datetime.now(), | |
| open_price=100, | |
| high_price=None, | |
| low_price=95, | |
| close_price=102, | |
| volume=1000 | |
| ) | |
| def test_volume_validation_negative(self): | |
| """Test volume validation with negative values.""" | |
| with pytest.raises(ValueError, match="Volume must be non-negative"): | |
| MarketData( | |
| symbol='TEST', | |
| timestamp=datetime.now(), | |
| open_price=100, | |
| high_price=105, | |
| low_price=95, | |
| close_price=102, | |
| volume=-1000 | |
| ) | |
| def test_volume_validation_zero(self, session): | |
| """Test volume validation with zero (should be allowed).""" | |
| market_data = MarketData( | |
| symbol='TEST', | |
| timestamp=datetime.now(), | |
| open_price=100, | |
| high_price=105, | |
| low_price=95, | |
| close_price=102, | |
| volume=0 | |
| ) | |
| session.add(market_data) | |
| session.commit() | |
| assert market_data.volume == 0 | |
| def test_unique_constraint(self, session, sample_market_data): | |
| """Test unique constraint on symbol and timestamp.""" | |
| # Add first record | |
| market_data1 = MarketData(**sample_market_data) | |
| session.add(market_data1) | |
| session.commit() | |
| # Try to add duplicate | |
| market_data2 = MarketData(**sample_market_data) | |
| session.add(market_data2) | |
| with pytest.raises(IntegrityError): | |
| session.commit() | |
| def test_to_dict(self, session, sample_market_data): | |
| """Test conversion to dictionary.""" | |
| market_data = MarketData(**sample_market_data) | |
| session.add(market_data) | |
| session.commit() | |
| data_dict = market_data.to_dict() | |
| assert data_dict['symbol'] == '^GSPC' | |
| assert data_dict['open_price'] == 4500.50 | |
| assert data_dict['close_price'] == 4510.00 | |
| assert isinstance(data_dict['timestamp'], datetime) | |
| def test_repr(self, sample_market_data): | |
| """Test string representation.""" | |
| market_data = MarketData(**sample_market_data) | |
| repr_str = repr(market_data) | |
| assert '^GSPC' in repr_str | |
| assert '4510.00' in repr_str | |
| assert 'MarketData' in repr_str | |