Discussion:
Unit Testing Uploading CSV file
Alexander Lamas
2018-10-26 10:11:26 UTC
Permalink
Hi All,

I've created a DRF Web API application and I've got the following error
when running my unit test.

*Message: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse`
to be returned from the view, but received a `<class 'NoneType'>`*

My intention is to simulate my ajax call when uploading the file.
The only difference I've noticed between ajax call and unit test api call
is when calling via ajax I can use *request.data['file'].read()* which
gives me the file contents.
When I make the API call via django I can not use*
request.data['file'].read()*, as it gives me *b'' (which means blank)*

This is the code

class Import_CSV_File_Test(TestCase):
def setUp(self):
utf8_file = os.path.join(settings.BASE_DIR, "app", "tests", "data", "user_utf8n.csv")

file_utf8 = SimpleUploadedFile(utf8_file, open(utf8_file, 'rb').read(), content_type='application/vnd.ms-excel')

self.form_data = {'encode': 'utf8', 'file': file_utf8}

def test_import_file_format_utf8(self):
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)


Have any of you encountered this issue?

Do you guys have another way of unit testing the file upload api call?

Thank you very much.

Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-10-26 11:08:23 UTC
Permalink
sounds like your view is a little wrong, because its not returning any
response (eg None)
Post by Alexander Lamas
Hi All,
I've created a DRF Web API application and I've got the following error
when running my unit test.
*Message: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse`
to be returned from the view, but received a `<class 'NoneType'>`*
My intention is to simulate my ajax call when uploading the file.
The only difference I've noticed between ajax call and unit test api call
is when calling via ajax I can use *request.data['file'].read()* which
gives me the file contents.
When I make the API call via django I can not use*
request.data['file'].read()*, as it gives me *b'' (which means blank)*
This is the code
utf8_file = os.path.join(settings.BASE_DIR, "app", "tests", "data", "user_utf8n.csv")
file_utf8 = SimpleUploadedFile(utf8_file, open(utf8_file, 'rb').read(), content_type='application/vnd.ms-excel')
self.form_data = {'encode': 'utf8', 'file': file_utf8}
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Have any of you encountered this issue?
Do you guys have another way of unit testing the file upload api call?
Thank you very much.
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-10-29 00:58:19 UTC
Permalink
Hi Jason,

Thanks for your reply.

Actually, my view is working fine and running time.
The problem is only at the unit test time.
Post by Jason
sounds like your view is a little wrong, because its not returning any
response (eg None)
Post by Alexander Lamas
Hi All,
I've created a DRF Web API application and I've got the following error
when running my unit test.
*Message: Expected a `Response`, `HttpResponse` or
`HttpStreamingResponse` to be returned from the view, but received a
`<class 'NoneType'>`*
My intention is to simulate my ajax call when uploading the file.
The only difference I've noticed between ajax call and unit test api call
is when calling via ajax I can use *request.data['file'].read()* which
gives me the file contents.
When I make the API call via django I can not use*
request.data['file'].read()*, as it gives me *b'' (which means blank)*
This is the code
utf8_file = os.path.join(settings.BASE_DIR, "app", "tests", "data", "user_utf8n.csv")
file_utf8 = SimpleUploadedFile(utf8_file, open(utf8_file, 'rb').read(), content_type='application/vnd.ms-excel')
self.form_data = {'encode': 'utf8', 'file': file_utf8}
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Have any of you encountered this issue?
Do you guys have another way of unit testing the file upload api call?
Thank you very much.
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gordon
2018-10-29 03:33:18 UTC
Permalink
Didn't see which request client you were using. Did you try
adding format='multipart' to your post call? That seems like your issue
Post by Alexander Lamas
Hi Jason,
Thanks for your reply.
Actually, my view is working fine and running time.
The problem is only at the unit test time.
Post by Jason
sounds like your view is a little wrong, because its not returning any
response (eg None)
Post by Alexander Lamas
Hi All,
I've created a DRF Web API application and I've got the following error
when running my unit test.
*Message: Expected a `Response`, `HttpResponse` or
`HttpStreamingResponse` to be returned from the view, but received a
`<class 'NoneType'>`*
My intention is to simulate my ajax call when uploading the file.
The only difference I've noticed between ajax call and unit test api
call is when calling via ajax I can use *request.data['file'].read()* which
gives me the file contents.
When I make the API call via django I can not use*
request.data['file'].read()*, as it gives me *b'' (which means blank)*
This is the code
utf8_file = os.path.join(settings.BASE_DIR, "app", "tests", "data", "user_utf8n.csv")
file_utf8 = SimpleUploadedFile(utf8_file, open(utf8_file, 'rb').read(), content_type='application/vnd.ms-excel')
self.form_data = {'encode': 'utf8', 'file': file_utf8}
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Have any of you encountered this issue?
Do you guys have another way of unit testing the file upload api call?
Thank you very much.
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-10-29 04:27:18 UTC
Permalink
Hi g,

Thanks for your reply.

Actually, I've tried adding format='multipart' and it did't work.

Here is what I have tried.

def test_import_file_format_utf8(self):
#response = self.client.post(reverse(self._url, args=[self._egateid]), self.formData, content_type = 'multipart/form-data', **self.headers)
#response = self.client.post(reverse(self._url, args=[self._egateid]), self.formData, format='multipart')
#response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.formData, content_type='multipart/form-data')
#response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data, follow=True, **self.headers)
#response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
#response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data, **self.headers)
#response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data, content_type='multipart/form-data')
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)



Have you done something like that?

Could please send me some sample or even a link with something similar?

Thank you!

Regards,
Alex
Post by gordon
Didn't see which request client you were using. Did you try
adding format='multipart' to your post call? That seems like your issue
Post by Alexander Lamas
Hi Jason,
Thanks for your reply.
Actually, my view is working fine and running time.
The problem is only at the unit test time.
Post by Jason
sounds like your view is a little wrong, because its not returning any
response (eg None)
Post by Alexander Lamas
Hi All,
I've created a DRF Web API application and I've got the following error
when running my unit test.
*Message: Expected a `Response`, `HttpResponse` or
`HttpStreamingResponse` to be returned from the view, but received a
`<class 'NoneType'>`*
My intention is to simulate my ajax call when uploading the file.
The only difference I've noticed between ajax call and unit test api
call is when calling via ajax I can use *request.data['file'].read()* which
gives me the file contents.
When I make the API call via django I can not use*
request.data['file'].read()*, as it gives me *b'' (which means blank)*
This is the code
utf8_file = os.path.join(settings.BASE_DIR, "app", "tests", "data", "user_utf8n.csv")
file_utf8 = SimpleUploadedFile(utf8_file, open(utf8_file, 'rb').read(), content_type='application/vnd.ms-excel')
self.form_data = {'encode': 'utf8', 'file': file_utf8}
response = self.client.post(reverse(self._url, args=[self._egateid]), data=self.form_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Have any of you encountered this issue?
Do you guys have another way of unit testing the file upload api call?
Thank you very much.
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...