Spaces:
Running
Running
| """ | |
| Integration Tests for Database Module | |
| Tests for SQLite article caching operations. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| from typing import Any | |
| from unittest.mock import patch | |
| import pytest | |
| class TestDatabaseOperations: | |
| """Tests for database CRUD operations.""" | |
| def temp_db(self, tmp_path: Path) -> Path: | |
| """Create a temporary database path.""" | |
| return tmp_path / "test_articles.db" | |
| def test_save_and_get_article(self, temp_db: Path, sample_article: dict[str, Any]) -> None: | |
| """Test saving and retrieving an article.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import save_article, get_article | |
| # Save the article | |
| save_article(sample_article) | |
| # Retrieve it | |
| url = sample_article.get("url", "https://medium.com/test") | |
| result = get_article(url) | |
| if result is not None: | |
| assert result.get("title") == sample_article.get("title") | |
| def test_get_nonexistent_article(self, temp_db: Path) -> None: | |
| """Test getting an article that doesn't exist.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import get_article | |
| result = get_article("https://medium.com/nonexistent-article") | |
| assert result is None | |
| def test_update_existing_article( | |
| self, temp_db: Path, sample_article: dict[str, Any] | |
| ) -> None: | |
| """Test updating an existing article.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import save_article, get_article | |
| # Save original | |
| save_article(sample_article) | |
| # Update with new title | |
| updated = sample_article.copy() | |
| updated["title"] = "Updated Title" | |
| save_article(updated) | |
| # Verify update | |
| url = sample_article.get("url", "") | |
| result = get_article(url) | |
| if result is not None: | |
| assert result.get("title") == "Updated Title" | |
| class TestDatabaseSearch: | |
| """Tests for database search functionality.""" | |
| def test_search_by_title(self, temp_db: Path, sample_article: dict[str, Any]) -> None: | |
| """Test searching articles by title.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import save_article | |
| # Save article for searching | |
| save_article(sample_article) | |
| # Search functionality would be tested here | |
| # Placeholder for actual search tests | |
| class TestDatabaseEdgeCases: | |
| """Tests for database edge cases.""" | |
| def test_save_article_with_special_characters( | |
| self, temp_db: Path, sample_article: dict[str, Any] | |
| ) -> None: | |
| """Test saving article with special characters.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import save_article, get_article | |
| # Article with special characters | |
| special_article = sample_article.copy() | |
| special_article["title"] = "Test with 'quotes' and \"double quotes\"" | |
| special_article["url"] = "https://medium.com/special-chars-test" | |
| # Should not raise | |
| save_article(special_article) | |
| result = get_article(special_article["url"]) | |
| if result is not None: | |
| assert "quotes" in result.get("title", "") | |
| def test_save_article_with_unicode( | |
| self, temp_db: Path, sample_article: dict[str, Any] | |
| ) -> None: | |
| """Test saving article with Unicode content.""" | |
| with patch.dict(os.environ, {"ARTICLES_DB_PATH": str(temp_db)}): | |
| from src.database import save_article, get_article | |
| # Article with Unicode | |
| unicode_article = sample_article.copy() | |
| unicode_article["title"] = "日本語タイトル 🚀" | |
| unicode_article["url"] = "https://medium.com/unicode-test" | |
| # Should handle Unicode | |
| save_article(unicode_article) | |
| result = get_article(unicode_article["url"]) | |
| if result is not None: | |
| assert "日本語" in result.get("title", "") or result is not None | |