Skip to content

django-tenants & pytest - Issues with postgres multiple schemas. #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
geekashu opened this issue Aug 12, 2018 · 3 comments
Closed

django-tenants & pytest - Issues with postgres multiple schemas. #633

geekashu opened this issue Aug 12, 2018 · 3 comments

Comments

@geekashu
Copy link

geekashu commented Aug 12, 2018

Hi,

I am trying to use pytest with django-tenants which uses PostgreSQL schemas to isolate clients. How do I configure pytest-django so that if I am using TenantTestCase which actually creates a test schema along with public starts to use both the schema's or set the database search path to 'search_path=test, public'.

It seems pytest is only using the public schema and thus fails to find out the test schema.

The models that I want to test gets created in test schema, isolated from public.

I read in the documentation that pytest-django doesn't play well with multiple databases. Is it true with multiple schema's as well which are in a single database?

The tests are running fine with manage.py test (Django inbuilt testing) and utilizing both the schemas.

Thanks.

@shinto-dev
Copy link

I am facing the same issue. My pytests are using only public schema. Is there any way to overcome this problem?

@akifd
Copy link

akifd commented Sep 26, 2019

This is not pytest-django issue. I personally worked around this by making fixture that creates the tenant and activates it. A simple version would go something like this:

import pytest
from django_tenants.utils import tenant_context


@pytest.fixture
def tenant(db):
    tenant = Tenant(schema_name="test")
    tenant.save(verbosity=0)
    tenant.domains.create(domain="tenant.test.com", read_only=True)

    with tenant_context(tenant):
        yield tenant

    tenant.save()

Then you can create a test like this:

def test_create_user(tenant):
    # Tenant is activated in this test.

@bluetech
Copy link
Member

pytest-django does not support using custom TestCases. You can always use the TenantTestCase itself as unittest test and pytest will pick it up as normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants