whitebear
whitebear

Reputation: 12433

How to make the csv file from django.http.response.StreamingHttpResponse

I have StreamingHttpResponse and it downloads the csv file in brower.

Now I want to use this in testcase

response = self.client.get('/lusers/?&export=csv')

response is StreamingHttpResponse

How can I make the csv file from this (not via browser)

and check the contents??

self.assertContains(response, "dataincsv")

Upvotes: 0

Views: 420

Answers (1)

var211
var211

Reputation: 606

You can make text string from StreamingHttpResponse like this:

str_response = '\n'.join(s.decode('U8') for s in response)

or

str_response = '\n'.join(map(lambda s: s.decode('U8'), iter(response)))

and check content with:

self.assertContains(str_response, "dataincsv")

StreamingHttpResponse is iterable object, and you can iteratate through this object any way you need.

For example if you want to write to file you could do something like:

# ...
for line in response:
    some_file.write(line)
# ...

Upvotes: 1

Related Questions