2023-05-03 12:24:45 +00:00
|
|
|
import subprocess
|
2019-12-14 23:13:33 +00:00
|
|
|
from pathlib import Path
|
2023-05-03 12:24:45 +00:00
|
|
|
from unittest import mock, skipUnless
|
2019-12-14 23:13:33 +00:00
|
|
|
|
2023-05-03 12:24:45 +00:00
|
|
|
from django.core.management import CommandError, call_command
|
|
|
|
from django.db import connection
|
2019-12-14 23:13:33 +00:00
|
|
|
from django.db.backends.sqlite3.client import DatabaseClient
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class SqliteDbshellCommandTestCase(SimpleTestCase):
|
2020-10-04 22:25:29 +00:00
|
|
|
def settings_to_cmd_args_env(self, settings_dict, parameters=None):
|
2020-04-14 07:56:40 +00:00
|
|
|
if parameters is None:
|
|
|
|
parameters = []
|
2020-10-04 22:25:29 +00:00
|
|
|
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
|
2019-12-14 23:13:33 +00:00
|
|
|
|
|
|
|
def test_path_name(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env({"NAME": Path("test.db.sqlite3")}),
|
|
|
|
(["sqlite3", Path("test.db.sqlite3")], None),
|
2020-10-04 22:25:29 +00:00
|
|
|
)
|
2020-04-14 07:56:40 +00:00
|
|
|
|
|
|
|
def test_parameters(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env({"NAME": "test.db.sqlite3"}, ["-help"]),
|
|
|
|
(["sqlite3", "test.db.sqlite3", "-help"], None),
|
2020-10-04 22:25:29 +00:00
|
|
|
)
|
2023-05-03 12:24:45 +00:00
|
|
|
|
|
|
|
@skipUnless(connection.vendor == "sqlite", "SQLite test")
|
|
|
|
def test_non_zero_exit_status_when_path_to_db_is_path(self):
|
|
|
|
sqlite_with_path = {
|
|
|
|
"ENGINE": "django.db.backends.sqlite3",
|
|
|
|
"NAME": Path("test.db.sqlite3"),
|
|
|
|
}
|
|
|
|
cmd_args = self.settings_to_cmd_args_env(sqlite_with_path)[0]
|
|
|
|
|
|
|
|
msg = '"sqlite3 test.db.sqlite3" returned non-zero exit status 1.'
|
2024-01-26 11:45:07 +00:00
|
|
|
with (
|
|
|
|
mock.patch(
|
|
|
|
"django.db.backends.sqlite3.client.DatabaseClient.runshell",
|
|
|
|
side_effect=subprocess.CalledProcessError(returncode=1, cmd=cmd_args),
|
|
|
|
),
|
|
|
|
self.assertRaisesMessage(CommandError, msg),
|
|
|
|
):
|
2023-05-03 12:24:45 +00:00
|
|
|
call_command("dbshell")
|