Fixed formtools tests with Python 2

Fixes #19905 again.
This commit is contained in:
Claude Paroz 2014-06-09 10:28:39 +02:00
parent 96e4b52ab2
commit 1aaf4053f5
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.core.urlresolvers import reverse
from django.http import QueryDict from django.http import QueryDict
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils._os import upath
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.tests.utils import skipIfCustomUser 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 from .forms import temp_storage
# On Python 2, __file__ may end with .pyc
THIS_FILE = upath(__file__).rstrip("c")
UPLOADED_FILE_NAME = 'tests.py' UPLOADED_FILE_NAME = 'tests.py'
@ -141,7 +144,7 @@ class NamedWizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
@ -153,7 +156,7 @@ class NamedWizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form3') self.assertEqual(response.context['wizard']['steps'].current, 'form3')
# Check that the file got uploaded properly. # 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()) self.assertEqual(f.read(), f2.read())
response = self.client.post( response = self.client.post(
@ -196,7 +199,7 @@ class NamedWizardTests(object):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
@ -210,7 +213,7 @@ class NamedWizardTests(object):
response = self.client.get(step2_url) response = self.client.get(step2_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form2') 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()) self.assertEqual(f.read(), f2.read())
response = self.client.post( response = self.client.post(
@ -259,7 +262,7 @@ class NamedWizardTests(object):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, reverse(self.wizard_urlname,

View File

@ -16,6 +16,8 @@ from django.contrib.formtools.tests.models import Poet, Poem
from .forms import temp_storage from .forms import temp_storage
# On Python 2, __file__ may end with .pyc
THIS_FILE = upath(__file__.rstrip("c"))
UPLOADED_FILE_NAME = 'tests.py' UPLOADED_FILE_NAME = 'tests.py'
@ -115,14 +117,14 @@ class WizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post(self.wizard_url, post_data) response = self.client.post(self.wizard_url, post_data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form3') self.assertEqual(response.context['wizard']['steps'].current, 'form3')
# Check that the file got uploaded properly. # 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()) self.assertEqual(f.read(), f2.read())
response = self.client.post(self.wizard_url, self.wizard_step_data[2]) response = self.client.post(self.wizard_url, self.wizard_step_data[2])
@ -152,7 +154,7 @@ class WizardTests(object):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post(self.wizard_url, post_data) response = self.client.post(self.wizard_url, post_data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -184,7 +186,7 @@ class WizardTests(object):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post(self.wizard_url, post_data) response = self.client.post(self.wizard_url, post_data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -212,7 +214,7 @@ class WizardTests(object):
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
post_data = self.wizard_step_data[1] 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 post_data['form2-file1'] = post_file
response = self.client.post(self.wizard_url, post_data) response = self.client.post(self.wizard_url, post_data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -333,7 +335,7 @@ class WizardTestKwargs(TestCase):
self.wizard_step_data[0]['form1-user'] = self.testuser.pk self.wizard_step_data[0]['form1-user'] = self.testuser.pk
def test_template(self): 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( with self.settings(
TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]): TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
response = self.client.get(self.wizard_url) response = self.client.get(self.wizard_url)