0001-general-add-a-dbshell-management-command-52235.patch
tests/test_ctl.py | ||
---|---|---|
2 | 2 |
import os |
3 | 3 |
import sys |
4 | 4 | |
5 |
import mock |
|
5 | 6 |
import psycopg2 |
6 | 7 |
import pytest |
7 | 8 |
from django.core.management import CommandError, call_command |
... | ... | |
26 | 27 | |
27 | 28 |
@pytest.fixture |
28 | 29 |
def pub(): |
29 |
return create_temporary_pub() |
|
30 |
yield create_temporary_pub() |
|
31 |
clean_temporary_pub() |
|
32 | ||
33 | ||
34 |
@pytest.fixture |
|
35 |
def sql_pub(): |
|
36 |
yield create_temporary_pub(sql_mode=True) |
|
37 |
clean_temporary_pub() |
|
30 | 38 | |
31 | 39 | |
32 | 40 |
def pytest_generate_tests(metafunc): |
... | ... | |
477 | 485 |
assert 'error: You must use a command' in captured.err |
478 | 486 |
finally: |
479 | 487 |
sys.argv = old_argv |
488 | ||
489 | ||
490 |
def test_dbshell(sql_pub): |
|
491 | ||
492 |
with pytest.raises(CommandError): |
|
493 |
call_command('dbshell') # missing tenant name |
|
494 | ||
495 |
with mock.patch('subprocess.call') as call: |
|
496 |
call.side_effect = lambda *args: 0 |
|
497 |
call_command('dbshell', '--domain', 'example.net') |
|
498 |
assert call.call_args[0][-1][0] == 'psql' |
|
499 |
assert call.call_args[0][-1][-1] == sql_pub.cfg['postgresql']['database'] |
wcs/ctl/management/commands/dbshell.py | ||
---|---|---|
1 |
# w.c.s. - web application for online forms |
|
2 |
# Copyright (C) 2005-2021 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software; you can redistribute it and/or modify |
|
5 |
# it under the terms of the GNU General Public License as published by |
|
6 |
# the Free Software Foundation; either version 2 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU General Public License |
|
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.db.backends.postgresql.client import DatabaseClient |
|
18 | ||
19 |
from . import TenantCommand |
|
20 | ||
21 | ||
22 |
class Command(TenantCommand): |
|
23 |
def add_arguments(self, parser): |
|
24 |
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN') |
|
25 | ||
26 |
def handle(self, *args, **options): |
|
27 |
pub = self.init_tenant_publisher(options['domain'], register_tld_names=False) |
|
28 |
DatabaseClient.runshell_db(conn_params=pub.cfg['postgresql']) |
|
0 |
- |