Avoided firing the request_finished signal in tests.

* Avoided calling BaseHttpResponse.close(). The test client take care of
  that since acc5396e.
* Disconnected the request_finished signal when this method must be
  called. The test client has a similar implementation since bacb097a.
This commit is contained in:
Aymeric Augustin 2013-02-18 14:26:33 +01:00
parent 09ca010768
commit 92837ae569
3 changed files with 11 additions and 7 deletions

View File

@ -7,6 +7,8 @@ import pickle
import warnings
from django.core.exceptions import SuspiciousOperation
from django.core.signals import request_finished
from django.db import close_connection
from django.http import (QueryDict, HttpResponse, HttpResponseRedirect,
HttpResponsePermanentRedirect, HttpResponseNotAllowed,
HttpResponseNotModified, StreamingHttpResponse,
@ -484,6 +486,15 @@ class StreamingHttpResponseTests(TestCase):
r.tell()
class FileCloseTests(TestCase):
def setUp(self):
# Disable the request_finished signal during this test
# to avoid interfering with the database connection.
request_finished.disconnect(close_connection)
def tearDown(self):
request_finished.connect(close_connection)
def test_response(self):
filename = os.path.join(os.path.dirname(upath(__file__)), 'abc.txt')

View File

@ -506,7 +506,6 @@ def streamTest(format, self):
self.assertEqual(string_data, stream.getvalue())
else:
self.assertEqual(string_data, stream.content.decode('utf-8'))
stream.close()
for format in serializers.get_serializer_formats():
setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))

View File

@ -27,7 +27,6 @@ class StaticTests(TestCase):
for filename in media_files:
response = self.client.get('/views/%s/%s' % (self.prefix, filename))
response_content = b''.join(response)
response.close()
file_path = path.join(media_dir, filename)
with open(file_path, 'rb') as fp:
self.assertEqual(fp.read(), response_content)
@ -36,14 +35,12 @@ class StaticTests(TestCase):
def test_unknown_mime_type(self):
response = self.client.get('/views/%s/file.unknown' % self.prefix)
response.close()
self.assertEqual('application/octet-stream', response['Content-Type'])
def test_copes_with_empty_path_component(self):
file_name = 'file.txt'
response = self.client.get('/views/%s//%s' % (self.prefix, file_name))
response_content = b''.join(response)
response.close()
with open(path.join(media_dir, file_name), 'rb') as fp:
self.assertEqual(fp.read(), response_content)
@ -52,7 +49,6 @@ class StaticTests(TestCase):
response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT')
response_content = b''.join(response)
response.close()
with open(path.join(media_dir, file_name), 'rb') as fp:
self.assertEqual(fp.read(), response_content)
@ -77,7 +73,6 @@ class StaticTests(TestCase):
response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
HTTP_IF_MODIFIED_SINCE=invalid_date)
response_content = b''.join(response)
response.close()
with open(path.join(media_dir, file_name), 'rb') as fp:
self.assertEqual(fp.read(), response_content)
self.assertEqual(len(response_content),
@ -94,7 +89,6 @@ class StaticTests(TestCase):
response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
HTTP_IF_MODIFIED_SINCE=invalid_date)
response_content = b''.join(response)
response.close()
with open(path.join(media_dir, file_name), 'rb') as fp:
self.assertEqual(fp.read(), response_content)
self.assertEqual(len(response_content),