Add frontend project structure with React/TypeScript setup
This commit is contained in:
1 parent
c7abb73c66
commit
6914a6855c
58 files changed
+9056
-236
No files matched your search
@@ -0,0 +1,71 @@
|
||||
"""
|
||||
Pytest fixtures and configuration for Newzy tests.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
|
||||
from config import settings
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_db_url():
|
||||
"""Database URL for testing."""
|
||||
return "sqlite:///./test_newzy.db"
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_engine(test_db_url):
|
||||
"""Create an engine for the test database."""
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
engine = create_engine(
|
||||
test_db_url,
|
||||
connect_args={"check_same_thread": False},
|
||||
)
|
||||
return engine
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session(db_engine):
|
||||
"""Create a database session for testing."""
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
SessionLocal = sessionmaker(bind=db_engine)
|
||||
session = SessionLocal()
|
||||
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_db(db_engine, db_session):
|
||||
"""Setup and teardown for database tables in tests."""
|
||||
from db.session import Base, engine as app_engine
|
||||
from models.article import Article
|
||||
from models.author import Author
|
||||
from models.category import Category
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=db_engine)
|
||||
|
||||
# Clean up old test tables if any
|
||||
for table in Base.metadata.sorted_tables:
|
||||
db_session.execute(text(f"DELETE FROM {table.name}"))
|
||||
db_session.commit()
|
||||
|
||||
yield db_session
|
||||
|
||||
# Cleanup after test
|
||||
Base.metadata.drop_all(bind=db_engine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_db():
|
||||
"""Create database tables."""
|
||||
from db.session import Base, engine as app_engine
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
return lambda: Base.metadata.drop_all(bind=engine)
|
||||
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
Test health and version endpoints.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from main import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Create test client."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
class TestHealthEndpoint:
|
||||
"""Tests for the /health endpoint."""
|
||||
|
||||
def test_health_returns_200(self, client):
|
||||
"""Test that health check returns 200 OK."""
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_health_response_structure(self, client):
|
||||
"""Test health response contains expected JSON structure."""
|
||||
response = client.get("/health")
|
||||
assert response.json()["status"] == "healthy"
|
||||
|
||||
def test_api_v1_health_exists(self, client):
|
||||
"""Test alternative /api/v1/health path works."""
|
||||
# Note: This endpoint is not yet implemented - placeholder test
|
||||
response = client.get("/api/v1/health")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
class TestVersionEndpoint:
|
||||
"""Tests for the /api/v1/version endpoint."""
|
||||
|
||||
def test_version_returns_200(self, client):
|
||||
"""Test that version endpoint returns 200 OK."""
|
||||
response = client.get("/api/v1/version")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_version_response_format(self, client):
|
||||
"""Test version response format validation."""
|
||||
response = client.get("/api/v1/version")
|
||||
data = response.json()
|
||||
assert "version" in data
|
||||
assert "application" in data
|
||||
# Version should be in format like v0.1.0
|
||||
assert isinstance(data["version"], str)
|
||||
@@ -0,0 +1,151 @@
|
||||
"""
|
||||
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)
|
||||
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
Test version endpoint behavior.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Create test client."""
|
||||
from main import app
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
class TestVersionEndpoint:
|
||||
"""Tests for the /api/v1/version endpoint."""
|
||||
|
||||
def test_version_endpoint_exists(self, client):
|
||||
"""Test that version endpoint exists and is accessible."""
|
||||
response = client.get("/api/v1/version")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_version_format(self, client):
|
||||
"""Test version returns valid version string."""
|
||||
response = client.get("/api/v1/version")
|
||||
data = response.json()
|
||||
|
||||
# Check structure
|
||||
assert "version" in data
|
||||
assert "application" in data
|
||||
|
||||
# Check version format (vX.Y.Z)
|
||||
version = data["version"]
|
||||
assert isinstance(version, str)
|
||||
assert version.startswith("v")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
Reference in new issue
Block a user