2012-04-24 26 views
5

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?

Trả lời

7

Ok, giao diện mới có thể đáng giá. Đây là thử nghiệm test_to_python mới của tôi, lần này nó hoạt động:

def test_to_python(self): 
    f = file(self.AUDIOFILE_TEST_ROOT + self.files[0]) 
    file_data = {'audiofilefield':SimpleUploadedFile(self.files[0],f.read())} 
    data = {} 
    form = self.TestForm(data,file_data) 
    self.assertTrue(form.is_valid()) 
+2

Bạn không thấy điều này gây ô nhiễm cho thư mục 'upload /' của bạn? –

5

giải pháp thay thế (vì câu hỏi này là kết quả nhất trên Google "Django thử nghiệm mô phỏng upload"): Khách hàng kiểm tra được xây dựng trong Django chấp nhận đối tượng tập tin mở như dữ liệu POST :

# the form 
class TestForm(forms.Form): 
    audiofilefield = AudioFileFormField() 

# the view 
def upload_view(request): 
    form = TestForm(data=request.POST or None, files=request.FILES or None) 
    if request.method == 'POST': 
     if form.is_valid(): 
       return HttpResponse('success') 
     else: 
       return HttpResponse('fail') 

# the test function 
class UploadTest(TestCase): 
    def test_upload(self): 
     c = Client() # django.test.client.Client 
     formdata = {} 
     with open('/my/audiofile.mp3', 'rb') as f: 
      formdata['audiofilefield'] = f 
      response = c.post('/my-test-view/', formdata) 
Các vấn đề liên quan