-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathconftest.py
More file actions
166 lines (130 loc) · 3.79 KB
/
conftest.py
File metadata and controls
166 lines (130 loc) · 3.79 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import tempfile
import shutil
from pathlib import Path
import pytest
import json
@pytest.fixture
def temp_dir():
"""Create a temporary directory for test files."""
temp_path = tempfile.mkdtemp()
yield Path(temp_path)
shutil.rmtree(temp_path)
@pytest.fixture
def sample_csv_content():
"""Sample CSV content for testing."""
return """name,age,city
John Doe,30,New York
Jane Smith,25,Los Angeles
Bob Johnson,35,Chicago"""
@pytest.fixture
def sample_csv_file(temp_dir, sample_csv_content):
"""Create a sample CSV file in temp directory."""
csv_path = temp_dir / "sample.csv"
csv_path.write_text(sample_csv_content)
return csv_path
@pytest.fixture
def mock_config():
"""Mock configuration dictionary."""
return {
"host": "0.0.0.0",
"port": 5050,
"debug": False,
"models": ["sqlova", "valuenet", "irnet"],
"database_path": "/tmp/test_dbs"
}
@pytest.fixture
def mock_api_request():
"""Mock API request payload."""
return {
"csv": "name,age,city\\nJohn,30,NYC",
"question": "What is the average age?",
"model": "valuenet"
}
@pytest.fixture
def mock_sql_response():
"""Mock SQL response."""
return {
"sql": "SELECT AVG(age) FROM table",
"confidence": 0.95,
"model": "valuenet"
}
@pytest.fixture
def sample_sqlite_db(temp_dir):
"""Create a sample SQLite database."""
import sqlite3
db_path = temp_dir / "test.db"
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
city TEXT
)
''')
users = [
(1, 'John Doe', 30, 'New York'),
(2, 'Jane Smith', 25, 'Los Angeles'),
(3, 'Bob Johnson', 35, 'Chicago')
]
cursor.executemany('INSERT INTO users VALUES (?, ?, ?, ?)', users)
conn.commit()
conn.close()
return db_path
@pytest.fixture
def flask_test_client(monkeypatch):
"""Create a Flask test client (placeholder for actual implementation)."""
monkeypatch.setenv("FLASK_ENV", "testing")
return None
@pytest.fixture
def mock_model_predictions():
"""Mock model prediction results."""
return {
"sqlova": {
"sql": "SELECT * FROM table WHERE age > 25",
"confidence": 0.89
},
"valuenet": {
"sql": "SELECT AVG(age) FROM table WHERE city = 'NYC'",
"confidence": 0.92
},
"irnet": {
"sql": "SELECT name, age FROM table ORDER BY age DESC",
"confidence": 0.87
}
}
@pytest.fixture(autouse=True)
def reset_environment():
"""Reset environment variables before each test."""
original_env = os.environ.copy()
yield
os.environ.clear()
os.environ.update(original_env)
@pytest.fixture
def captured_logs():
"""Capture log messages during tests."""
import logging
from io import StringIO
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.DEBUG)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
yield log_capture
root_logger.removeHandler(handler)
@pytest.fixture
def mock_http_response():
"""Mock HTTP response object."""
class MockResponse:
def __init__(self, json_data, status_code=200):
self.json_data = json_data
self.status_code = status_code
self.text = json.dumps(json_data)
def json(self):
return self.json_data
def raise_for_status(self):
if self.status_code >= 400:
raise Exception(f"HTTP {self.status_code}")
return MockResponse