File size: 4,573 Bytes
e98cc10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
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."""

    @pytest.fixture
    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