From 64a3c7f9aeda347a72e3f1e8e88381b5b3d479d8 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 15 Aug 2012 03:46:07 -0700 Subject: [PATCH] Ensured that about half of the files opened in the formtools tests were explicitly closed. --- .../tests/wizard/namedwizardtests/tests.py | 17 ++++++++++++----- .../formtools/tests/wizard/wizardtests/tests.py | 15 +++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py index a860edd9e9..7529d89a2c 100644 --- a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py @@ -122,6 +122,7 @@ class NamedWizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] + post_data['form2-file1'].close() post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post( reverse(self.wizard_urlname, @@ -149,7 +150,9 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['form_list'] - self.assertEqual(all_data[1]['file1'].read(), open(__file__, 'rb').read()) + with open(__file__, 'rb') as f: + self.assertEqual(all_data[1]['file1'].read(), f.read()) + all_data[1]['file1'].close() del all_data[1]['file1'] self.assertEqual(all_data, [ {'name': 'Pony', 'thirsty': True, 'user': self.testuser}, @@ -182,9 +185,10 @@ class NamedWizardTests(object): response = self.client.get(step2_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form2') - self.assertEqual( - response.context['wizard']['form'].files['form2-file1'].read(), - open(__file__, 'rb').read()) + with open(__file__, 'rb') as f: + self.assertEqual( + response.context['wizard']['form'].files['form2-file1'].read(), + f.read()) response = self.client.post( reverse(self.wizard_urlname, @@ -201,7 +205,9 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['all_cleaned_data'] - self.assertEqual(all_data['file1'].read(), open(__file__, 'rb').read()) + with open(__file__, 'rb') as f: + self.assertEqual(all_data['file1'].read(), f.read()) + all_data['file1'].close() del all_data['file1'] self.assertEqual( all_data, @@ -225,6 +231,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] + post_data['form2-file1'].close() post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post( reverse(self.wizard_urlname, diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py index a35664b322..586bd59341 100644 --- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py @@ -95,7 +95,9 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['form_list'] - self.assertEqual(all_data[1]['file1'].read(), open(__file__, 'rb').read()) + with open(__file__, 'rb') as f: + self.assertEqual(all_data[1]['file1'].read(), f.read()) + all_data[1]['file1'].close() del all_data[1]['file1'] self.assertEqual(all_data, [ {'name': 'Pony', 'thirsty': True, 'user': self.testuser}, @@ -112,8 +114,9 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__, 'rb') - response = self.client.post(self.wizard_url, post_data) + with open(__file__, 'rb') as post_file: + post_data['form2-file1'] = post_file + response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) response = self.client.post(self.wizard_url, self.wizard_step_data[2]) @@ -123,7 +126,9 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['all_cleaned_data'] - self.assertEqual(all_data['file1'].read(), open(__file__, 'rb').read()) + with open(__file__, 'rb') as f: + self.assertEqual(all_data['file1'].read(), f.read()) + all_data['file1'].close() del all_data['file1'] self.assertEqual(all_data, { 'name': 'Pony', 'thirsty': True, 'user': self.testuser, @@ -140,6 +145,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] + post_data['form2-file1'].close() post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -167,6 +173,7 @@ class WizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] + post_data['form2-file1'].close() post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200)