[1.7.x] Fixed formtools tests with Python 2

Fixes #19905 again.
Backport of 1aaf4053f5 from master.
This commit is contained in:
Claude Paroz 2014-06-09 10:28:39 +02:00
parent b78710ec02
commit d4623d13b7
2 changed files with 16 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import copy
from django.core.urlresolvers import reverse
from django.http import QueryDict
from django.test import TestCase
from django.utils._os import upath
from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser
@ -16,6 +17,8 @@ from django.contrib.formtools.tests.wizard.test_forms import get_request, Step1,
from .forms import temp_storage
# On Python 2, __file__ may end with .pyc
THIS_FILE = upath(__file__).rstrip("c")
UPLOADED_FILE_NAME = 'tests.py'
@ -142,7 +145,7 @@ class NamedWizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1]
with open(__file__, 'rb') as post_file:
with open(THIS_FILE, 'rb') as post_file:
post_data['form2-file1'] = post_file
response = self.client.post(
reverse(self.wizard_urlname,
@ -154,7 +157,7 @@ class NamedWizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form3')
# Check that the file got uploaded properly.
with open(__file__, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
self.assertEqual(f.read(), f2.read())
response = self.client.post(
@ -197,7 +200,7 @@ class NamedWizardTests(object):
self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1]
with open(__file__, 'rb') as post_file:
with open(THIS_FILE, 'rb') as post_file:
post_data['form2-file1'] = post_file
response = self.client.post(
reverse(self.wizard_urlname,
@ -211,7 +214,7 @@ class NamedWizardTests(object):
response = self.client.get(step2_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form2')
with open(__file__, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
self.assertEqual(f.read(), f2.read())
response = self.client.post(
@ -260,7 +263,7 @@ class NamedWizardTests(object):
self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1]
with open(__file__, 'rb') as post_file:
with open(THIS_FILE, 'rb') as post_file:
post_data['form2-file1'] = post_file
response = self.client.post(
reverse(self.wizard_urlname,

View File

@ -16,6 +16,8 @@ from django.contrib.formtools.tests.models import Poet, Poem
from .forms import temp_storage
# On Python 2, __file__ may end with .pyc
THIS_FILE = upath(__file__.rstrip("c"))
UPLOADED_FILE_NAME = 'tests.py'
@ -116,14 +118,14 @@ class WizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1]
with open(upath(__file__), 'rb') as post_file:
with open(upath(THIS_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)
self.assertEqual(response.context['wizard']['steps'].current, 'form3')
# Check that the file got uploaded properly.
with open(upath(__file__), 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2:
self.assertEqual(f.read(), f2.read())
response = self.client.post(self.wizard_url, self.wizard_step_data[2])
@ -153,7 +155,7 @@ class WizardTests(object):
self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1]
with open(upath(__file__), 'rb') as post_file:
with open(THIS_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)
@ -185,7 +187,7 @@ class WizardTests(object):
self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1]
with open(upath(__file__), 'rb') as post_file:
with open(THIS_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)
@ -213,7 +215,7 @@ class WizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1]
with open(upath(__file__), 'rb') as post_file:
with open(THIS_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)
@ -332,7 +334,7 @@ class WizardTestKwargs(TestCase):
self.wizard_step_data[0]['form1-user'] = self.testuser.pk
def test_template(self):
templates = os.path.join(os.path.dirname(upath(__file__)), 'templates')
templates = os.path.join(os.path.dirname(THIS_FILE), 'templates')
with self.settings(
TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
response = self.client.get(self.wizard_url)