-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repl.py
More file actions
201 lines (154 loc) · 7.08 KB
/
test_repl.py
File metadata and controls
201 lines (154 loc) · 7.08 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Unit tests for pysqlit/repl.py module."""
import pytest
from unittest.mock import patch, MagicMock
from io import StringIO
from pysqlit.repl import EnhancedREPL
class TestEnhancedREPL:
"""Test cases for EnhancedREPL class."""
def test_repl_creation(self, temp_db_path):
"""Test REPL creation."""
repl = EnhancedREPL(temp_db_path)
assert repl.database.filename == temp_db_path
repl.close()
def test_repl_close(self, temp_db_path):
"""Test REPL close."""
repl = EnhancedREPL(temp_db_path)
repl.close()
# Should not raise exception
def test_repl_run_with_exit(self, temp_db_path):
"""Test REPL run with exit command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.exit']):
repl.run()
repl.close()
def test_repl_help_command(self, temp_db_path):
"""Test REPL help command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.help', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
assert "help" in output.lower()
repl.close()
def test_repl_tables_command(self, temp_db_path):
"""Test REPL tables command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.tables', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
assert "users" in output
repl.close()
def test_repl_database_info_command(self, temp_db_path):
"""Test REPL database info command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.dbinfo', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
assert "Database" in output
repl.close()
def test_repl_invalid_command(self, temp_db_path):
"""Test REPL invalid command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.invalid', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
assert "Unrecognized" in output or "invalid" in output.lower()
repl.close()
def test_repl_sql_insert(self, temp_db_path):
"""Test REPL SQL INSERT command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=[
"INSERT INTO users (id, username, email) VALUES (1, 'Alice', 'alice@example.com')",
".exit"
]):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle the SQL command
repl.close()
def test_repl_sql_select(self, temp_db_path):
"""Test REPL SQL SELECT command."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=[
"SELECT * FROM users",
".exit"
]):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle the SQL command
repl.close()
def test_repl_empty_input(self, temp_db_path):
"""Test REPL empty input."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle empty input gracefully
repl.close()
def test_repl_whitespace_input(self, temp_db_path):
"""Test REPL whitespace input."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=[' ', '.exit']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle whitespace input gracefully
repl.close()
def test_repl_meta_command_case_insensitive(self, temp_db_path):
"""Test REPL meta command case insensitivity."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=['.HELP', '.EXIT']):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
assert "help" in output.lower()
repl.close()
def test_repl_sql_syntax_error(self, temp_db_path):
"""Test REPL SQL syntax error handling."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=[
"INVALID SQL SYNTAX",
".exit"
]):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle syntax error gracefully
repl.close()
def test_repl_keyboard_interrupt(self, temp_db_path):
"""Test REPL keyboard interrupt handling."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=KeyboardInterrupt):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Should handle keyboard interrupt
repl.close()
def test_repl_eof_handling(self, temp_db_path):
"""Test REPL EOF handling."""
repl = EnhancedREPL(temp_db_path)
with patch('builtins.input', side_effect=EOFError):
repl.run()
def test_repl_alias_column_display(self, temp_db_path):
"""Test REPL correctly displays column aliases."""
repl = EnhancedREPL(temp_db_path)
# Create table and insert data
with patch('builtins.input', side_effect=[
"CREATE TABLE animal (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",
"INSERT INTO animal (id, name, age) VALUES (100, 'Tom', 20)",
"SELECT name, age as nianling FROM animal",
".exit"
]):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
repl.run()
output = mock_stdout.getvalue()
# Verify column headers
assert "name | nianling" in output
# Verify data row
assert "Tom | 20" in output
repl.close()