| 1 |
"""sqlite3 CLI tests.""" |
| 2 |
import sqlite3 |
| 3 |
import unittest |
| 4 |
|
| 5 |
from sqlite3.__main__ import main as cli |
| 6 |
from test.support.os_helper import TESTFN, unlink |
| 7 |
from test.support import ( |
| 8 |
captured_stdout, |
| 9 |
captured_stderr, |
| 10 |
captured_stdin, |
| 11 |
force_not_colorized, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class CommandLineInterface(unittest.TestCase): |
| 16 |
|
| 17 |
def _do_test(self, *args, expect_success=True): |
| 18 |
with ( |
| 19 |
captured_stdout() as out, |
| 20 |
captured_stderr() as err, |
| 21 |
self.assertRaises(SystemExit) as cm |
| 22 |
): |
| 23 |
cli(args) |
| 24 |
return out.getvalue(), err.getvalue(), cm.exception.code |
| 25 |
|
| 26 |
def expect_success(self, *args): |
| 27 |
out, err, code = self._do_test(*args) |
| 28 |
self.assertEqual(code, 0, |
| 29 |
"\n".join([f"Unexpected failure: {args=}", out, err])) |
| 30 |
self.assertEqual(err, "") |
| 31 |
return out |
| 32 |
|
| 33 |
def expect_failure(self, *args): |
| 34 |
out, err, code = self._do_test(*args, expect_success=False) |
| 35 |
self.assertNotEqual(code, 0, |
| 36 |
"\n".join([f"Unexpected failure: {args=}", out, err])) |
| 37 |
self.assertEqual(out, "") |
| 38 |
return err |
| 39 |
|
| 40 |
@force_not_colorized |
| 41 |
def test_cli_help(self): |
| 42 |
out = self.expect_success("-h") |
| 43 |
self.assertIn("usage: ", out) |
| 44 |
self.assertIn(" [-h] [-v] [filename] [sql]", out) |
| 45 |
self.assertIn("Python sqlite3 CLI", out) |
| 46 |
|
| 47 |
def test_cli_version(self): |
| 48 |
out = self.expect_success("-v") |
| 49 |
self.assertIn(sqlite3.sqlite_version, out) |
| 50 |
|
| 51 |
def test_cli_execute_sql(self): |
| 52 |
out = self.expect_success(":memory:", "select 1") |
| 53 |
self.assertIn("(1,)", out) |
| 54 |
|
| 55 |
def test_cli_execute_too_much_sql(self): |
| 56 |
stderr = self.expect_failure(":memory:", "select 1; select 2") |
| 57 |
err = "ProgrammingError: You can only execute one statement at a time" |
| 58 |
self.assertIn(err, stderr) |
| 59 |
|
| 60 |
def test_cli_execute_incomplete_sql(self): |
| 61 |
stderr = self.expect_failure(":memory:", "sel") |
| 62 |
self.assertIn("OperationalError (SQLITE_ERROR)", stderr) |
| 63 |
|
| 64 |
def test_cli_on_disk_db(self): |
| 65 |
self.addCleanup(unlink, TESTFN) |
| 66 |
out = self.expect_success(TESTFN, "create table t(t)") |
| 67 |
self.assertEqual(out, "") |
| 68 |
out = self.expect_success(TESTFN, "select count(t) from t") |
| 69 |
self.assertIn("(0,)", out) |
| 70 |
|
| 71 |
|
| 72 |
class InteractiveSession(unittest.TestCase): |
| 73 |
MEMORY_DB_MSG = "Connected to a transient in-memory database" |
| 74 |
PS1 = "sqlite> " |
| 75 |
PS2 = "... " |
| 76 |
|
| 77 |
def run_cli(self, *args, commands=()): |
| 78 |
with ( |
| 79 |
captured_stdin() as stdin, |
| 80 |
captured_stdout() as stdout, |
| 81 |
captured_stderr() as stderr, |
| 82 |
self.assertRaises(SystemExit) as cm |
| 83 |
): |
| 84 |
for cmd in commands: |
| 85 |
stdin.write(cmd + "\n") |
| 86 |
stdin.seek(0) |
| 87 |
cli(args) |
| 88 |
|
| 89 |
out = stdout.getvalue() |
| 90 |
err = stderr.getvalue() |
| 91 |
self.assertEqual(cm.exception.code, 0, |
| 92 |
f"Unexpected failure: {args=}\n{out}\n{err}") |
| 93 |
return out, err |
| 94 |
|
| 95 |
def test_interact(self): |
| 96 |
out, err = self.run_cli() |
| 97 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 98 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 99 |
self.assertEndsWith(out, self.PS1) |
| 100 |
self.assertEqual(out.count(self.PS1), 1) |
| 101 |
self.assertEqual(out.count(self.PS2), 0) |
| 102 |
|
| 103 |
def test_interact_quit(self): |
| 104 |
out, err = self.run_cli(commands=(".quit",)) |
| 105 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 106 |
self.assertEndsWith(out, self.PS1) |
| 107 |
self.assertEqual(out.count(self.PS1), 1) |
| 108 |
self.assertEqual(out.count(self.PS2), 0) |
| 109 |
|
| 110 |
def test_interact_version(self): |
| 111 |
out, err = self.run_cli(commands=(".version",)) |
| 112 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 113 |
self.assertIn(sqlite3.sqlite_version + "\n", out) |
| 114 |
self.assertEndsWith(out, self.PS1) |
| 115 |
self.assertEqual(out.count(self.PS1), 2) |
| 116 |
self.assertEqual(out.count(self.PS2), 0) |
| 117 |
self.assertIn(sqlite3.sqlite_version, out) |
| 118 |
|
| 119 |
def test_interact_empty_source(self): |
| 120 |
out, err = self.run_cli(commands=("", " ")) |
| 121 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 122 |
self.assertEndsWith(out, self.PS1) |
| 123 |
self.assertEqual(out.count(self.PS1), 3) |
| 124 |
self.assertEqual(out.count(self.PS2), 0) |
| 125 |
|
| 126 |
def test_interact_dot_commands_unknown(self): |
| 127 |
out, err = self.run_cli(commands=(".unknown_command", )) |
| 128 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 129 |
self.assertEndsWith(out, self.PS1) |
| 130 |
self.assertEqual(out.count(self.PS1), 2) |
| 131 |
self.assertEqual(out.count(self.PS2), 0) |
| 132 |
self.assertIn("Error", err) |
| 133 |
# test "unknown_command" is pointed out in the error message |
| 134 |
self.assertIn("unknown_command", err) |
| 135 |
|
| 136 |
def test_interact_dot_commands_empty(self): |
| 137 |
out, err = self.run_cli(commands=(".")) |
| 138 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 139 |
self.assertEndsWith(out, self.PS1) |
| 140 |
self.assertEqual(out.count(self.PS1), 2) |
| 141 |
self.assertEqual(out.count(self.PS2), 0) |
| 142 |
|
| 143 |
def test_interact_dot_commands_with_whitespaces(self): |
| 144 |
out, err = self.run_cli(commands=(".version ", ". version")) |
| 145 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 146 |
self.assertEqual(out.count(sqlite3.sqlite_version + "\n"), 2) |
| 147 |
self.assertEndsWith(out, self.PS1) |
| 148 |
self.assertEqual(out.count(self.PS1), 3) |
| 149 |
self.assertEqual(out.count(self.PS2), 0) |
| 150 |
|
| 151 |
def test_interact_valid_sql(self): |
| 152 |
out, err = self.run_cli(commands=("SELECT 1;",)) |
| 153 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 154 |
self.assertIn("(1,)\n", out) |
| 155 |
self.assertEndsWith(out, self.PS1) |
| 156 |
self.assertEqual(out.count(self.PS1), 2) |
| 157 |
self.assertEqual(out.count(self.PS2), 0) |
| 158 |
|
| 159 |
def test_interact_incomplete_multiline_sql(self): |
| 160 |
out, err = self.run_cli(commands=("SELECT 1",)) |
| 161 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 162 |
self.assertEndsWith(out, self.PS2) |
| 163 |
self.assertEqual(out.count(self.PS1), 1) |
| 164 |
self.assertEqual(out.count(self.PS2), 1) |
| 165 |
|
| 166 |
def test_interact_valid_multiline_sql(self): |
| 167 |
out, err = self.run_cli(commands=("SELECT 1\n;",)) |
| 168 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 169 |
self.assertIn(self.PS2, out) |
| 170 |
self.assertIn("(1,)\n", out) |
| 171 |
self.assertEndsWith(out, self.PS1) |
| 172 |
self.assertEqual(out.count(self.PS1), 2) |
| 173 |
self.assertEqual(out.count(self.PS2), 1) |
| 174 |
|
| 175 |
def test_interact_invalid_sql(self): |
| 176 |
out, err = self.run_cli(commands=("sel;",)) |
| 177 |
self.assertIn(self.MEMORY_DB_MSG, err) |
| 178 |
self.assertIn("OperationalError (SQLITE_ERROR)", err) |
| 179 |
self.assertEndsWith(out, self.PS1) |
| 180 |
self.assertEqual(out.count(self.PS1), 2) |
| 181 |
self.assertEqual(out.count(self.PS2), 0) |
| 182 |
|
| 183 |
def test_interact_on_disk_file(self): |
| 184 |
self.addCleanup(unlink, TESTFN) |
| 185 |
|
| 186 |
out, err = self.run_cli(TESTFN, commands=("CREATE TABLE t(t);",)) |
| 187 |
self.assertIn(TESTFN, err) |
| 188 |
self.assertEndsWith(out, self.PS1) |
| 189 |
|
| 190 |
out, _ = self.run_cli(TESTFN, commands=("SELECT count(t) FROM t;",)) |
| 191 |
self.assertIn("(0,)\n", out) |
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__": |
| 195 |
unittest.main() |