Moves the renaming ttask into the serialiser update instead of post_save. Feels more correct

This commit is contained in:
Trenton H
2023-02-17 17:43:56 -08:00
parent 5e3ef94697
commit abc58000b4
4 changed files with 46 additions and 104 deletions

View File

@@ -1033,95 +1033,6 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
self.assertEqual(generate_filename(doc_a), "0000002.pdf")
self.assertEqual(generate_filename(doc_b), "SomeImportantNone/2020-07-25.pdf")
@mock.patch("documents.bulk_edit.bulk_update_documents.delay")
def test_document_storage_path_changed_with_doc(self, bulk_update_mock):
"""
GIVEN:
- Document with a defined storage path
WHEN:
- The storage path format is updated
THEN:
- Document is renamed to the new format
"""
sp = StoragePath.objects.create(path="TestFolder/{created}")
document = Document.objects.create(
mime_type="application/pdf",
created=timezone.make_aware(datetime.datetime(2020, 6, 25, 7, 36, 51, 153)),
storage_path=sp,
)
# Ensure that filename is properly generated
document.filename = generate_filename(document)
self.assertEqual(
document.source_path,
os.path.join(settings.ORIGINALS_DIR, "TestFolder/2020-06-25.pdf"),
)
# Actually create the file
create_source_path_directory(document.source_path)
Path(document.source_path).touch()
self.assertTrue(os.path.isfile(document.source_path))
document.save()
# Change format
bulk_update_mock.side_effect = (
bulk_update_documents # call through, no Redis needed
)
sp.path = "NewFolder/{created}"
sp.save()
document.refresh_from_db()
self.assertEqual(
document.source_path,
os.path.join(settings.ORIGINALS_DIR, "NewFolder/2020-06-25.pdf"),
)
self.assertTrue(os.path.isfile(document.source_path))
@mock.patch("documents.bulk_edit.bulk_update_documents.delay")
def test_document_storage_path_changed_with_no_doc(self, bulk_update_mock):
"""
GIVEN:
- Document with no storage path
- Storage path exist
WHEN:
- The storage path format is updated
THEN:
- No changes to document
"""
sp = StoragePath.objects.create(path="TestFolder/{created}")
document = Document.objects.create(
mime_type="application/pdf",
created=timezone.make_aware(datetime.datetime(2021, 6, 25, 7, 36, 51, 153)),
)
# Ensure that filename is properly generated
document.filename = generate_filename(document)
self.assertEqual(
document.source_path,
os.path.join(settings.ORIGINALS_DIR, f"{document.pk:07}.pdf"),
)
# Actually create the file
create_source_path_directory(document.source_path)
Path(document.source_path).touch()
self.assertTrue(os.path.isfile(document.source_path))
document.save()
# Change format
sp.path = "NewFolder/{created}"
sp.save()
bulk_update_mock.assert_not_called()
document.refresh_from_db()
self.assertEqual(
document.source_path,
os.path.join(settings.ORIGINALS_DIR, f"{document.pk:07}.pdf"),
)
self.assertTrue(os.path.isfile(document.source_path))
@override_settings(
FILENAME_FORMAT="{created_year_short}/{created_month_name_short}/{created_month_name}/{title}",
)