summaryrefslogtreecommitdiff
path: root/tests/dbshell/test_sqlite.py
blob: c3b2b1e28d7eaa1b247233e60fe6018c0318c030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from pathlib import Path
from subprocess import CompletedProcess
from unittest import mock, skipUnless

from django.db import connection
from django.db.backends.sqlite3.client import DatabaseClient
from django.test import SimpleTestCase


@skipUnless(connection.vendor == 'sqlite', 'SQLite tests.')
class SqliteDbshellCommandTestCase(SimpleTestCase):
    def _run_dbshell(self, 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)

        if parameters is None:
            parameters = []
        client = DatabaseClient(connection)
        with mock.patch('subprocess.run', new=_mock_subprocess_run):
            client.runshell(parameters)
        return self.subprocess_args

    def test_path_name(self):
        with mock.patch.dict(
            connection.settings_dict,
            {'NAME': Path('test.db.sqlite3')},
        ):
            self.assertEqual(
                self._run_dbshell(),
                ['sqlite3', 'test.db.sqlite3'],
            )

    def test_parameters(self):
        with mock.patch.dict(
            connection.settings_dict,
            {'NAME': Path('test.db.sqlite3')},
        ):
            self.assertEqual(
                self._run_dbshell(['-help']),
                ['sqlite3', 'test.db.sqlite3', '-help'],
            )