8

I have a Django test that accesses a web page using the Django test client.

In one of the tests, the server returns a ZIP file as an attachment. I access the content of the ZIP file with the following code:

zip_content = StringIO(response.content)
zip = ZipFile(zip_content)

This causes the following deprecation warning:

D:/Developments/Archaeology/DB/ArtefactDatabase/Webserver\importexport\tests\test_import.py:1: DeprecationWarning: Accessing the content attribute on a streaming response is deprecated. Use the streaming_content attribute instead.`

response.streaming_content returns some sort of map, which is definitely not a file-like object that's required for ZipFile. How can I use the streaming_content attribute for this?

Incidentally, I only get the deprecation warning when passing response.content to a StringIO, when I access the response.content of an ordinary HTML page, there's no warning.

zmbq
  • 38,013
  • 14
  • 101
  • 171

2 Answers2

8

Using Python 3.4.

with String:

zip_content = io.StringIO("".join(response.streaming_content))
zip = ZipFile(zip_content)

with Bytes:

zip_content = io.BytesIO(b"".join(response.streaming_content))
zip = ZipFile(zip_content)

Solution found in TestStreamingMixin of https://github.com/sio2project/oioioi/blob/master/oioioi/filetracker/tests.py

See also: https://docs.djangoproject.com/en/1.7/ref/request-response/

You might want to test whether the response is a stream by checking response.streaming (boolean).

Risadinha
  • 16,058
  • 2
  • 88
  • 91
-3

You should change the approach of your test. The response.streaming_content does exactly what it purposes to do. Just test is call to download is ok.

If you want to test file generation/integrity methods, you need to test its feature separately. Doesn't matter if your file is a ZIP or a CSV to Django Test, but if your call to that is ok.

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • 7
    The purpose of an integration test is to test the call and its result for correctness. It's not sufficient to say the test passes without checking whether the content of the response is correct. – Risadinha Nov 08 '14 at 16:44