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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
import pytest
from django.core import management
@pytest.fixture(autouse=True)
def _setup(tenant1, tenant2, tenant3, TenantModel, DomainModel):
if TenantModel is None:
pytest.skip("Dynamic tenants are not in use")
if DomainModel:
for tenant in [tenant1, tenant2, tenant3]:
DomainModel.objects.create(
tenant=tenant, domain=f"{tenant.schema_name}.localhost", is_primary=True
)
def split_output(buffer):
buffer.seek(0)
return set(buffer.read().strip().splitlines())
def test_all_schemas(DomainModel, stdout):
management.call_command("whowill", all_schemas=True, stdout=stdout)
expected_dynamic = (
{"tenant1.localhost", "tenant2.localhost", "tenant3.localhost"}
if DomainModel
else {"tenant1", "tenant2", "tenant3"}
)
assert (
split_output(stdout)
== {"public", "sample", "localhost", "blog.localhost"} | expected_dynamic
)
def test_static_schemas(stdout):
management.call_command("whowill", static_schemas=True, stdout=stdout)
assert split_output(stdout) == {"public", "sample", "localhost", "blog.localhost"}
def test_tenant_like_schemas(DomainModel, stdout):
management.call_command("whowill", tenant_schemas=True, stdout=stdout)
expected_dynamic = (
{"tenant1.localhost", "tenant2.localhost", "tenant3.localhost"}
if DomainModel
else {"tenant1", "tenant2", "tenant3"}
)
assert split_output(stdout) == {"sample"} | expected_dynamic
def test_dynamic_schemas(DomainModel, stdout):
management.call_command("whowill", dynamic_schemas=True, stdout=stdout)
expected_dynamic = (
{"tenant1.localhost", "tenant2.localhost", "tenant3.localhost"}
if DomainModel
else {"tenant1", "tenant2", "tenant3"}
)
assert split_output(stdout) == expected_dynamic
def test_specific_schemas(DomainModel, stdout):
management.call_command("whowill", schemas=["www", "blog", "tenant1"], stdout=stdout)
expected_dynamic = {"tenant1.localhost"} if DomainModel else {"tenant1"}
assert split_output(stdout) == {"localhost", "blog.localhost"} | expected_dynamic
# Same test cases as before, but excluding one
def test_all_schemas_minus_one(DomainModel, stdout):
management.call_command("whowill", all_schemas=True, excluded_schemas=["blog"], stdout=stdout)
expected_dynamic = (
{
"tenant1.localhost",
"tenant2.localhost",
"tenant3.localhost",
}
if DomainModel
else {"tenant1", "tenant2", "tenant3"}
)
assert split_output(stdout) == {"public", "sample", "localhost"} | expected_dynamic
def test_static_schemas_minus_one(stdout):
management.call_command(
"whowill", static_schemas=True, excluded_schemas=["sample"], stdout=stdout
)
assert split_output(stdout) == {"public", "localhost", "blog.localhost"}
def test_tenant_like_schemas_minus_one(DomainModel, stdout):
management.call_command(
"whowill", tenant_schemas=True, excluded_schemas=["tenant1"], stdout=stdout
)
expected_dynamic = (
{"tenant2.localhost", "tenant3.localhost"} if DomainModel else {"tenant2", "tenant3"}
)
assert split_output(stdout) == {"sample"} | expected_dynamic
def test_dynamic_schemas_minus_one(DomainModel, stdout):
management.call_command(
"whowill", dynamic_schemas=True, excluded_schemas=["public"], stdout=stdout
)
expected_dynamic = (
{
"tenant1.localhost",
"tenant2.localhost",
"tenant3.localhost",
}
if DomainModel
else {
"tenant1",
"tenant2",
"tenant3",
}
)
assert split_output(stdout) == expected_dynamic
def test_specific_schemas_minus_one(DomainModel, stdout):
management.call_command(
"whowill",
schemas=["www", "blog", "tenant1"],
excluded_schemas=["www"],
stdout=stdout,
)
expected_dynamic = {"tenant1.localhost"} if DomainModel else {"tenant1"}
assert split_output(stdout) == {"blog.localhost"} | expected_dynamic
|