mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Refs #32061 -- Unified DatabaseClient.runshell() in db backends.
This commit is contained in:
parent
4ac2d4fa42
commit
bbe6fbb876
16 changed files with 272 additions and 206 deletions
|
@ -1,4 +1,3 @@
|
|||
from subprocess import CompletedProcess
|
||||
from unittest import mock, skipUnless
|
||||
|
||||
from django.db import connection
|
||||
|
@ -6,37 +5,48 @@ from django.db.backends.oracle.client import DatabaseClient
|
|||
from django.test import SimpleTestCase
|
||||
|
||||
|
||||
@skipUnless(connection.vendor == 'oracle', 'Oracle tests')
|
||||
@skipUnless(connection.vendor == 'oracle', 'Requires cx_Oracle to be installed')
|
||||
class OracleDbshellTests(SimpleTestCase):
|
||||
def _run_dbshell(self, rlwrap=False, parameters=None):
|
||||
"""Run runshell command and capture its arguments."""
|
||||
def _mock_subprocess_run(*args, **kwargs):
|
||||
self.subprocess_args = list(*args)
|
||||
return CompletedProcess(self.subprocess_args, 0)
|
||||
|
||||
def settings_to_cmd_args_env(self, settings_dict, parameters=None, rlwrap=False):
|
||||
if parameters is None:
|
||||
parameters = []
|
||||
client = DatabaseClient(connection)
|
||||
self.subprocess_args = None
|
||||
with mock.patch('subprocess.run', new=_mock_subprocess_run):
|
||||
with mock.patch('shutil.which', return_value='/usr/bin/rlwrap' if rlwrap else None):
|
||||
client.runshell(parameters)
|
||||
return self.subprocess_args
|
||||
with mock.patch('shutil.which', return_value='/usr/bin/rlwrap' if rlwrap else None):
|
||||
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
|
||||
|
||||
def test_without_rlwrap(self):
|
||||
expected_args = [
|
||||
'sqlplus',
|
||||
'-L',
|
||||
connection.client.connect_string(connection.settings_dict),
|
||||
]
|
||||
self.assertEqual(
|
||||
self._run_dbshell(rlwrap=False),
|
||||
['sqlplus', '-L', connection._connect_string()],
|
||||
self.settings_to_cmd_args_env(connection.settings_dict, rlwrap=False),
|
||||
(expected_args, None),
|
||||
)
|
||||
|
||||
def test_with_rlwrap(self):
|
||||
expected_args = [
|
||||
'/usr/bin/rlwrap',
|
||||
'sqlplus',
|
||||
'-L',
|
||||
connection.client.connect_string(connection.settings_dict),
|
||||
]
|
||||
self.assertEqual(
|
||||
self._run_dbshell(rlwrap=True),
|
||||
['/usr/bin/rlwrap', 'sqlplus', '-L', connection._connect_string()],
|
||||
self.settings_to_cmd_args_env(connection.settings_dict, rlwrap=True),
|
||||
(expected_args, None),
|
||||
)
|
||||
|
||||
def test_parameters(self):
|
||||
expected_args = [
|
||||
'sqlplus',
|
||||
'-L',
|
||||
connection.client.connect_string(connection.settings_dict),
|
||||
'-HELP',
|
||||
]
|
||||
self.assertEqual(
|
||||
self._run_dbshell(parameters=['-HELP']),
|
||||
['sqlplus', '-L', connection._connect_string(), '-HELP'],
|
||||
self.settings_to_cmd_args_env(
|
||||
connection.settings_dict,
|
||||
parameters=['-HELP'],
|
||||
),
|
||||
(expected_args, None),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue