-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic.py
More file actions
131 lines (100 loc) · 3.74 KB
/
test_basic.py
File metadata and controls
131 lines (100 loc) · 3.74 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
"""
Basic tests for Txt2SQL.
Run with: pytest test_basic.py
"""
import pytest
import sqlite3
import tempfile
from pathlib import Path
from database import DatabaseManager
from utils import validate_sql, format_results, truncate_text
class TestDatabaseManager:
"""Test database operations."""
@pytest.fixture
def temp_db(self):
"""Create a temporary test database."""
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db_path = f.name
# Create test schema
conn = sqlite3.connect(db_path)
conn.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)
""")
conn.execute("""
INSERT INTO users (name, email) VALUES
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com')
""")
conn.commit()
conn.close()
yield db_path
# Cleanup
Path(db_path).unlink()
def test_database_initialization(self, temp_db):
"""Test database manager initialization."""
db_manager = DatabaseManager(temp_db)
assert db_manager.db_path == temp_db
def test_get_schema(self, temp_db):
"""Test schema extraction."""
db_manager = DatabaseManager(temp_db)
schema = db_manager.get_schema()
assert "users" in schema
assert "id" in schema
assert "name" in schema
assert "email" in schema
def test_get_tables(self, temp_db):
"""Test table listing."""
db_manager = DatabaseManager(temp_db)
tables = db_manager.get_tables()
assert "users" in tables
assert len(tables) == 1
def test_execute_select_query(self, temp_db):
"""Test SELECT query execution."""
db_manager = DatabaseManager(temp_db)
success, results = db_manager.execute_query("SELECT * FROM users")
assert success is True
assert "rows" in results
assert len(results["rows"]) == 2
def test_execute_invalid_query(self, temp_db):
"""Test invalid query handling."""
db_manager = DatabaseManager(temp_db)
success, results = db_manager.execute_query("INVALID SQL")
assert success is False
assert "Error" in results
class TestUtils:
"""Test utility functions."""
def test_validate_sql_valid(self):
"""Test SQL validation with valid queries."""
assert validate_sql("SELECT * FROM users") is True
assert validate_sql("INSERT INTO users VALUES (1, 'test')") is True
assert validate_sql("UPDATE users SET name='test'") is True
def test_validate_sql_invalid(self):
"""Test SQL validation with invalid queries."""
assert validate_sql("") is False
assert validate_sql(" ") is False
assert validate_sql("RANDOM TEXT") is False
def test_truncate_text(self):
"""Test text truncation."""
text = "a" * 150
truncated = truncate_text(text, 100)
assert len(truncated) == 100
assert truncated.endswith("...")
def test_truncate_text_short(self):
"""Test truncation of short text."""
text = "short text"
truncated = truncate_text(text, 100)
assert truncated == text
class TestConfig:
"""Test configuration management."""
def test_config_validation(self):
"""Test config validation."""
from config import Config
# Should raise error without model path
with pytest.raises(ValueError):
Config(model_path=None)
if __name__ == "__main__":
pytest.main([__file__, "-v"])