2019-08-23 16:53:36 +08:00
|
|
|
from subprocess import CompletedProcess
|
2018-10-26 07:39:42 +08:00
|
|
|
from unittest import mock, skipUnless
|
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
from django.db.backends.oracle.client import DatabaseClient
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(connection.vendor == 'oracle', 'Oracle tests')
|
|
|
|
class OracleDbshellTests(SimpleTestCase):
|
2020-04-14 15:56:40 +08:00
|
|
|
def _run_dbshell(self, rlwrap=False, parameters=None):
|
2018-10-26 07:39:42 +08:00
|
|
|
"""Run runshell command and capture its arguments."""
|
2019-08-24 03:25:21 +08:00
|
|
|
def _mock_subprocess_run(*args, **kwargs):
|
2019-08-23 16:53:36 +08:00
|
|
|
self.subprocess_args = list(*args)
|
|
|
|
return CompletedProcess(self.subprocess_args, 0)
|
2018-10-26 07:39:42 +08:00
|
|
|
|
2020-04-14 15:56:40 +08:00
|
|
|
if parameters is None:
|
|
|
|
parameters = []
|
2018-10-26 07:39:42 +08:00
|
|
|
client = DatabaseClient(connection)
|
|
|
|
self.subprocess_args = None
|
2019-08-23 16:53:36 +08:00
|
|
|
with mock.patch('subprocess.run', new=_mock_subprocess_run):
|
2018-10-26 07:39:42 +08:00
|
|
|
with mock.patch('shutil.which', return_value='/usr/bin/rlwrap' if rlwrap else None):
|
2020-04-14 15:56:40 +08:00
|
|
|
client.runshell(parameters)
|
2018-10-26 07:39:42 +08:00
|
|
|
return self.subprocess_args
|
|
|
|
|
|
|
|
def test_without_rlwrap(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_dbshell(rlwrap=False),
|
2019-08-24 03:25:21 +08:00
|
|
|
['sqlplus', '-L', connection._connect_string()],
|
2018-10-26 07:39:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_with_rlwrap(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_dbshell(rlwrap=True),
|
2019-08-24 03:25:21 +08:00
|
|
|
['/usr/bin/rlwrap', 'sqlplus', '-L', connection._connect_string()],
|
2018-10-26 07:39:42 +08:00
|
|
|
)
|
2020-04-14 15:56:40 +08:00
|
|
|
|
|
|
|
def test_parameters(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_dbshell(parameters=['-HELP']),
|
|
|
|
['sqlplus', '-L', connection._connect_string(), '-HELP'],
|
|
|
|
)
|