bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.46k stars 424 forks source link

Testing status response of django-rest-framework fails #229

Open m4l opened 9 years ago

m4l commented 9 years ago

I'm struggling to get all my test cases running again after switching to django-tenant-schemas. I'm heavily using djangorestframework v2.4.4 for API views. Using TenantClient I get a full HTML response with a 200 status code where I expect an empty 204.

Here my minimum example:

    from rest_framework.views import APIView
    from rest_framework.response import Response

    class Test(APIView):
        def get(self, request):
            return Response(status=status.HTTP_204_NO_CONTENT)

    from rest_framework.test import APIClient, APITestCase
    from tenant_schemas.test.cases import TenantTestCase
    from tenant_schemas.test.client import TenantClient

    class APITenantClient(TenantClient, APIClient):
        ''' Mixin of Tenant and API specific client logic '''

    class APITestCase(TenantTestCase, APITestCase):
        def setUp(self):
            super(APITestCase, self).setUp()
            self.client = APITenantClient(self.tenant)

        def test_test(self):
            r = self.client.get(reverse('api:test'), format='json')
            self.assertTrue("HTTP 204 NO CONTENT" in r.content)
            self.assertEqual(r.status_code, 204)  # fail, is 200 
robert-malai commented 9 years ago

I've managed to couple DRF with Tenant Schemas by adapting intial classes of TenantClient and TenantTestCase to mixins, which will be combined with APIClient and APITenantClient.

Not sure if relevant, but I have override the initial definition of migrate to forward the call to migrate_schemas.

I will do a pull reqest to introduce the modification to this original repository.

robert-malai commented 9 years ago

Everybody interested, have a look over this patch: https://github.com/bernardopires/django-tenant-schemas/pull/251

bartmika commented 8 years ago

We switched to django-tenant-schema and encountered similar problems integrating Django-REST-Framework.

+1 for better integration requested.