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.

Reply via email to