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 add_from_file(*, gpx_file_file_path, user): """ Read content from gpx file <gpx_files_file_path> and add to <user> """ assert_is_file(gpx_file_file_path) with gpx_file_file_path.open("r") as f: gpx_content = f.read() return add_gpx(gpx_content=gpx_content, user=user)
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 file_compare(path1, path2): log.debug("compare %r vs, %r", path1, path2) assert_is_file(path1) assert_is_file(path2) # check if file refer to same file if path1.samefile(path2): log.debug("File refer to the same file") return True # compare file size: size1 = path1.stat().st_size size2 = path2.stat().st_size if size1 != size2: log.debug("Not the same file: different file size.") return False # Compare file content: filecmp.clear_cache() # if we didn't clear the cache: unittests may be failed! return filecmp.cmp(str(path1), str(path2), shallow=False) # str() needed for python 3.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"), ], )
def file_compare(path1, path2): log.debug("compare %r vs, %r", path1, path2) assert_is_file(path1) assert_is_file(path2) # check if file refer to same file if path1.samefile(path2): log.debug("File refer to the same file") return True # compare file size: size1 = path1.stat().st_size size2 = path2.stat().st_size if size1 != size2: log.debug("Not the same file: different file size.") return False # Compare file content: filecmp.clear_cache( ) # if we didn't clear the cache: unittests may be failed! return filecmp.cmp(str(path1), str(path2), shallow=False) # str() needed for python 3.5
def assert_is_file(self, path): warnings.warn("Use django_tools.unittest_utils.assertments.assert_is_file!", DeprecationWarning) assertments.assert_is_file(path)
def test_is_file_failed_file_not_exists(self): with self.assertRaises(AssertionError) as cm: assert_is_file("/notexisting.txt") assert_pformat_equal(cm.exception.args[0], "File not exists: /notexisting.txt")
def test_is_existing_file_with_string(self): assert_is_file(str(self.existing_file_path))
def test_is_existing_file_with_path_instance(self): assert_is_file(self.existing_file_path)
def fixture_content(file_name, mode='rb'): file_path = FIXTURES_PATH / file_name assert_is_file(file_path) with file_path.open(mode) as f: return f.read()
def test_is_existing_file_with_string(self): assert_is_file(str(self.existing_file_path))
def test_is_file_failed_not_directory(self): with self.assertRaises(AssertionError) as cm: assert_is_file("/path/to/does/not/exists/foo.txt") assert_pformat_equal(cm.exception.args[0], "Directory not exists: /path/to/does/not/exists")
def test_is_file_failed_not_directory(self): with self.assertRaises(AssertionError) as cm: assert_is_file("/path/to/does/not/exists/foo.txt") assert_pformat_equal(cm.exception.args[0], "Directory not exists: /path/to/does/not/exists")
def test_is_existing_file_with_path_instance(self): assert_is_file(self.existing_file_path)
def test_is_file_failed_file_not_exists(self): with self.assertRaises(AssertionError) as cm: assert_is_file("/notexisting.txt") assert_pformat_equal(cm.exception.args[0], "File not exists: /notexisting.txt")