tests: add test to tenant management commands (#40098)
This commit is contained in:
parent
2825b4ddbd
commit
df22a989c9
|
@ -3,10 +3,14 @@
|
|||
import pytest
|
||||
import mock
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django.core.management import BaseCommand, call_command, load_command_class
|
||||
from django.utils.encoding import force_bytes
|
||||
from django.utils import six
|
||||
from django.core.management.base import CommandError
|
||||
|
||||
from hobo.multitenant.models import Tenant
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
@ -20,6 +24,13 @@ class RecordTenant(object):
|
|||
self.tenants.append(connection.tenant)
|
||||
|
||||
|
||||
def get_schemas():
|
||||
from django.db import connection
|
||||
cursor = connection.cursor()
|
||||
cursor.execute('select schema_name from information_schema.schemata')
|
||||
return [x[0] for x in cursor.fetchall()]
|
||||
|
||||
|
||||
@mock.patch('django.contrib.sessions.management.commands.clearsessions.Command.handle')
|
||||
def test_all_tenants(handle, tenants):
|
||||
from django.core.management import execute_from_command_line
|
||||
|
@ -48,12 +59,6 @@ def test_delete_tenant(tenants):
|
|||
if any('removed' in d for d in os.listdir(base)):
|
||||
assert False
|
||||
|
||||
def get_schemas():
|
||||
from django.db import connection
|
||||
cursor = connection.cursor()
|
||||
cursor.execute('select schema_name from information_schema.schemata')
|
||||
return [x[0] for x in cursor.fetchall()]
|
||||
|
||||
if any('removed' in x for x in get_schemas()):
|
||||
assert False
|
||||
all_tenants = list(TenantMiddleware.get_tenants())
|
||||
|
@ -134,3 +139,33 @@ def test_tenant_command_all_tenants_errors(tenants, monkeypatch, capsys):
|
|||
|
||||
captured = capsys.readouterr()
|
||||
assert 'Unrepresentable exception' in captured.err
|
||||
|
||||
|
||||
def test_list_tenants_command(db, tenants, capsys):
|
||||
call_command('list_tenants')
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out.split('\n') == [
|
||||
'tenant1_example_net tenant1.example.net',
|
||||
'tenant2_example_net tenant2.example.net',
|
||||
''
|
||||
]
|
||||
|
||||
|
||||
def test_list_tenants_command_empty(db, capsys):
|
||||
call_command('list_tenants')
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == ''
|
||||
|
||||
|
||||
@mock.patch('hobo.multitenant.management.commands.create_schemas.TenantMiddleware.get_tenants')
|
||||
def test_create_schema_command(mocked_get_tenants):
|
||||
tenant = Tenant(domain_url='my-tenant-url', schema_name='my_schema_name')
|
||||
mocked_get_tenants.return_value = [tenant]
|
||||
nb_schemas = len(get_schemas())
|
||||
call_command('create_schemas')
|
||||
assert 'my_schema_name' in get_schemas()[nb_schemas]
|
||||
|
||||
|
||||
def test_shell_command_empty():
|
||||
with pytest.raises(CommandError, match="There are no tenants in the system."):
|
||||
call_command('shell')
|
||||
|
|
Loading…
Reference in New Issue