""" Test database models and basic operations. """ import pytest from sqlalchemy import inspect, text from datetime import datetime @pytest.fixture def test_db(): """Database session for model tests.""" yield None # Will be provided by conftest.py class TestAuthorModel: """Tests for the Author model.""" def test_author_creation(self, db_session): """Test Author model creation with required fields.""" from models.author import Author author = Author( name="John Doe", email="john@example.com", bio="A test author" ) assert author.id is None # ID assigned after commit def test_author_unique_constraints(self, db_session): """Test that authors have unique name and email.""" from models.author import Author # Create first author author1 = Author(name="Jane Doe", email="jane@example.com") db_session.add(author1) db_session.commit() # Try to create duplicate try: author2 = Author(name="Jane Doe", email="newemail@example.com") db_session.add(author2) db_session.commit() # Should raise IntegrityError pytest.fail("Expected IntegrityError for duplicate name") except Exception as e: assert "IntegrityError" in str(type(e).__name__) or "foreign_key" in str(type(e).__name__).lower() def test_author_relationship(self, db_session): """Test author has articles relationship.""" from models.author import Author author = Author(name="Test Author", email="test@example.com") assert hasattr(author, "articles") assert author.articles == [] # Initially empty class TestCategoryModel: """Tests for the Category model.""" def test_category_creation(self, db_session): """Test Category model creation.""" from models.category import Category category = Category(name="Technology", slug="technology") assert category.id is None # ID assigned after commit def test_category_unique_constraints(self, db_session): """Test that categories have unique name and slug.""" from models.category import Category category1 = Category(name="News", slug="news") db_session.add(category1) db_session.commit() # Try duplicate slug try: category2 = Category(name="Sports", slug="news") # Same slug as news db_session.add(category2) db_session.commit() pytest.fail("Expected IntegrityError for duplicate slug") except Exception: pass # Expected class TestArticleModel: """Tests for the Article model.""" def test_article_creation(self, db_session): """Test Article model creation with required fields.""" from models.author import Author from models.category import Category from models.article import Article author = Author(name="Author", email="author@example.com") category = Category(name="Category", slug="category") db_session.add_all([author, category]) db_session.commit() article = Article( title="Test Article", content="This is test content", author_id=author.id, category_id=category.id, published=True ) assert article.title == "Test Article" def test_article_default_published(self, db_session): """Test that articles default to unpublished.""" from models.author import Author from models.category import Category from models.article import Article author = Author(name="Author", email="author@example.com") category = Category(name="Category", slug="category") db_session.add_all([author, category]) db_session.commit() article = Article( title="Test Article", content="Content", author_id=author.id, category_id=category.id # published not specified - should default to False ) assert article.published is False class TestDatabaseSetup: """Tests for database initialization.""" def test_tables_exist(self, db_engine): """Test that all required tables are created.""" inspector = inspect(db_engine) table_names = [t["name"] for t in inspector.get_table_names()] assert "authors" in table_names assert "categories" in table_names assert "articles" in table_names def test_foreign_keys_exist(self, db_session): """Test that foreign key relationships are defined.""" from models.article import Article inspector = inspect(db_engine) article_constraints = inspector.get_table_constraints("articles") constraint_names = [c["name"] for c in article_constraints] assert any("author_id" in str(c) for c in article_constraints) assert any("category_id" in str(c) for c in article_constraints)