Django DRF with django-tenants Unit Test Issue

115 Views Asked by At

Description:

I am working on a Django project using Django Rest Framework (DRF) and django-tenants for multi-tenancy. In my unit tests, I'm encountering an error that I need help resolving.

Issue:

When running a unit test to create a restaurant using the TenantTestCase, I am getting the following error:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).

F
======================================================================
FAIL: test_create_valid_restaurant (django_apis.restaurants.tests.RestaurantCreateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\OK\BackendProjects\apis\django_apis\restaurants\tests.py", line 55, in test_create_valid_restaurant
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
AssertionError: 404 != 201

----------------------------------------------------------------------
Ran 1 test in 4.596s

FAILED (failures=1)
Destroying test database for alias 'default'...

Code:

Here's the relevant code for the unit test:

class RestaurantCreateTest(TenantTestCase):
    tenant = None  # Define a class attribute to store the tenant instance

    @classmethod
    def setup_tenant(cls, tenant):
        tenant.name = "kangaroo"
        tenant.subdomain_prefix = "kangaroo"

        # Store the tenant instance as a class attribute
        cls.tenant = tenant

        return tenant

    def setUp(self):
        super().setUp()
        self.c = TenantClient(self.tenant)

    # Override the get_test_tenant_domain method to specify a custom test domain
    @staticmethod
    def get_test_tenant_domain():
        if RestaurantCreateTest.tenant:
            domain = f"{RestaurantCreateTest.tenant.subdomain_prefix + settings.ORGANIZATION_DOMAIN}"
            return domain
        else:
            return "tenant.test.com"  # Default custom test domain if tenant is not set

    def test_create_valid_restaurant(self):
        url = reverse("Restaurant-list")
        # url = "http://localhost:8000/restaurants/"
        data = {
            "name": "Sample Restaurant",
            "address": "123 Main Street",
            "phone_number": "555-123-4567",
            "email": "[email protected]",
            "cuisine": "Italian",
            "rating": 4.5,
        }
        response = self.c.post(url, data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # Check if the restaurant object was created in the database
        restaurant = Restaurant.objects.get(name="Sample Restaurant")
        self.assertIsNotNone(restaurant)

        # Check if the response data matches the serialized restaurant
        serializer = RestaurantSerializer(restaurant)
        self.assertEqual(response.data, serializer.data)

Additional Information:

I'm using django-tenants for multi-tenancy. The error suggests a 404 status code instead of the expected 201 Created status when creating a restaurant. I've verified my URL configuration, view names, and tenant setup, but the issue persists.

Request:

I'm looking for guidance on how to resolve this error and successfully create a restaurant in my unit test using django-tenants with DRF. Any insights or suggestions would be greatly appreciated. Thank you!

0

There are 0 best solutions below