def test_backup(self): with TemporaryUploadedFile("test_backup.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(b"content one") tmp_file.flush() tmp_file.seek(0) instance1 = OverwriteFileSystemStorageModel.objects.create(file=tmp_file) # create a second instance with the same filename and different content: with TemporaryUploadedFile("test_backup.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(b"content two") tmp_file.flush() tmp_file.seek(0) instance2 = OverwriteFileSystemStorageModel.objects.create(file=tmp_file) assert_is_file(instance1.file.path) assert_is_file(instance2.file.path) assert instance1.file.path == instance2.file.path assert instance1.file.name == "test_backup.txt" assert instance2.file.name == "test_backup.txt" assert_filenames_and_content( path=temp_storage_location, reference=[("test_backup.txt", b"content two"), ("test_backup.txt.bak01", b"content one")], )
def test_backup(self): with TemporaryUploadedFile("test_backup.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(b"content one") tmp_file.flush() tmp_file.seek(0) instance1 = OverwriteFileSystemStorageModel.objects.create( file=tmp_file) # create a second instance with the same filename and different content: with TemporaryUploadedFile("test_backup.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(b"content two") tmp_file.flush() tmp_file.seek(0) instance2 = OverwriteFileSystemStorageModel.objects.create( file=tmp_file) assert_is_file(instance1.file.path) assert_is_file(instance2.file.path) assert instance1.file.path == instance2.file.path assert instance1.file.name == "test_backup.txt" assert instance2.file.name == "test_backup.txt" assert_filenames_and_content( path=temp_storage_location, reference=[("test_backup.txt", b"content two"), ("test_backup.txt.bak01", b"content one")], )
def test_backup_only_one_time(self): for i in range(4): with TemporaryUploadedFile("test_backup_only_one_time.txt", "text/plain", 0, "UTF-8") as tmp_file: instance = OverwriteFileSystemStorageModel.objects.create(file=tmp_file) assert_is_file(instance.file.path) assert instance.file.name == "test_backup_only_one_time.txt" assert_filenames_and_content(path=temp_storage_location, reference=[("test_backup_only_one_time.txt", b"")])
def test_basic_save(self): with TemporaryUploadedFile("test_basic_save.txt", "text/plain", 0, "UTF-8") as tmp_file: instance = OverwriteFileSystemStorageModel.objects.create( file=tmp_file) print(instance.file.path) assert_is_file(instance.file.path) assert instance.file.name == "test_basic_save.txt" assert_filenames_and_content(path=temp_storage_location, reference=[("test_basic_save.txt", b"")])
def test_backups(self): from django_tools.file_storage.file_system_storage import log as origin_log def log_temp_storage_location(): for no, item in enumerate( sorted(Path(temp_storage_location).iterdir()), 1): with item.open("rb") as f: origin_log.info("\t%i %s -> %r", no, item, repr(f.read())) for content_no in range(1, 7): # Save 4 times different content content = "content %i" % content_no for file_no in range(1, 5): origin_log.critical("_" * 100) origin_log.critical("Save %r for %i time(s)", content, file_no) with TemporaryUploadedFile("same_filename.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(bytes(content, "utf-8")) tmp_file.flush() tmp_file.seek(0) instance = OverwriteFileSystemStorageModel.objects.create( file=tmp_file) print(repr(instance)) log_temp_storage_location() print(instance.file.path) assert_is_file(instance.file.path) assert instance.file.name == "same_filename.txt" assert_filenames_and_content( path=temp_storage_location, reference=[ ("same_filename.txt", b"content 6"), ("same_filename.txt.bak01", b"content 1"), ("same_filename.txt.bak02", b"content 2"), ("same_filename.txt.bak03", b"content 3"), ("same_filename.txt.bak04", b"content 4"), ("same_filename.txt.bak05", b"content 5"), ], )
def test_backups(self): from django_tools.file_storage.file_system_storage import log as origin_log def log_temp_storage_location(): for no, item in enumerate(sorted(Path(temp_storage_location).iterdir()), 1): with item.open("rb") as f: origin_log.info("\t%i %s -> %r", no, item, repr(f.read())) for content_no in range(1, 7): # Save 4 times different content content = "content %i" % content_no for file_no in range(1, 5): origin_log.critical("_" * 100) origin_log.critical("Save %r for %i time(s)", content, file_no) with TemporaryUploadedFile("same_filename.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file.write(bytes(content, "utf-8")) tmp_file.flush() tmp_file.seek(0) instance = OverwriteFileSystemStorageModel.objects.create(file=tmp_file) print(repr(instance)) log_temp_storage_location() print(instance.file.path) assert_is_file(instance.file.path) assert instance.file.name == "same_filename.txt" assert_filenames_and_content( path=temp_storage_location, reference=[ ("same_filename.txt", b"content 6"), ("same_filename.txt.bak01", b"content 1"), ("same_filename.txt.bak02", b"content 2"), ("same_filename.txt.bak03", b"content 3"), ("same_filename.txt.bak04", b"content 4"), ("same_filename.txt.bak05", b"content 5"), ], )