Hi,
I had come across a similar issue last year. Filename would change after
the upload.
It been time and I am not sure why it happened then. But, i think it
happened
because a file with the same name was already present the given location.
And the django/drf code would make the name of the file unique by appending
random text like above.
You can check if this is the case.

You can try a couple of things.

1. Print the name of the file being generated as soon as it gets generated.

2. Print the name of the file and Path(d1.file.name).name just before the
assertion.
3. Check if a document with the same name already exists in the db and
media root folder, and make sure that it doesnt.

I hope it helps.

Cheers


Regards,
Chetan Ganji
+91-900-483-4183
ganji.che...@gmail.com
http://ryucoder.in


On Wed, Jun 10, 2020 at 12:31 AM Matthew Pava <matthew.p...@gmail.com>
wrote:

> Good day,
> I have been struggling with this issue for weeks, and I can't figure out
> what I'm doing wrong.
> I have a model with a FileField with a custom upload_to function. It
> seems to work fine when I'm doing runserver.
> Problems arise during my tests.
>
> My assertion error fails:
>
> AssertionError: 'Rev0_2020-06-09_L123_My_Document_63ExUTF.docx' !=
> 'Rev0_2020-06-09_L123_My_Document.docx'
> - Rev0_2020-06-09_L123_My_Document_63ExUTF.docx
> ?                                 --------
> + Rev0_2020-06-09_L123_My_Document.docx
>
>
> You see, it keeps adding these extra random characters to the filename,
> which is not at all in my upload_to function.
>
> class DocumentTestCase(TestCase):
>     def create_document(self, **kwargs):
>         if 'file' not in kwargs:
>             kwargs['file'] = self.get_test_file()
>         return Document.objects.create(**kwargs)
>
>      def _create_file(self):
>         f = tempfile.NamedTemporaryFile(suffix=self.TEST_FILE_EXTENSION, 
> delete=False)
>         with open(f.name, mode='wb'):
>
>             f.write(b"NA")
>
>         return open(f.name, mode='rb')
>
>
>     TEST_FILE_EXTENSION = ".docx"
>
>     def setUp(self):
>         super().setUp()
>
>         settings.MEDIA_ROOT = MEDIA_ROOT
>
>     def get_test_file(self):
>         file = self._create_file()
>
>         return File(file, name=file.name)
>
>     def test_rename_file_after_upload(self):
>         d1 = self.create_document(title="My Document", number="L123")
>
>         title = d1.title.replace(" ", "_")
>         extension = Path(d1.file.path).suffix
>
>         new_name = 
> f"Rev{d1.revision}_{d1.revision_date}_{d1.number}_{title}{extension}"
>         self.assertEqual(Path(d1.file.name).name, new_name)
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cbc812fc-6d5e-4afb-ab64-6bc0fad303a3o%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/cbc812fc-6d5e-4afb-ab64-6bc0fad303a3o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMKMUjsSKYU3FyD4SL%3DxumDc3spfM74KmnFhpRBbFniNe5ex2w%40mail.gmail.com.

Reply via email to