File size: 3,560 Bytes
d89ad0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for storage functionality."""
import pytest
import tempfile
import os
from app.store.db import init_db, save_result, load_result, _gen_id


def test_gen_id():
    """Test ID generation."""
    rid1 = _gen_id()
    rid2 = _gen_id()
    
    # Should generate different IDs
    assert rid1 != rid2
    
    # Should be reasonable length
    assert 6 <= len(rid1) <= 10
    assert 6 <= len(rid2) <= 10
    
    # Should only contain URL-safe characters
    allowed = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    assert all(c in allowed for c in rid1)


def test_save_and_load_result():
    """Test saving and loading results."""
    # Use temporary database
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
        tmp_path = tmp.name
    
    try:
        # Override DB_PATH for this test
        original_path = os.environ.get("DB_PATH")
        os.environ["DB_PATH"] = tmp_path
        
        # Reload the module to pick up new path
        import importlib
        from app.store import db
        importlib.reload(db)
        
        # Initialize database
        db.init_db()
        
        # Test data
        test_result = {
            "claim": "Test claim for storage",
            "verdict": "True",
            "confidence": 0.85,
            "rationale": "Test rationale",
            "post": "Test post content",
            "sources": [
                {
                    "title": "Test Source",
                    "url": "https://example.com",
                    "snippet": "Test snippet",
                    "evidence": ["Test evidence"]
                }
            ]
        }
        
        # Save result
        rid = db.save_result(test_result)
        assert rid
        assert len(rid) >= 6
        
        # Load result
        loaded = db.load_result(rid)
        assert loaded is not None
        assert loaded["claim"] == test_result["claim"]
        assert loaded["verdict"] == test_result["verdict"]
        assert loaded["confidence"] == test_result["confidence"]
        assert loaded["id"] == rid  # Should have added ID
        
        # Test non-existent ID
        missing = db.load_result("nonexistent")
        assert missing is None
        
    finally:
        # Cleanup
        if original_path:
            os.environ["DB_PATH"] = original_path
        else:
            os.environ.pop("DB_PATH", None)
        
        # Remove temp file
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)


def test_save_result_with_existing_id():
    """Test saving result with existing ID."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
        tmp_path = tmp.name
    
    try:
        original_path = os.environ.get("DB_PATH")
        os.environ["DB_PATH"] = tmp_path
        
        import importlib
        from app.store import db
        importlib.reload(db)
        
        db.init_db()
        
        # Test with predefined ID
        test_result = {
            "id": "custom123",
            "claim": "Test with custom ID",
            "verdict": "False"
        }
        
        rid = db.save_result(test_result)
        assert rid == "custom123"
        
        loaded = db.load_result("custom123")
        assert loaded["claim"] == "Test with custom ID"
        
    finally:
        if original_path:
            os.environ["DB_PATH"] = original_path
        else:
            os.environ.pop("DB_PATH", None)
        
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)