Ticket #7812: r8007-seek-testcase.patch

File r8007-seek-testcase.patch, 2.5 KB (added by kratorius, 7 years ago)

Test for the right behaviour of .read()

  • tests/regressiontests/file_uploads/views.py

     
    7878    for key in request.FILES.keys():
    7979        file_counts[key] = len(request.FILES.getlist(key))
    8080    return HttpResponse(simplejson.dumps(file_counts))
     81
     82def file_upload_not_empty_read(request):
     83    """
     84    Check that the first .read() returns the whole file (because we don't specify
     85    the size to read, so we read the entire file), while the second returns an
     86    empty string
     87    """
     88    for key in request.FILES.keys():
     89        if request.FILES[key].read() == '':
     90            return HttpResponseServerError()
     91        if request.FILES[key].read() != '':
     92            return HttpResponseServerError()
     93
     94    return HttpResponse('')
  • tests/regressiontests/file_uploads/tests.py

     
    187187        self.assertEqual(got.get('file1'), 1)
    188188        self.assertEqual(got.get('file2'), 2)
    189189
     190    def test_fileupload_seek(self):
     191        # small file (under the 5M quota)
     192        small_f = tempfile.NamedTemporaryFile()
     193        small_f.write('a' * (2 ** 10))
     194
     195        response = self.client.post('/file_uploads/notempty/', {
     196            'small_f': open(small_f.name)
     197        })
     198        self.assertEqual(response.status_code, 200)
     199
     200        # big file (over the 5M quota)
     201        big_f = tempfile.NamedTemporaryFile()
     202        big_f.write('a' * (10 * 2 ** 20))
     203
     204        response = self.client.post('/file_uploads/notempty/', {
     205            'big_f': open(big_f.name)
     206        })
     207        self.assertEqual(response.status_code, 200)
     208
    190209class DirectoryCreationTests(unittest.TestCase):
    191210    """
    192211    Tests for error handling during directory creation
  • tests/regressiontests/file_uploads/urls.py

     
    88    (r'^quota/$',           views.file_upload_quota),
    99    (r'^quota/broken/$',    views.file_upload_quota_broken),
    1010    (r'^getlist_count/$',   views.file_upload_getlist_count),
     11    (r'^notempty/$',        views.file_upload_not_empty_read),
    1112)
Back to Top