4

I've been trying to get the tests running on upload forms. But, whenever I run the tests, it says that file is of wrong type.

Upload form saves the file with a randomly generated file name:

class Video(models.Model):
    def get_generated_path(self):
        # Generates random path for video file upload

    original_file = models.CharField(null=True)
    uploaded_file = models.FileField(storage=FileSystemStorage(location=
        settings.MEDIA_ROOT), upload_to=get_generated_path)
    video_name = models.TextField()

And form looks like:

 class VideoForm(forms.Form):
      video_file = forms.FileField()
      video_name = forms.CharField()

      def clean_video_file(forms.Form):
          content = self.cleaned_data['video_file']
          content_type = content.content_type.split('/')[0]
          if content_type in settings.CONTENT_TYPES:
              if content._size > settings.MAX_UPLOAD_SIZE:
                  raise forms.ValidationError(_('Please keep filesize under %s. 
                      Current filesize %s') % (filesizeformat(
                      settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
          else:
              raise forms.ValidationError(_('File type is not supported, 
                  content type is: %s' % content_type))
          return content

Most of the remaining logic is in views:

 def upload_video(request):
     try:
         # Check if user is authenticated
         if form.is_valid():
             video_file = request.FILES['video_file']
             video_name = form.cleaned_data['video_name']
             save_video = Video.objects.create(
                  original_file = 'uploaded_videos' + user.username,
                  video_name = video_name)
             return HTTPResponseRedirect('next-page')
      except Exception, e:
             ...

The tests are written as:

def test_video_form(TestCase):
    user = #Create a dummy user
    test_video = SimpleUploadedFile('test_video.flv', open(
        'path/to/test/video', 'rb'))
    form = VideoForm(user, {'video_name': test_video}, )
    self.assertTrue(form.is_valid())

The above test always fails since it says that the file type of 'test_video.flv' is plain/text. I've checked the 'test_video.flv' and its of correct type. How to pass these files to the upload form and test it.

4

1 回答 1

3

SimpleUploadedFile has the content_type text/plain by default. It is why your testing code goes fail in clean_video_file and this line:

 raise forms.ValidationError(_('File type is not supported, 
              content type is: %s' % content_type))

is printing:

File type is not supported, 
              content type is: text/plain

Pass the content_type = 'video'in the SimpleUploadedFile line as shown below.

def test_video_form(TestCase):
    user = #Create a dummy user
    test_video = SimpleUploadedFile('test_video.flv', open(
    'path/to/test/video', 'rb'), content_type='video') # Notice the change here.
    form = VideoForm(user, {'video_name': test_video}, )
    self.assertTrue(form.is_valid())
于 2013-04-28T07:42:38.943 回答