Chitrank Dixit
Chitrank Dixit

Reputation: 4051

Test case for GET api for APIView not working in djangorestframework

I am writing a test case in Django on GET API. I just want to make the very first test pass. Here is my code.

class InventoryItemDetailTestCase(APITestCase):
    """
        This is the API test case for the reserve/express detail api
    """
    def setUp(self):
        self.resource_uri = '/api/v2/inventory/inventory_item_detail/1233/'

    def test_inventory_item_detail_route(self):
        """
            Test to set routing for item_detail
        """
        route = resolve('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(route.func.__name__, 'InventoryItemDetails')

    def test_inventory_item_detail_data(self):
        """
            Test case to check the response json of the reserve/express API
        """
        response = self.client.get('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(response.status_code, 200)

Here I am using client.get to make request. But It gives me error as

Creating test database for alias 'default'...

F

FAIL: test_inventory_item_detail_data (inventory.tests.functional_tests.InventoryItemDetailTestCase)

Traceback (most recent call last): File "/Users/chitrankdixit/Documents/work/flyrobe/flyrobe-django/project/inventory/tests/functional_tests.py", line 131, in >test_inventory_item_detail_data self.assertEqual(response.status_code, 200) AssertionError: 400 != 200


Ran 1 test in 0.135s

FAILED (failures=1)

I tried to use pdb.set_trace() to figure out what error is coming and I have figured I when I run

self.client.get('/api/v2/inventory/inventory_item_detail/1233/')

I get this error

*** KeyError: 'content-type'

I have tried supplying an extra argument name content_type like

self.client.get('/api/v2/inventory/inventory_item_detail/1233/', content_type='application/json')

but still I get the same error. I am able to run the APIs separately and my API is fetching proper responses.If someone has gone through this before please let me know.

Upvotes: 2

Views: 1459

Answers (2)

Chitrank Dixit
Chitrank Dixit

Reputation: 4051

I resolved the issue with the test, I was not making the instances of the required models that are used in the test case, So I just need to update the setUp() method now my setup method look something like this.

def setUp(self):
        self.warehouse = Warehouse.objects.create(
            location_name = 'Mumbai',
            address = 'D1/D2 tex center Chandivali',
        )
        self.supplier = Supplier.objects.create(
            name = "John Doe",
            website = "[email protected]",
            address = "USA, San francisco , CA"
        )
        self.procurement_detail = ProcurementDetail.objects.create(
            invoice_name = "Indore Mill",
            invoice_number = "14235",
            details = "Cloth from Central India",
            total_cost_price = "0.00",
            payment_method = "CASH",
            supplier_id = self.supplier.id,
            is_paid = "True"
        )
        self.inventory_category = InventoryCategory.objects.create(
            name = "party time"
        )
        self.color = Color.objects.create(
            name = "Green",
            hex_val = "#007089"
        )
        self.occasion = Occasion.objects.create(
            name = "BDay"
        )
        self.photo_shoot = PhotoShoot.objects.create(
            name = 'front 1',
            details = 'This is the front facing image',
            model_size = {'name':'abcd'}
        )
        self.brand = Brand.objects.create(
            name = "ZARA1"
        )
        self.parent_item_description = ParentItemDescription.objects.create(
            features = 'Indian clothing at its best',
            style_tip = 'This is style tip',
            fitting_notes = 'This is fitting notes',
            long_description = 'This is the Long description'
        )
        self.parent_item = ParentItem.objects.create(
            parent_id = "1003",
            mrp = "0.00",
            photo_shoot_id = self.photo_shoot.id,
            description_id = self.parent_item_description.id,
            cost_price = "0.00",
            rental_price = "0.00",
            discount_price = "0.00",
            discount_percent = "0.00",
            security_deposit = "0.00",
            material_percentage = '"cotton"=>"20"',
            mode = "Express",
            brand_id = self.brand.id,
            features = "THe best feature the best it is ",
            size = ['S','M','L'],
            online_link_url = "http://chitrank-dixit.github.io/",
            visibility_order_index = 1,
            category = 'Express clothing',
            inventory_category_id = self.inventory_category.id,
            popularity_index = 90,
            item_domain = "CLT",
            gender = "FML"
        )
        self.inventory_item = InventoryItem.objects.create(
            warehouse_id = self.warehouse.id,
            parent_item_id = self.parent_item.id,
            size = ['S','M','L'],
            procurement_detail_id = self.procurement_detail.id,
            size_range_id = 2,
            product_tag = {'name':'abcd'},
            short_description = 'abcd',
            is_virtual = False,
            warehouse_stack_id = "12345",
            reason_for_out_stock = "NA",
            status = "ILV"
        )
        self.inventory_item.color.add(self.color)
        self.inventory_item.occasion.add(self.occasion)
        self.resource_url = '/api/v2/inventory/inventory_item_detail/'+str(self.inventory_item.parent_item.parent_id)+'/'

Now the instances made in the setUp can be used in the the test cases defined below.

Upvotes: 0

GwynBleidD
GwynBleidD

Reputation: 20539

Name of headers passed to client.get should follow CGI specification. From docs:

The headers sent via **extra should follow CGI specification. For example, emulating a different “Host” header as sent in the HTTP request from the browser to the server should be passed as HTTP_HOST.

That means, you should specify HTTP_CONTENT_TYPE instead of content_type.

Upvotes: 1

Related Questions