File size: 4,665 Bytes
d545f81
 
 
 
3cdce90
d545f81
 
3cdce90
 
d545f81
 
 
3cdce90
d545f81
 
3cdce90
d545f81
 
 
 
 
 
 
 
 
 
 
 
3cdce90
d545f81
 
3cdce90
 
d545f81
 
 
 
3cdce90
 
 
d545f81
 
 
3cdce90
 
d545f81
 
 
 
 
 
 
 
 
 
 
3cdce90
d545f81
 
 
 
 
3cdce90
 
 
d545f81
 
 
 
 
 
 
 
3cdce90
 
 
 
d545f81
 
 
 
 
3cdce90
d545f81
 
 
 
 
 
 
 
3cdce90
d545f81
 
 
 
 
 
 
 
 
3cdce90
 
 
d545f81
3cdce90
 
 
 
d545f81
3cdce90
d545f81
 
 
3cdce90
 
 
 
d545f81
3cdce90
d545f81
3cdce90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Pytest configuration file
Sets up test environment and fixtures
"""

import os
import sys
from unittest.mock import MagicMock, Mock, patch

import pytest

# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

# Set mock environment variables before importing any modules
os.environ.setdefault("GEMINI_API_KEY", "test-api-key-12345")

# Mock Google Sheets credentials
mock_credentials = {
    "type": "service_account",
    "project_id": "test-project",
    "private_key_id": "test-key-id",
    "private_key": "-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n",
    "client_email": "test@test.iam.gserviceaccount.com",
    "client_id": "12345",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test",
}
import json

os.environ.setdefault("GOOGLE_SHEETS_CREDENTIALS", json.dumps(mock_credentials))

# Mock google.oauth2 and gspread modules before src.logger imports them
mock_credentials_class = MagicMock()
mock_creds_instance = MagicMock()
mock_credentials_class.from_service_account_info = Mock(
    return_value=mock_creds_instance
)

mock_oauth2 = MagicMock()
mock_oauth2.service_account.Credentials = mock_credentials_class
sys.modules["google.oauth2"] = mock_oauth2
sys.modules["google.oauth2.service_account"] = mock_oauth2.service_account

mock_gspread = MagicMock()
mock_spreadsheet = MagicMock()
mock_worksheet = MagicMock()
mock_worksheet.append_row = Mock()
mock_spreadsheet.get_worksheet = Mock(return_value=mock_worksheet)
mock_spreadsheet.worksheet = Mock(return_value=mock_worksheet)
mock_spreadsheet.add_worksheet = Mock(return_value=mock_worksheet)
mock_client = MagicMock()
mock_client.open = Mock(return_value=mock_spreadsheet)
mock_gspread.authorize = Mock(return_value=mock_client)
sys.modules["gspread"] = mock_gspread


@pytest.fixture(autouse=True)
def mock_google_sheets():
    """Mock Google Sheets to avoid actual connections during testing"""
    with patch("src.logger.response_sheet") as mock_response, patch(
        "src.logger.timing_sheet"
    ) as mock_timing:
        mock_response.append_row = Mock()
        mock_timing.append_row = Mock()
        yield mock_response, mock_timing


@pytest.fixture
def mock_genai():
    """Mock Google Generative AI"""
    with patch("google.generativeai.configure") as mock_config, patch(
        "google.generativeai.GenerativeModel"
    ) as mock_model, patch("google.generativeai.embed_content") as mock_embed:
        yield {"configure": mock_config, "model": mock_model, "embed": mock_embed}


@pytest.fixture
def mock_chromadb():
    """Mock ChromaDB client"""
    with patch("chromadb.PersistentClient") as mock_client:
        mock_collection = Mock()
        mock_client.return_value.get_collection.return_value = mock_collection
        yield mock_client


@pytest.fixture
def mock_sqlite():
    """Mock SQLite connections for memory"""
    with patch("sqlite3.connect") as mock_connect:
        mock_conn = Mock()
        mock_connect.return_value = mock_conn
        yield mock_conn


@pytest.fixture
def sample_documents():
    """Provide sample documents for testing"""
    doc1 = Mock()
    doc1.page_content = (
        "Question: How do I create an account?\nAnswer: Visit our website."
    )
    doc1.metadata = {
        "id": "KB001",
        "question": "How do I create an account?",
        "content": "Visit our website.",
        "section": "Account Management",
    }

    doc2 = Mock()
    doc2.page_content = "Question: What are the fees?\nAnswer: 1% per transaction."
    doc2.metadata = {
        "id": "KB002",
        "question": "What are the fees?",
        "content": "1% per transaction.",
        "section": "Fees",
    }

    return [doc1, doc2]


@pytest.fixture
def mock_genai_client():
    """Mock Google Generative AI client with new SDK structure"""
    with patch("src.config.genai_client") as mock_client:
        # Mock generate_content for LLM
        mock_generate_response = Mock()
        mock_generate_response.text = "Test response from LLM"
        mock_client.models.generate_content.return_value = mock_generate_response
        
        # Mock embed_content for embeddings
        mock_embedding = Mock()
        mock_embedding.values = [0.1, 0.2, 0.3]
        mock_embed_response = Mock()
        mock_embed_response.embeddings = [mock_embedding]
        mock_client.models.embed_content.return_value = mock_embed_response
        
        yield mock_client