| 1 |
import subprocess |
| 2 |
from pathlib import Path |
| 3 |
from unittest import mock, skipUnless |
| 4 |
|
| 5 |
from django.core.management import CommandError, call_command |
| 6 |
from django.db import connection |
| 7 |
from django.db.backends.sqlite3.client import DatabaseClient |
| 8 |
from django.test import SimpleTestCase |
| 9 |
|
| 10 |
|
| 11 |
class SqliteDbshellCommandTestCase(SimpleTestCase): |
| 12 |
def settings_to_cmd_args_env(self, settings_dict, parameters=None): |
| 13 |
if parameters is None: |
| 14 |
parameters = [] |
| 15 |
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters) |
| 16 |
|
| 17 |
def test_path_name(self): |
| 18 |
self.assertEqual( |
| 19 |
self.settings_to_cmd_args_env({"NAME": Path("test.db.sqlite3")}), |
| 20 |
(["sqlite3", Path("test.db.sqlite3")], None), |
| 21 |
) |
| 22 |
|
| 23 |
def test_parameters(self): |
| 24 |
self.assertEqual( |
| 25 |
self.settings_to_cmd_args_env({"NAME": "test.db.sqlite3"}, ["-help"]), |
| 26 |
(["sqlite3", "test.db.sqlite3", "-help"], None), |
| 27 |
) |
| 28 |
|
| 29 |
@skipUnless(connection.vendor == "sqlite", "SQLite test") |
| 30 |
def test_non_zero_exit_status_when_path_to_db_is_path(self): |
| 31 |
sqlite_with_path = { |
| 32 |
"ENGINE": "django.db.backends.sqlite3", |
| 33 |
"NAME": Path("test.db.sqlite3"), |
| 34 |
} |
| 35 |
cmd_args = self.settings_to_cmd_args_env(sqlite_with_path)[0] |
| 36 |
|
| 37 |
msg = '"sqlite3 test.db.sqlite3" returned non-zero exit status 1.' |
| 38 |
with ( |
| 39 |
mock.patch( |
| 40 |
"django.db.backends.sqlite3.client.DatabaseClient.runshell", |
| 41 |
side_effect=subprocess.CalledProcessError(returncode=1, cmd=cmd_args), |
| 42 |
), |
| 43 |
self.assertRaisesMessage(CommandError, msg), |
| 44 |
): |
| 45 |
call_command("dbshell") |