#27184: django test client bug when use TemporaryFile  in Mac
-------------------------------------+-------------------------------------
     Reporter:  joonsikjang          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Testing framework    |                  Version:  1.9
     Severity:  Normal               |               Resolution:
     Keywords:  test TemporaryFile   |             Triage Stage:
  django client file upload          |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by timgraham):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> below code is file upload test method
> This code was generated error message when I test only in Mac.
> This code was ok when I test in Windows 10.
> if I change TemporaryFile to NamedTemporaryFile, then It work correctly.
>

> {{{
> def test_ChunkedSampleFile(self):
>         """
>         ChunkedSampleFileList, ChunkedSampleFileDetail View Test
>         """
>         with TemporaryFile() as test_file:
>             for count in range(100000):
>                 test_file.write(('test_data %s' % count).encode())
>             test_file.seek(0)
>             hash_md5 = hashlib.md5()
>             for chunk in iter(lambda: test_file.read(4096), b""):
>                 hash_md5.update(chunk)
>             etag = hash_md5.hexdigest()
>             test_file.seek(0)
>
>             #post
>             response = self.client.post('%s/chunked_sample_files' %
> URL_PREFIX,
>                         data={'file': test_file, 'name': 'test.vcf',
>                               'sample_file_meta':
> self.sample_file_meta_id,
>                               'file_seq': 1, 'etag': etag},
>                         **{AUTH_TOKEN_HEADER_NAME: 'Token %s' %
> self.testuserfastqc1_token} )
>             self.assertEqual(response.status_code,
> status.HTTP_201_CREATED, response.content)
>             file_id = response.json()['id']
> }}}
>
> Error Message
>
> ERROR: test_ChunkedSampleFile
> (analysis_files.tests.AnalysisFilesTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File
> "/Users/joonsik/git/ngenebio_webapi/ngenebio_webapi/analysis_files/tests.py",
> line 174, in test_ChunkedSampleFile
>     **{AUTH_TOKEN_HEADER_NAME: 'Token %s' % self.testuserfastqc1_token} )
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
> packages/django/test/client.py", line 515, in post
>     secure=secure, **extra)
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
> packages/django/test/client.py", line 311, in post
>     post_data = self._encode_data(data, content_type)
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
> packages/django/test/client.py", line 275, in _encode_data
>     return encode_multipart(BOUNDARY, data)
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
> packages/django/test/client.py", line 171, in encode_multipart
>     lines.extend(encode_file(boundary, key, value))
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
> packages/django/test/client.py", line 200, in encode_file
>     filename = os.path.basename(file.name) if hasattr(file, 'name') else
> ''
>   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/posixpath.py",
> line 139, in basename
>     i = p.rfind(sep) + 1
> AttributeError: 'int' object has no attribute 'rfind'

New description:

 below code is file upload test method
 This code was generated error message when I test only in Mac.
 This code was ok when I test in Windows 10.
 if I change TemporaryFile to NamedTemporaryFile, then It work correctly.


 {{{
 def test_ChunkedSampleFile(self):
         """
         ChunkedSampleFileList, ChunkedSampleFileDetail View Test
         """
         with TemporaryFile() as test_file:
             for count in range(100000):
                 test_file.write(('test_data %s' % count).encode())
             test_file.seek(0)
             hash_md5 = hashlib.md5()
             for chunk in iter(lambda: test_file.read(4096), b""):
                 hash_md5.update(chunk)
             etag = hash_md5.hexdigest()
             test_file.seek(0)

             #post
             response = self.client.post('%s/chunked_sample_files' %
 URL_PREFIX,
                         data={'file': test_file, 'name': 'test.vcf',
                               'sample_file_meta':
 self.sample_file_meta_id,
                               'file_seq': 1, 'etag': etag},
                         **{AUTH_TOKEN_HEADER_NAME: 'Token %s' %
 self.testuserfastqc1_token} )
             self.assertEqual(response.status_code,
 status.HTTP_201_CREATED, response.content)
             file_id = response.json()['id']
 }}}

 Error Message
 {{{
 ERROR: test_ChunkedSampleFile (analysis_files.tests.AnalysisFilesTestCase)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File
 "/Users/joonsik/git/ngenebio_webapi/ngenebio_webapi/analysis_files/tests.py",
 line 174, in test_ChunkedSampleFile
     **{AUTH_TOKEN_HEADER_NAME: 'Token %s' % self.testuserfastqc1_token} )
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
 packages/django/test/client.py", line 515, in post
     secure=secure, **extra)
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
 packages/django/test/client.py", line 311, in post
     post_data = self._encode_data(data, content_type)
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
 packages/django/test/client.py", line 275, in _encode_data
     return encode_multipart(BOUNDARY, data)
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
 packages/django/test/client.py", line 171, in encode_multipart
     lines.extend(encode_file(boundary, key, value))
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-
 packages/django/test/client.py", line 200, in encode_file
     filename = os.path.basename(file.name) if hasattr(file, 'name') else
 ''
   File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/posixpath.py",
 line 139, in basename
     i = p.rfind(sep) + 1
 AttributeError: 'int' object has no attribute 'rfind'
 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27184#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.88a45105c5b04067d30163bfff381383%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to