Tôi đang thử nghiệm một FormField AudioFileFormField tự viết, kiểm tra xem một tệp có phải là tệp âm thanh trước khi lưu trữ nó hay không. Cho rằng tôi đã ghi đè lên phương pháp to_python. Đang cố gắng để kiểm tra FormField này tôi gặp phải một số khó khăn.Cách chính xác để mô phỏng đệ quy để thử nghiệm một biểu mẫu là gì?
Đây là TestCase của tôi cho đến nay:
from django import forms
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
class TestAudioFileFormField(TestCase):
""" test the formfield to use for AudioFile Uploads """
class TestForm(forms.Form):
audiofilefield = AudioFileFormField()
def setUp(self):
self.AUDIOFILE_TEST_ROOT = os.path.dirname(__file__) + '/data/'
self.files = audiofile_files
def test_to_python(self):
""" assign some files to a form and validate the form """
f = file(self.AUDIOFILE_TEST_ROOT + self.files[0])
file_data = {'audiofilefield':SimpleUploadedFile(self.files[0],f.read())}
data = {}
form = self.TestForm(data,f)
form.is_valid()
Dòng form.is_valid() đặt ra một AttributeError: 'file' đối tượng không có thuộc tính 'get'
Khi tôi chèn một dấu vết debug ngay trước form.is_valid(), đây là những gì tôi nhận được trong phiên tương tác đó:
ipdb> form.is_valid()
AttributeError: 'file' object has no attribute 'get'
ipdb> suf = file_data['audiofilefield']
ipdb> suf
<SimpleUploadedFile: invalid_format.mp3 (text/plain)>
ipdb> dir(suf)
[lots of info here]
ipdb> form.is_valid()
True
Tôi đã thay đổi gì trong phiên tương tác để xác thực biểu mẫu đang hoạt động? Điều gì sẽ là cách chính xác để truyền tệp tới SimpleUploadedFile để tránh AttributeError?
Bạn không thấy điều này gây ô nhiễm cho thư mục 'upload /' của bạn? –