I am trying to write unit test for a class which is having a POST method for uploading documents to web based django application. Here is my view class for which I want to write unit test:
class SOP(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def post(self,request):
returnDict={}
returnDict['msg']='File not uploaded'
#if form.is_valid():
newdoc = Document(sopFile = request.FILES['sopFile'])
newdoc.save()
returnDict['msg']='File uploaded'
returnDict['fileName']=newdoc.sopFile.name
# Redirect to the document list after POST
return Response(returnDict)
As my django application is using forms.py for uploading a file so I am putting that code also along with this:
from django import forms
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
I have tried to write test case using RequestFactory() and TestCase() but I am not able to figure how to write unit test for this type of class/views...