""" Unit tests for Tweet model. """ import pytest from datetime import datetime, timezone from sqlalchemy.orm import Session from app.models.tweet import Tweet from app.database import Base, engine, SessionLocal @pytest.fixture(scope="function") def db_session(): """Create a fresh database session for each test.""" # Create tables Base.metadata.create_all(bind=engine) # Create session session = SessionLocal() try: yield session session.rollback() finally: session.close() Base.metadata.drop_all(bind=engine) class TestTweetModel: """Test Tweet SQLAlchemy model.""" def test_tweet_creation(self, db_session: Session): """Test creating a tweet in the database.""" tweet = Tweet( tweet_id="123456789", text="Test tweet content", created_at=datetime.now(timezone.utc), retweet_count=10, like_count=20, match_id=1, source="twitter" ) db_session.add(tweet) db_session.commit() db_session.refresh(tweet) assert tweet.id is not None assert tweet.tweet_id == "123456789" assert tweet.text == "Test tweet content" assert tweet.retweet_count == 10 assert tweet.like_count == 20 assert tweet.match_id == 1 assert tweet.source == "twitter" def test_tweet_defaults(self, db_session: Session): """Test tweet default values.""" tweet = Tweet( tweet_id="987654321", text="Another test tweet", created_at=datetime.now(timezone.utc) ) db_session.add(tweet) db_session.commit() db_session.refresh(tweet) assert tweet.retweet_count == 0 assert tweet.like_count == 0 assert tweet.source == "twitter" assert tweet.match_id is None def test_tweet_unique_constraint(self, db_session: Session): """Test that tweet_id must be unique.""" tweet1 = Tweet( tweet_id="111111111", text="First tweet", created_at=datetime.now(timezone.utc) ) tweet2 = Tweet( tweet_id="111111111", # Same tweet_id text="Second tweet", created_at=datetime.now(timezone.utc) ) db_session.add(tweet1) db_session.commit() db_session.add(tweet2) with pytest.raises(Exception): # IntegrityError expected db_session.commit() def test_tweet_to_dict(self, db_session: Session): """Test converting tweet to dictionary.""" tweet = Tweet( tweet_id="222222222", text="Test tweet for dict", created_at=datetime.now(timezone.utc), retweet_count=5, like_count=10, match_id=2, source="reddit" ) db_session.add(tweet) db_session.commit() db_session.refresh(tweet) tweet_dict = tweet.to_dict() assert tweet_dict['tweet_id'] == "222222222" assert tweet_dict['text'] == "Test tweet for dict" assert tweet_dict['retweet_count'] == 5 assert tweet_dict['like_count'] == 10 assert tweet_dict['match_id'] == 2 assert tweet_dict['source'] == "reddit" assert 'id' in tweet_dict assert 'created_at' in tweet_dict def test_tweet_repr(self, db_session: Session): """Test tweet __repr__ method.""" tweet = Tweet( tweet_id="333333333", text="Test tweet repr", created_at=datetime.now(timezone.utc), match_id=3 ) db_session.add(tweet) db_session.commit() db_session.refresh(tweet) repr_str = repr(tweet) assert "Tweet" in repr_str assert "id=" in repr_str assert "tweet_id=333333333" in repr_str assert "match_id=3" in repr_str