Search code examples
django-viewsdjango-testing

Unable to unittest Django APIviews with TestCase library?


This is a very project specific question, but I can't figure out what I'm doing wrong. I have a view class that allows users to perform GET and POST requests to the project database. I am able to successfully GET/POST using curl, but when I write my unittests for this, I can't get it to pass the test, and I don't fully understand why.

This command works:

curl http://127.0.0.1:8000/list/categories/ -d '{"id":"00001", "name":"some info"}'

view.py:

class CategoryList(APIView):

    def get(self, request, format=None):
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        context = {
            'categories': categories
        }
        return render(request, 'myapp/categories.html', context)


    def post(self, request, format=None):
        print('=======inside post=========')
        data = JSONParser().parse(request)
        serializer = CategorySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

urls.py:

urlpatterns = [
# eg: /list/categories/
url(r'^categories/$', CategoryList.as_view()),
]

Here's where my test fails.

tests.py:

class CategoryViewTests(TestCase):
    def setUp(self):
        """ Mock model data """
        cat1 = Category.objects.create(id=55555, name="Shoes")
        cat2 = Category.objects.create(id=12345, name="Wallets")

    def test_new_unique_category(self):
        """
        Create a new category obj
        """
        c = Client()
        print(str(Category.objects.all()))
        new_cat_data = {'id':'54211', 'name':'Soccer Balls'}
        response = c.post('/list/categories', new_cat_data)

        print(str(Category.objects.all()))

        self.assertEqual(response.status_code, 201)

Terminal output:

test_new_unique_category (inferbrand.tests.CategoryViewTests) ... 
<QuerySet [<Category: Test Men's Shoes>, <Category: Test Wallets>]>
<QuerySet [<Category: Test Men's Shoes>, <Category: Test Wallets>]>
FAIL


======================================================================
FAIL: test_new_unique_category (inferbrand.tests.CategoryViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/proj/tests.py", line 104, in test_new_unique_category
self.assertEqual(response.status_code, 201)
AssertionError: 301 != 201

What am I doing wrong?? Any help would be greatly appreciated.


Solution

  • Okay. I think the root cause of this is that serializers aren't able to process the incoming request from django unittests correctly. I ended up removing all the serializers, and instead replaced them like so:

    class CategoryList(APIView):
    
        def get(self, request, format=None):
            categories = Category.objects.all()
            context = {
                'categories': categories
            }
            return render(
                request,
                'myapp/categories.html',
                context
                )
    
    
        def post(self, request, format=None):
            data = request.POST
            new_cat = Category(id=data['id'],
                               name=data['name'])
            new_cat.save()
    
    
            return HttpResponseRedirect(reverse('myapp:category_detail', args=[ new_cat.id ]))
    

    new curl format:

    curl http://127.0.0.1:8000/list/categories/ -d 'id=1299988&name=Tennis123'
    

    The post HttpResponseRedirect still isn't working correctly, (returns status 301), but POST/GET now works using curl and unittesting which was the main issue I was having.